SquigglyMonkey
asked on
script help with displaying content and saving it.
I have a simple script to check up down of ips. I have it displaying the way I would like, I would also like to save all of those up/downs to a text file. ( I have to send the results to someone else.) When I try to pipe it after the write-hosts are done, I get "An empty pipe element..."
Is there a simple way to get both?
Is there a simple way to get both?
Get-content c:\path\names.txt | %{
If (Test-Connection $_ -Quiet -Count 2){
write-host -foregroundcolor black -backgroundcolor green "$_ is UP"
}
Else{
write-host -foregroundcolor black -backgroundcolor red "$_ is Down"
}
}
|Out-File c:\path\result.txt
Thanks!
ASKER
Oh,,,, nice, let me test this real quick, thankyou!!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I used the latter and I got an error that :
"At c:path\script.ps1:1 char:75
+ Get-Content -Path c:\names.txt| ForEach-Object {
+ ~
Missing closing '}' in statement block."
So I added a } and it ran, but is only displaying and appending the down ip's.
"At c:path\script.ps1:1 char:75
+ Get-Content -Path c:\names.txt| ForEach-Object {
+ ~
Missing closing '}' in statement block."
So I added a } and it ran, but is only displaying and appending the down ip's.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yup, pebkac......
It works great now. Thanks to both of you. Both of the solutions work for me, just adjusting the path is all it takes.
It works great now. Thanks to both of you. Both of the solutions work for me, just adjusting the path is all it takes.
Glad to help. :)
ASKER
Also, since both solutions append the file, to keep results from inadvertently adding to one already there, from earlier in the night. I added a date-time stamp to the file name
$datetime = (get-date -f MM-dd-yyyy-hh-mm)
Get-Content -Path D:\path\ips.txt | ForEach-Object {
If (Test-Connection $_ -Quiet -Count 2) {
$BackgroundColor = 'Green'
$Message = "$($_) is UP"
} Else {
$BackgroundColor = 'Red'
$Message = "$($_) is Down"
}
$Message | Tee-Object -FilePath D:\path\$datetime-results.txt -Append | Write-Host -ForegroundColor Black -BackgroundColor $BackgroundColor
}
I'd suggest using a timestamp like 'yyyy-MM-dd_HH-mm'. This allows you to see the files sorted in the order in which they were created.
ASKER
I shortened the time the script runs considerably by making the count 1, in the first check, and I added a second check, and made it count 2. How can I suppress it displaying 'false'? I messed around with Elseif, but I could not get that to help me.
The display looks like this :
The display looks like this :
10.10.10.111 is UP
10.10.10.112 is UP
False
10.10.10.113 is Down
$datetime = (get-date -f yyyy-MM-dd-HH-mm)
Get-Content -Path D:\path\ips.txt | ForEach-Object {
If (Test-Connection $_ -Quiet -Count 1) {
$BackgroundColor = 'Green'
$Message = "$($_) is UP"
} Else {
(Test-Connection $_ -Quiet -Count 2)
$BackgroundColor = 'Red'
$Message = "$($_) is Down"
}
$Message | Tee-Object -FilePath D:\path\$datetime-results.txt -Append | Write-Host -ForegroundColor Black -BackgroundColor $BackgroundColor
}
Replace line 7 with
Test-Connection $_ -Quiet -Count 2 | Out-Null
So you either need to duplicate the output like so:
Open in new window
Or use Tee-Object (PS 3.0 and later):Open in new window