@@ -181,7 +181,10 @@ function New-TestFxServicePrincipal {
181181 }
182182 }
183183
184- $sp = Invoke-TestFxCommand - Command " New-AzADServicePrincipal -DisplayName $ServicePrincipalDisplayName "
184+ $sp = Invoke-TestFxCommand - ScriptBlock {
185+ param ($DisplayName )
186+ New-AzADServicePrincipal - DisplayName $DisplayName
187+ } - Parameters @ { DisplayName = $ServicePrincipalDisplayName }
185188 Start-Sleep - Seconds 10
186189 Set-TestFxServicePrincipalPermission - SubscriptionId $SubscriptionId - ServicePrincipalObjectId $sp.Id
187190
@@ -204,7 +207,14 @@ function Set-TestFxServicePrincipalPermission {
204207 try {
205208 $spRoleAssg = Get-AzRoleAssignment - ObjectId $ServicePrincipalObjectId - Scope $scope - RoleDefinitionName $roleName - ErrorAction Stop
206209 if ($null -eq $spRoleAssg ) {
207- Invoke-TestFxCommand - Command " New-AzRoleAssignment -ObjectId $ServicePrincipalObjectId -RoleDefinitionName $roleName -Scope $scope | Out-Null"
210+ Invoke-TestFxCommand - ScriptBlock {
211+ param ($ObjectId , $RoleName , $Scope )
212+ New-AzRoleAssignment - ObjectId $ObjectId - RoleDefinitionName $RoleName - Scope $Scope | Out-Null
213+ } - Parameters @ {
214+ ObjectId = $ServicePrincipalObjectId
215+ RoleName = $roleName
216+ Scope = $scope
217+ }
208218 }
209219 }
210220 catch {
@@ -264,34 +274,46 @@ function Get-TestFxEnvironment {
264274function Invoke-TestFxCommand {
265275 [CmdletBinding ()]
266276 param (
267- [Parameter (Mandatory , ValueFromPipeline )]
277+ [Parameter (Mandatory )]
268278 [ValidateNotNullOrEmpty ()]
269- [string ] $Command
279+ [scriptblock ] $ScriptBlock ,
280+
281+ [Parameter ()]
282+ [hashtable ] $Parameters = @ {},
283+
284+ [Parameter ()]
285+ [int ] $MaxRetries = 3 ,
286+
287+ [Parameter ()]
288+ [int ] $RetryDelaySeconds = 5
270289 )
271290
272291 $cmdRetryCount = 0
273292
274293 do {
275294 try {
276- Write-Verbose " Start to execute the command `" $Command `" ."
277- $cmdResult = Invoke-Expression - Command $Command - ErrorAction Stop
278- Write-Verbose " Successfully executed the command `" $Command `" ."
295+ Write-Verbose " Start to execute the command."
296+
297+ $cmdResult = & $ScriptBlock @Parameters - ErrorAction Stop
298+
299+ Write-Verbose " Successfully executed the command."
300+
279301 $cmdResult
280302 break
281303 }
282304 catch {
283305 $cmdErrorMessage = $_.Exception.Message
284- if ($cmdRetryCount -le 3 ) {
285- Write-Warning " Error occurred when executing the command `" $Command `" with error message `" $cmdErrorMessage `" ."
286- Write-Warning " Will retry automatically in 5 seconds."
306+ if ($cmdRetryCount -le $MaxRetries ) {
307+ Write-Warning " Error occurred when executing the command with error message `" $cmdErrorMessage `" ."
308+ Write-Warning " Will retry automatically in $RetryDelaySeconds seconds."
287309 Write-Host
288310
289- Start-Sleep - Seconds 5
311+ Start-Sleep - Seconds $RetryDelaySeconds
290312 $cmdRetryCount ++
291- Write-Warning " Retrying #$cmdRetryCount to execute the command `" $Command `" ."
313+ Write-Warning " Retrying #$cmdRetryCount to execute the command."
292314 }
293315 else {
294- throw " Failed to execute the command `" $Command `" after retrying for 3 times with error message `" $cmdErrorMessage `" ."
316+ throw " Failed to execute the command after retrying for $MaxRetries times with error message `" $cmdErrorMessage `" ."
295317 }
296318 }
297319 }
0 commit comments