Solved

powershell write-output

Posted on 2009-07-15
2
4,149 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:bryan oakley-wiggins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:bryan oakley-wiggins
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
The viewer will learn how to count occurrences of each item in an array.

751 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