I have a big question for me.
In my situation, I have a cmd batch file and Powershell file. And the cmd batch script will call to Powershell file to process.
The question is "if the Powershell file is running and appear an abnormal case, e.g. Due to max attempts reached and exit, etc... And the cmd batch file is whether to check with Powershell file to determine keep going or exit?
Thanks.
cmd code
POWERSHELL -noP -c "& { Start-Process POWERSHELL -ArgumentList '-ExecutionPolicy Bypass -File "D:\powershell\callfromcmd.ps1"' -Wait }"
PowerShell code
function getLog() {
$attempts = 0
$flags = $false
do {
try {
if ($attempts -gt 2) {
Write-Host
Write-Host 'INFO: Maximum Retry Attempts Reached! Script Exit!' -BackgroundColor BLACK -ForegroundColor YELLOW
Start-Sleep 5
exit
}
Write-Host 'Updating...Please Wait!'
Write-Host
$ChromeService = [OpenQA.Selenium.Chrome.ChromeDriverService]::CreateDefaultService()
$ChromeService.HideCommandPromptWindow = $true
$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeOptions.addargument('--headless')
$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($ChromeService,$ChromeOptions)
$ChromeDriver.Navigate().GoToUrl('http://192.168.1.1/EventLog.cgi')
$ChromeDriver.FindElementByXpath('/html/body/div[2]/table/tbody/tr/td[1]/select').click()
$ChromeDriver.quit()
Write-Host 'INFO: Update Completed!' -BackgroundColor BLACK -ForegroundColor GREEN
Start-Sleep 1
$flags = $false
} catch {
Write-Host 'Error: Update Failure! Go To Retry Again!' -BackgroundColor BLACK -ForegroundColor RED
$ChromeDriver.quit()
Start-Sleep 1
$attempts++
$flags = $true
Start-Sleep 2
}
} while ($flags)
}