I have a PowerShell Web API endpoint running on Azure that takes more than the maximum duration of 230 seconds on an Azure Web App. Is it possible to start a background job consisting of PowerShell code from my PowerShell Web API endpoint, return the Sitecore Job ID as result from the Web API endpoint, and check with calls to a PowerShell Web API endpoint if the job is completed?


If you check out the docs here you can see that there are some commands to create script sessions and later poll for the results. Some of the commands exist both in the Sitecore context as well as in the SPE Remoting module.

  • Get-ScriptSession - Returns details about script sessions.
  • Receive-ScriptSession - Returns the results of a completed script session.
  • Remove-ScriptSession - Removes the script session from memory.
  • Start-ScriptSession - Executes a new script session.
  • Stop-ScriptSession - Terminates an existing script session.
  • Wait-ScriptSession - Waits for all the script sessions to complete before continuing.

Example: The following starts a script that changes its path to "master:" and sleeps for 4 seconds. The session will persist in memory as no -AutoDispose parameter has been provided.

Start-ScriptSession -ScriptBlock { cd master:\; Start-Sleep -Seconds 4 } -Id "Background Task"
  • I use these commands with remoting, and they work great! But can I use these also from a Web API call, return a job ID, and poll for the job ID? then I only have to open up restfulv2 with controlled endpoints, instead of opening up PowerShell remoting. Apr 12 '21 at 19:48
  • You should be able to return whatever you want from the web API call. The polling code in SPE remoting could be used in conjunction or can be copied to your web API scripts. Apr 12 '21 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.