Rerun command after sleep if previous execution of command failed

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Preenesh Nayanasudhan Preenesh Nayanasudhan 4 months, 4 weeks ago.

  • Author
    Posts
  • #46176

    Here is what I am trying to do, I am trying to run a command line for example netdom and out the result to a file. I read the output back into a variable and check if the command was successful. If the command was unsuccessful it sleep for a few seconds and then retries the command again. It retries certain number of times.

    Problem I am having is with the second part, it does not go into sleep. Could someone help and let me know what I am doing wrong?

        $retries = 5
        $secondsDelay = 2
        $retrycount = 0
        $completed = $false
            
            while (-not $completed) {
            try {
                & netdom Join $env:COMPUTERNAME /PasswordM:$env:COMPUTERNAME$ /Domain:DM.COM\RODC0001.dm.com /ReadOnly | Out-File "C:\Install\Join.txt"
                $a = Get-Content -Path "C:\Install\Join.txt"
                if ($a -ne "The command failed to complete successfully.")
                {
                    Write-Verbose ("Command [{0}] succeeded.")
                    $completed = $true
                }
            } catch {
                if ($retrycount -ge $retries) {
                    Write-Verbose ("Command [{0}] failed the maximum number of {1} times.")
                    throw
                } else {
                    Write-Verbose ("Command [{0}] failed. Retrying in {1} seconds.")
                    Start-Sleep $secondsDelay
                    $retrycount++
                }
            }
        }
    
    
  • #46182
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    Netdom is not returning an exception so you are not falling into the catch logic. try this

        $retries = 5
        $secondsDelay = 2
        $retrycount = 0
        $completed = $false
            
            while (-not $completed){
                & netdom Join $env:COMPUTERNAME /PasswordM:$env:COMPUTERNAME$ /Domain:DM.COM\RODC0001.dm.com /ReadOnly | Out-File "C:\Install\Join.txt"
                $a = Get-Content -Path "C:\Install\Join.txt"
                if ($a -ne "The command failed to complete successfully."){
                    Write-Verbose ("Command [{0}] succeeded.")
                    $completed = $true
                }
    
                if ($retrycount -ge $retries){
                    Write-Verbose ("Command [{0}] failed the maximum number of {1} times.")
                    throw
                } else {
                    Write-Verbose ("Command [{0}] failed. Retrying in {1} seconds.")
                    Start-Sleep $secondsDelay
                    $retrycount++
                }
        }
    
  • #46245
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Would the lastlogon or lastlogondate attributes of the computer object indicate that it is domain joined?

  • #46259

    Hi Jonathan.. Thank you.. Have modified the script that you provided to work as expected. Changed the Throw to Break to avoid error exception during execution. Here is the updated script.

        $retries = 5
        $secondsDelay = 10
        $retrycount = 0
        $completed = $false
            
            while (-not $completed){
                & netdom Join $env:COMPUTERNAME /PasswordM:$env:COMPUTERNAME$ /Domain:DM.COM\RODC0001.dm.com /ReadOnly | Out-File "C:\Install\Join.txt"
                $a = Get-Content -Path "C:\Install\Join.txt"
                if ($a -eq "The command complete successfully."){
                    Write-Output ("Command succeeded.")
                    $completed = $true
                }
                else                     
                {
                    if ($retrycount -ge $retries){
                        Write-Output ("Command failed the maximum number of $retrycount times.")
                        break
                    } else {
                        Write-Output ("Command failed. Retrying in $secondsDelay seconds.")
                        remove-item -path "C:\Install\Join.txt" -force
                        Start-Sleep $secondsDelay
                        $retrycount++
                    }
                }
        }
    

You must be logged in to reply to this topic.