Solved

powershell write-output

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
A brief introduction to what I consider to be the best editor for PowerShell.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

778 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