0

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 = @{
"VM1"="192.168.0.11"
"VM2"="192.168.0.12"
"VM3"="192.168.0.13"
"VM4"="192.168.0.14"
"VM5"="192.168.0.15"
}

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.

Massimo
  • 68,714
  • 56
  • 196
  • 319
  • 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
  • You may have better luck at StackOverflow if you don't get an answer here since it seems to be a general Powershell question too. – proteus Apr 10 '15 at 20:05
  • @Colyn1337, seriously, casting a command to void is useful for ignoring its output, but this doesn't change how PowerShell still has to wait for it to complete before executing the next one. FYI, I even went as far as testing it: as expected, each command still had to be completed before the next one was executed. – Massimo Apr 10 '15 at 20:16
  • @Colyn1337, if you think I'm wrong please show me how you'd solve this with a cast to void. Or in any other way. I'd really appreciate it. – Massimo Apr 10 '15 at 20:17

1 Answers1

0

Background runspaces should work for you.

Using Background Runspaces Instead of PSJobs For Better Performance

http://learn-powershell.net/2012/05/13/using-background-runspaces-instead-of-psjobs-for-better-performance/

Multithreading Powershell Scripts

http://thesurlyadmin.com/2013/02/11/multithreading-powershell-scripts/

ServerFault

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

Bruno Faria
  • 3,804
  • 1
  • 11
  • 18