Reputation: 1192
I'm struggling a little bit with this logic, and I'm hoping someone can point out and explain where I'm going wrong - I've tried all sorts of variations but haven't been able to get it quite right.
What I want is for it to run the uninstall.bat script and monitor the log file for the exit code. Then if the exit code was 0 continue with the script, if the exit code was anything other than 0 repeat the uninstall.bat up to a maximum of 4 times.
When I try to wildcard either side of the string with * it doesn't work - I don't think it's the string that's the issue though, I think it's the method/approach I'm taking.
Another slight complication is that the log file will always have 'CcmSetup is exiting with return code' as part of the last line initially, this changes as the bat file runs, but when completed will end with 'CcmSetup is exiting with return code 0' in the log file.
do{
$count = 1
Write-Output "Uninstalling Software Center - attempt $count"
Start Uninstall-Local.bat
$count++
While((Get-Content C:\Windows\ccmsetup\Logs\ccmsetup.log -tail 1 -Wait | Select-String -pattern "CcmSetup is exiting with return code" -SimpleMatch -Quiet)){Start-sleep -s 10}
} until((Get-Content C:\Windows\ccmsetup\Logs\ccmsetup.log -tail 1 -Wait | Select-String -pattern "CcmSetup is exiting with return code 0" -SimpleMatch -Quiet) -or $count -gt 3)
On the while() I've tried it with -notmatch and -not seperately. It doesn't seem to evaluate to false and thus doesn't move on from there.
Thank you in advance, any help/insight is much appreciated.
Upvotes: 1
Views: 813
Reputation: 694
Initialize your $count Variable before you enter the loop! Currently you are resetting it with every loop back to 1.
Personally I would use the -Wait Parameter of Start-Process instead of looping through a log file waiting for a specific string. Something like
$count = 1
$LogFilePath = 'C:\Windows\ccmsetup\Logs\ccmsetup.log'
do {
Start-Process Uninstall-Local.bat -Wait
$count++
} until ((Get-Content $LogFilePath -tail 1 | Select-String -pattern "CcmSetup is exiting with return code 0" -SimpleMatch) -or ($count -gt 3))
Upvotes: 2