0

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)
    
} 
user11343272
  • 77
  • 1
  • 1
  • 9
  • 4
    It's very unclear what your question is - how would the PowerShell process be running and having exited simultaneously? – Mathias R. Jessen Sep 25 '21 at 12:55
  • Let's me more detail to explain again. I mean the Powershell file is a script for looping some data. And it will have a max attempts and force to quit when reached attempts to prevent infinity loop. – user11343272 Sep 25 '21 at 15:37
  • Why are you using batch to start PowerShell to start PowerShell? Just run the script from the first POWERSHELL. And if the script is your own, use "RemoteSigned" instead of "Bypass", it's a better option. In your script, define what an "abnormal condition" is and detect it, then use the "exit" keyword to return an exit code > 0, and read the exit code from that. I don't use batch files much, but I think something like if ERRORLEVEL 1 should work? – DarkMoon Sep 25 '21 at 22:14
  • It is because the cmd batch is a main program, and the PowerShell script is a assistance script to take action on web by selenium. That's why I need to make two files. So I am thinking if the Powershell script occurs failure result, how can prevent the cmd batch go on running? Thanks. [UPDATED PowerShell code above] – user11343272 Sep 26 '21 at 00:50
  • No, I mean you're calling POWERSHELL -c 'POWERSHELL -f' in your batch file; why not just call POWERSHELL -f directly? And using 'if ERRORLEVEL 1' in your batch file (search "batch file if site:learn.microsoft.com" for how to use that) after that should catch the exit code, and then you exit however you normally exit batch files; is there an exit or end or quit keyword? I don't use batch files much, other than to just call a script when something doesn't support it drectly, so I'm not sure myself. – DarkMoon Sep 26 '21 at 01:19

0 Answers0