Solved

powershell write-output

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 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

13 Experts available now in Live!

Get 1:1 Help Now