Skip to content

Invoke-Retry

SYNOPSIS

Runs a scriptblock with a retry mechanism.

SYNTAX

Invoke-Retry [-Run] <ScriptBlock> [[-Count] <Int32>] [[-Delay] <Int32>] [[-Catch] <ScriptBlock>]
 [[-Finally] <ScriptBlock>] [-ProgressAction <ActionPreference>] [<CommonParameters>]

DESCRIPTION

Runs a scriptblock with a retry mechanism. If the scriptblock fails, it will retry the scriptblock a number of times with a delay between each try. If a catch scriptblock is provided, it will run that scriptblock if the scriptblock fails.

EXAMPLES

EXAMPLE 1

Retry -Count 5 -Delay 5 -Run {
    Invoke-RestMethod -Uri 'https://api.myip.com/'
}

PARAMETERS

-Run

The scriptblock to run

Type: ScriptBlock
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Count

The number of tries to make

Type: Int32
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: 3
Accept pipeline input: False
Accept wildcard characters: False

-Delay

The delay between tries in seconds

Type: Int32
Parameter Sets: (All)
Aliases:

Required: False
Position: 3
Default value: 5
Accept pipeline input: False
Accept wildcard characters: False

-Catch

A scriptblock to run if it fails

Type: ScriptBlock
Parameter Sets: (All)
Aliases:

Required: False
Position: 4
Default value: {}
Accept pipeline input: False
Accept wildcard characters: False

-Finally

A scriptblock to run after the scriptblock has run

Type: ScriptBlock
Parameter Sets: (All)
Aliases:

Required: False
Position: 5
Default value: {}
Accept pipeline input: False
Accept wildcard characters: False

-ProgressAction

{{ Fill ProgressAction Description }}

Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

NOTES