Invoke-ADTCommandWithRetries
SYNOPSIS
Drop-in replacement for any cmdlet/function where a retry is desirable due to transient issues.
SYNTAX
Invoke-ADTCommandWithRetries [-Command] <Object> [-Retries <UInt32>] [-SleepDuration <TimeSpan>]
[-MaximumElapsedTime <TimeSpan>] [-SleepSeconds <UInt32>]
[-Parameters <System.Collections.Generic.IReadOnlyList`1[System.Object]>] [<CommonParameters>]
DESCRIPTION
This function invokes the specified cmdlet/function, accepting all of its parameters but retries an operation for the configured value before throwing.
EXAMPLES
EXAMPLE 1
Invoke-ADTCommandWithRetries -Command Invoke-WebRequest -Uri https://aka.ms/getwinget -OutFile "$($adtSession.DirSupportFiles)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
Downloads the latest WinGet installer to the SupportFiles directory. If the command fails, it will retry 3 times with 5 seconds between each attempt.
EXAMPLE 2
Invoke-ADTCommandWithRetries Get-FileHash -Path '\\MyShare\MyFile' -MaximumElapsedTime (New-TimeSpan -Seconds 90) -SleepDuration (New-TimeSpan -Seconds 1)
Gets the hash of a file on an SMB share. If the connection to the SMB share drops, it will retry the command every 2 seconds until it successfully gets the hash or 90 seconds have passed since the initial attempt.
EXAMPLE 3
Invoke-ADTCommandWithRetries Copy-ADTFile -Path '\\MyShare\MyFile' -Destination 'C:\Windows\Temp' -Retries 5 -MaximumElapsedTime (New-TimeSpan -Minutes 5)
Copies a file from an SMB share to C:\Windows\Temp. If the connection to the SMB share drops, it will retry the command once every 5 seconds until either 5 attempts have been made or 5 minutes have passed since the initial attempt.
PARAMETERS
-Command
The name of the command to invoke.
Type: Object
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-Retries
How many retries to perform before throwing.
Type: UInt32
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: 3
Accept pipeline input: False
Accept wildcard characters: False
-SleepDuration
How long to sleep between retries.
Type: TimeSpan
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: [System.TimeSpan]::FromSeconds(5)
Accept pipeline input: False
Accept wildcard characters: False
-MaximumElapsedTime
The maximum elapsed time allowed to passed while attempting retries. If the maximum elapsted time has passed and there are still attempts remaining they will be disgarded.
If this parameter is supplied and the -Retries
parameter isn't, this command will continue to retry the provided command until the time limit runs out.
Type: TimeSpan
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-SleepSeconds
This parameter is obsolete and will be removed in PSAppDeployToolkit 4.2.0. Please use -SleepDuration
instead.
Type: UInt32
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: 5
Accept pipeline input: False
Accept wildcard characters: False
-Parameters
A 'ValueFromRemainingArguments' parameter to collect the parameters as would be passed to the provided Command.
While values can be directly provided to this parameter, it's not designed to be explicitly called.
Type: System.Collections.Generic.IReadOnlyList`1[System.Object]
Parameter Sets: (All)
Aliases:
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
None
You cannot pipe objects to this function.
OUTPUTS
System.Object
Invoke-ADTCommandWithRetries returns the output of the invoked command.
NOTES
An active ADT session is NOT required to use this function.
Tags: psadt
Website: https://psappdeploytoolkit.com
Copyright: (C) 2025 PSAppDeployToolkit Team (Sean Lillis, Dan Cunningham, Muhammad Mashwani, Mitch Richters, Dan Gough).
License: https://opensource.org/license/lgpl-3-0
RELATED LINKS
https://psappdeploytoolkit.com/docs/reference/functions/Invoke-ADTCommandWithRetries