Several Azure PowerShell cmdlets have very tedious run times; a trivial task such as setting a static IP address on a virtual machine can take more than a minute (I guess this is because the cmdlet is actually sending commands to Azure and waiting for Azure to perform the requested task and report back); thus, especially when I need to use them a lot (and for independent operations), I'd really like to run them as background jobs, so to be able to send several requests to Azure and run them in parallel.

Take for example this code, which sets static IP addresses on several VMs:

$vms = @{

foreach ($vm in $vms.GetEnumerator())
    get-azurevm $vm.Key | Set-AzureStaticVNetIP $vm.Value | Update-AzureVM

This takes forever, as each command can take more than a minute, and needs to be finished before the next one is executed.

Is it possible to tell to Azure cmdlets "just return and keep doing what you need, then report back"?

I've tried using Start-Job, but this doesn't work, because backgound jobs don't inherit the environment of the parent session, and apart from the obvious missing variables, this means they are not logged into Azure and thus can't execute any command.

  • Try casting the expression to the void, does that work? If that sounds like greek let me know and I'll give you an example. – Colyn1337 Apr 10 '15 at 17:37
  • What does casting to void even remotely have to do with running in background?!? – Massimo Apr 10 '15 at 19:02
  • Good luck to you @massimo – Colyn1337 Apr 10 '15 at 19:23
  • Your comment would at least make a slight bit of sense if I had asked how to ignore the command output; but this, as should be obvious, has no effect at all on having to however wait for it to terminate. – Massimo Apr 10 '15 at 19:54
  • @Masimo, that's where you're wrong. But like I said, good luck to you. Hopefully someone will come by and assist you. – Colyn1337 Apr 10 '15 at 19:58

Background runspaces should work for you.

Using Background Runspaces Instead of PSJobs For Better Performance


Multithreading Powershell Scripts



How do I run my PowerShell scripts in parallel without using Jobs?

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.