Solved

powershell write-output

Posted on 2009-07-15
2
4,144 Views
Last Modified: 2012-05-07
Hi

*** Envirnment***
PoSH v 1.0
Windows 2003

I have a daily check script I am working on and part of this will be to ping a bunch of servers from an input file (I will later on hook this into AD with a custom attribute for critical servers) but for now I take input from a file and display on screen...

What I am trying to do, is for each server that doesn't respond to ping, I want to write-host but also output the failures to a .log file...

I usually wrtie-host "something";write-output "something" | out-file ".\test.log" but I am getting the empty pipeline error....
Here is the current script -I would appreciate any guidance on how to write out the failures..!

### Script start ###
$readfile=Get-Content "w:\pingtest.txt"
foreach($readf in $readfile)
{
$ALive=get-wmiobject win32_pingstatus -Filter "Address='$readf'" | Select-Object statuscode

if($ALive.statuscode -eq 0)
{Write-Host $readf is REACHABLE -background "GREEN" -foreground "BLACk"}
else
{Write-Host $readf is NOT reachable -background "RED" -foreground "BLACk"}
}
### Script End ###

Cheers
Bry
0
Comment
Question by:BryanOakley
2 Comments
 
LVL 14

Accepted Solution

by:
flob9 earned 250 total points
ID: 24857709
This works for me (no changes); check typo
### Script start ###
$readfile=Get-Content "c:\pingtest.txt"
foreach($readf in $readfile)
{
$ALive=get-wmiobject win32_pingstatus -Filter "Address='$readf'" | Select-Object statuscode 
 
if($ALive.statuscode -eq 0)
{
	Write-Host $readf is REACHABLE -background "GREEN" -foreground "BLACK"
	
}
else
{
	Write-Host $readf is NOT reachable -background "RED" -foreground "BLACK"; Write-Output "$readf is NOT reachable" | out-file 
 
".\test.log"
}
}
### Script End ###

Open in new window

0
 

Author Closing Comment

by:BryanOakley
ID: 31603634
flob9

Marvellous..!
I swear I tried that but mustn't have ;-)
To note - I just added -append  to the out-file to collect all failures...

Once again, massive thanks and very much appreciated..!

Cheers
Bry
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question