Write to logfile and to file

by rcjay272 at 2013-03-21 07:30:27

All,

I am working on a script to ping a list of devices and then get the file version. I have that part down, but I am having problems with the logging. When running the script I would like to have it output the results to both the console window and to separate log files. For the logfiles, I would like to have a "pingable" logfile with the file versions and a separate logfile for the "not reachables".

The script I have going below doesn’t seem to write each device to the logfile, only the last entry it processed. It does create both a goodversion.txt and a NotReachables.txt file.
My questions are:

Can I use write-host and out-file to output to the console and logfile at the same time?

Thanks for looking.

$servers = Get-Content -Path C:\Load_Stuff\MonitoringScripts\EndPoints.txt
ForEach ($server in $servers) {
if (Test-Connection $server -quiet) {
gwmi -computername $server -query "select * from cim_datafile where name = ‘c:\Windows\system32\notepad.exe’ " |
Sort name,CSName |
Select Version,CSName|
Out-File C:\Load_Stuff\MonitoringScripts\PowerShell\GoodVersions.txt
}
else {
write-warning "Failed to ping $server"|
Out-File C:\Load_Stuff\MonitoringScripts\PowerShell\NotReachables.txt
}
}
by mjolinor at 2013-03-21 07:38:06
Would Start-Transcript be appropriate?
by ArtB0514 at 2013-03-21 11:03:33
Also look at Tee-Object
by rcjay272 at 2013-03-22 09:41:39
[quote="ArtB0514"]Also look at Tee-Object[/quote]

Thank you…Tee-Object rocks…Testing it seems straight forward. Will add it to the script and let you know.

Thank you,
ROb
by rcjay272 at 2013-03-22 12:35:34
Guys,

I am trying to get this to write to 2 different log files. Goodversions and badversion. It is not outputting anything in the badversion txt file.

Do I have a format wrong here?

$servers = Get-Content -Path C:\Load_Stuff\MonitoringScripts\130.txt
ForEach ($server in $servers) {
if (Test-Connection $server -quiet) {
gwmi -computername $server -query "select * from cim_datafile where name = ‘c:\Program Files\eof\SoftClient\TiSoftClient.exe’ " |
Sort name,CSName |
Select Version,CSName|
#Out-Host
Out-File C:\Load_Stuff\MonitoringScripts\PowerShell\GoodVersions.txt -Append | Format-Table
}
else {
write-warning "Failed to ping $server"|
Out-File C:\Load_Stuff\MonitoringScripts\PowerShell\BadVersions.txt -Append
}
}
by ArtB0514 at 2013-03-22 13:05:18
Look at the OUTPUTS section of Get-Help Write-Warning -Full (or -Online). Then try this:

else {
write-warning "Failed to ping $server"
"Failed to ping $Server" | Out-File C:\Load_Stuff\MonitoringScripts\PowerShell\BadVersions.txt -Append
}