Written by 5:13 pm Hosting, Microsoft, PowerShell, Windows Server • 12 Comments

Powershell: Simple retry logic

Powershell retry loop

I am working on some larger PowerShell scripts right now and so I needed to create a simple retry logic for sending web request to a server. This here gives you a simple retry logic for PowerShell.

Powershell retry loop

# Comment
$Stoploop = $false
[int]$Retrycount = "0"
 
do {
try {
Scripts Commands here
Write-Host "Job completed"
$Stoploop = $true
}
catch {
if ($Retrycount -gt 3){
Write-Host "Could not send Information after 3 retrys."
$Stoploop = $true
}
else {
Write-Host "Could not send Information retrying in 30 seconds..."
Start-Sleep -Seconds 30
$Retrycount = $Retrycount + 1
}
}
}
While ($Stoploop -eq $false)

Tags: , , , , , , , , Last modified: January 14, 2019
Close Search Window
Close