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++
}
}
}
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++
}
}
Would the lastlogon or lastlogondate attributes of the computer object indicate that it is domain joined?
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++
}
}
}