Solved

powershell write-output

Posted on 2009-07-15
2
4,137 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now