How to call a function again inside a function in powershell

262 views Asked by At

I'm trying to implement pooling function for datamovement operation in dynamic 365. I have a PowerShell script which is having a function called Test(pera1,pera2,pera3..) when we call this function it will take around 1hr+ time to complete the event. Now I have to call another task based on this Test() function result. If my condition is match then will call another task otherwise I have to put Start-Sleep -Seconds 3600 to complete the event.

function EnvOperationPooling($pera1, $pera2, $pera3)
{
 // here API call code 
// API result
if($apiResponse.DeploymentState-eq 'Inprogress')
{
  Start-Sleep -Seconds 3600
  EnvOperationPooling -proj1 $pera1 -proj2 $pera2 -proj2 $pera2
 Write-Host "##vso[task.setvariable variable=DeploymentState;isOutput=true]$($apiResponse.DeploymentState)"
}
else
{
 Write-Host "##vso[task.setvariable variable=DeploymentState;isOutput=true]$($apiResponse.DeploymentState)"
}

}

how i can make it recursive any suggetion to make above code better ..?

1

There are 1 answers

0
Delliganesh Sevanesan On

For recursive operation & time saving of your process you need use background jobs of PowerShell.

if you start a background job, the command prompt returns immediately, even if the job takes an extended time to complete. You can continue to work in the session without interruption while the job runs. Refer

Start-Job -ScriptBlock {Get-Process}

enter image description here

Refer for more here