0

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:

  1. Start a service,
  2. "Tail" the log file
  3. Confirm that the log file contains the entry ($waitfor)
  4. Once the log entry is generated in the Terminate
$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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Much appreciated, postanote! I want to ellaborate on your observations. For starters I am dealing with a customized applications with temperment issues; its a casino gaming app. I NEED to ensure (by using tail or anything) that the the log entry appears in the file as the log entries are enermurated as a result of starting a previous service. If this particular log entry is not in the log file and I start the next dependent service, the gaming app doesn't work. It's a design flaw the vendor is aware of. For now, we are simply trying to automate this tideous task.
I don't mind executing this using either case you presented: Use a instance for each server or doing one at a time aka paint drying session as you said since the log entries is on average generated around the ~2 min mark.
I had a similar issue with a enterprise vendor solution I have to support, and took this approach as a repeatable troubleshooting effort for the field teams. My tool would look for the string(s), send and alert, then start other services as needed only on success, then check again for all services. Why, paranoia. I use the ISE vs the console host, as the ISE allows multiple tabs to be open even when using remote servers. So, only one primary ISE GUI, with multiple local or remote tabs.
Well noted with thanks. You mentioned why am using tail. Does that mean I can perform this more efficiently by searching for a string? I am not sure how to do that. Indeed, the only reason I need to tail is to actually see/certify the log entry is generated to proceed to start my next service.
Depends on the goal. Tail is great when you what to catch and log entry the moment it happens. With string searching, of course that would engender a delay, that you'd decide on and find acceptable. Meaning you could just let the actions run all the targets at once and check for the string every few seconds on all of them. In my tool for about a 3 - 9 services on about a dozen servers, during troubleshooting sessions, I do that exact thing. I use the tail thing to trap log data as communication flows happen for correlation needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.