$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
}
PS C:\> 'Serious error, your computer will self destruct in 3 seconds.' -match '[4.2.3.GA (build: SVNTag=JBoss_4_2]'
True
PS C:\>
Why?$waitFor = [regex]::Escape('[4.2.3.GA (build: SVNTag=JBoss_4_2]')
Or, since there's no real need for a regex here, you can just use the -like operator instead of -match (assuming the string is enclosed in surrounding characters, otherwise a simple -eq will do):if (Get-Content $logfile2 -Tail 30 | ? {$_ -like "*$($waitFor)*"}) {
PS C:\Scripts> C:\Scripts\roll-2.1.startUIsvcs.ps1
Starting bits on servername1
Starting bits on servername1
Waiting for [4.2.3.GA (build: SVNTag=JBoss_4_2] in \\servername1\E$\all\log\wrapper.log - found
PS C:\Scripts>
PS C:\Scripts>
$serviceB = "bits"
$logfile2 = "E$\log\wrapper.log"
$waitFor = "[4.2.3.GA (build: SVNTag=JBoss_4_2]”
#$remotehosts = 'server1','server2' # and so on
foreach ($remotehost in $remotehosts) {
Write-Host "Starting $serviceB on $remotehost"
Start-Service $serviceB #-ComputerName $remotehost
}
$remotehostsleft = $remotehost
do {
foreach ($remotehost in $remotehostsleft) {
Write-Host "Waiting for $waitFor in \\$remotehost\$logfile2" -NoNewLine
if (Get-Content \\$remotehost\$logfile2 -Tail 30 | ? {$_ -match $waitFor}) {
write-Host " - found" -NoNewLine
$remotehostsleft = $remotehostsleft | ? { $_ -notmatch $remotehost }
}
}
}
while ($remotehostsleft)
$serviceB = "my application service"
$logfile2 = "F$\App\log\wrapper.log"
$waitFor = "[4.2.3.GA (build: SVNTag=JBoss_4_2]”
$hosts = 'server1', 'server2', 'server3' # and so on
foreach ($actHost in $hosts) {
Write-Host "Starting $serviceB on $actHost"
Start-Service $serviceB -ComputerName $actHost
}
$hostsleft = $hosts
do {
foreach ($actHost in $hostsleft) {
Write-Host "Waiting for $waitFor in \\$actHost\$logfile2" -NoNewLine
if (Get-Content "\\$actHost\$logfile2" -Tail 30 -ea SilentlyContinue | ? {$_ -like "*$($waitFor)*"}) {
write-Host " - found" -NoNewLine
$hostsleft = $hostsleft | ? { $_ -notmatch $host }
}
Write-Host
}
Start-Sleep 5
} while ($hostsleft)
PS C:\Scripts> C:\Scripts\SDS-roll-2.2.startUIsvcs.ps1
Starting bits on Server1
Start-Service : A parameter cannot be found that matches parameter name 'ComputerName'.
At C:\Scripts\roll-2.2.startUIsvcs.ps1:8 char:27
+ Start-Service $serviceB -ComputerName $actHost
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Service], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.StartServiceCommand
Starting bits on Server2
Start-Service : A parameter cannot be found that matches parameter name 'ComputerName'.
At C:\Scripts\roll-2.2.startUIsvcs.ps1:8 char:27
+ Start-Service $serviceB -ComputerName $actHost
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Service], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.StartServiceCommand
Waiting for [4.2.3.GA (build: SVNTag=JBoss_4_2] in \\Server1\E$\log\wrapper.log - found
Waiting for [4.2.3.GA (build: SVNTag=JBoss_4_2] in \\Server2\E$\log\wrapper.log - found
Waiting for [4.2.3.GA (build: SVNTag=JBoss_4_2] in \\server1\E$\log\wrapper.log - found
Waiting for [4.2.3.GA (build: SVNTag=JBoss_4_2] in \\Server2\E$\log\wrapper.log - found
Waiting for [4.2.3.GA (build: SVNTag=JBoss_4_2] in \\server1\E$\log\wrapper.log - found
Waiting for [4.2.3.GA (build: SVNTag=JBoss_4_2] in \\Server2\E$\log\wrapper.log - found
Write-Host "Starting $serviceB on $($hosts -join ', ')"
Get-Service $serviceB -ComputerName $hosts -ea SilentlyContinue | Start-Service
$serviceB = "my application service"
$logfile2 = "F$\App\log\wrapper.log"
$waitFor = "[4.2.3.GA (build: SVNTag=JBoss_4_2]”
$hosts = 'server1', 'server2', 'server3' # and so on
Write-Host "Starting $serviceB on $($hosts -join ', '"
Get-Service $serviceB -ComputerName $osts -ea SilentlyContinue | Start-Service
$hostsleft = $hosts
do {
foreach ($actHost in $hostsleft) {
Write-Host "`rWaiting for $actHost " -NoNewLine
if (Get-Content "\\$actHost\$logfile2" -Tail 30 -ea SilentlyContinue | ? {$_ -like "*${waitFor}*"}) {
write-Host " - found"
$hostsleft = $hostsleft | ? { $_ -ne $actHost }
}
}
Start-Sleep 5
} while ($hostsleft)
The last line always shows some "changing" info just to be sure the script really does something, and starts a new line whenever it found the search pattern on another server.
I would not try to use parallel execution, because that needs some setup (to get PowerShell jobs running, you need to enable WSMan Remoting on the excuting machine).
So the simple setup is to indeed use loops to go thru all targets in a cycle.
Open in new window
This should perform reaonsably, as it checks the remote machines in a pseudo-parallel way.