Powershell – How to run Azure cmdlets in background

azurebackgroundbackground-processpowershell

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.

Best Answer

Related Topic