I need to polish up this script. I need it to run across multiple (5 - all have the log file in the same location) Windows systems. I am having a hard time figuring out how to ensure I can "tail" the log for the entry on all 5 servers simultaneously and return the 'OK'; terminate the job only when the log entry is generated on all 5 servers and not just let's say 4 of them.
Here is what it is meant to do:
$waitfor
) $serviceB = "my application service"
$logfile2 = "F:\App\log\wrapper.log"
$waitFor = "[4.2.3.GA (build: SVNTag=JBoss_4_2]”
Write-Host "Starting $serviceB"
Start-Service $serviceB
Write-Host "Waiting for $waitFor in $logfile2"
while (!((Get-Content $logfile2 -Tail 30) | ? {$_ -match $waitFor})) {
Start-Sleep -Seconds 5
}
How can I use a foreach
loop to accomplish let's say - tail and wait for log entry on all 5 remote systems in a CSV file?
Simply put, you can't, not in the same session, all at once. Each log must be in its own session. How do I know, because I've tried wayyyy too many time to achieve this. Well you could, do just one server at a time, and when that's done move to the next using a loop.
You can use a for loop to hit each server host, but you need to pop a separate PowerShell Instance for each.
Well you could in one console session by, doing just one server at a time, wait for that to complete, let it message you, and when that's done move to the next using a loop. Yet, if it takes a while for that string to be found, then that becomes like watching paint dry.
foreach ($RemoteHost in (Import-Csv -Path 'D:\Temp\serverlist.csv'))
{
# You code here
Invoke-Command -ComputerName $RemoteHost -Credential 'domain\adminname' -ScriptBlock {
# the rest of your code here:
}
}
Since you say remote host, then this means you need to have PSRemoting enabled for them , leveraging Implicit removing sessions and be using an account that is in the remote host local admin group.
You can use -tail -wait, and eliminate the need for that Start-Sleep and line grab step as well.
Using -tail -wait, will show the log as it writes each entry line when an entry is made.
Get-Content -Path $CurrentHostLog.FullName -Tail 0 -Wait
So, though I am not real sure why you'd want to -tail this and watch these log(s) scroll since you are only looking for a simple string
IMHO, I'd suggest just starting each as a background job in parallel, and have that job alert you; text, email, screen alert, etc., you when they return the result you are after.