Kelly Garcia
asked on
start all VM's immediately
Hi All,
I have this script to auto-start Vm's
Problem is that there are Vm's that take a long time to start up therefore it gets stuck for a long time, until it starts up the next vm in the foreach loop. Is there a way I can start all vm's at once? thank you in advance for your help.
I have this script to auto-start Vm's
$WithoutTags=Get-Azurermvm
Foreach($VM in $WithoutTags)
{
$Value1=$VM.Tags.Values
$value2=$VM.Tags.Keys
if($value1 -eq "True" -and $value2 -eq "Start VM")
{
$ResourceGroupName = $vm | select -expandproperty resourcegroupname
Write-output "$($VM.Name) is starting"
Start-Azurermvm -Name $VM.Name -ResourceGroupName $ResourceGroupName -ErrorAction Continue ;
}
Else
{
Write-output "Not starting the VM $VM.Name"
}
}
Problem is that there are Vm's that take a long time to start up therefore it gets stuck for a long time, until it starts up the next vm in the foreach loop. Is there a way I can start all vm's at once? thank you in advance for your help.
I am not a powershell expert. I know exactly how to do this in a UNIX shell script, so some searching pointed me to the documentation for Start-Process. It looks like that is what you would need. It will spawn off another process to start your VM and then your loop would continue.
Start-AzureRmVM supports the -AsJob switch, which makes it a background task. You should collect the returned job object to allow for waiting and cleanup after completion.
$jobs = @()
Foreach ( $VM in Get-AzureRmVM )
{
if ( $_.Tags.Values -and $_.Tags.Keys -eq 'Start VM' )
{
$ResourceGroupName = $vm | select -expandproperty resourcegroupname
Write-Output "$($VM.Name) is starting"
$jobs += Start-Azurermvm -Name $VM.Name -ResourceGroupName $ResourceGroupName -ErrorAction Continue -asJob
}
Else
{
Write-output "Not starting the VM $VM.Name"
}
}
$jobs | Wait-Job | Remove-Job -Force
ASKER
Thank you very much Qlemo, this is very useful. is there a way I can add to the script to stop trying to start vm's that are already started?
If you loop through all VMs once, then each VM will only have one attempted start.
ASKER
I forgot to mention the script is within a workflow :
workflow Start_VMs_8AM
{
}
I am not sure if this is why the job fails?
workflow Start_VMs_8AM
{
}
I am not sure if this is why the job fails?
ASKER
I get this error with the Azure Runbooks test pane:
Start-AzureRmVM : A parameter cannot be found that matches parameter name 'asJob'.
At Start_VMs_8AM:39 char:39
+
+ CategoryInfo : InvalidArgument: (:) [Start-AzureRmVM], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Azure.Commands.Compute.StartAzureVMCommand
I think you may have passed "asJob" rather than "-asJob".
Looks like you're missing the leading "-" or you've added this flag at the wrong place.
Looks like you're missing the leading "-" or you've added this flag at the wrong place.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it set as -asjob
David, that cannot be the culprit as AsJob as been identified correctly as parameter name.
And there is no "wrong place" for parameters, the sequence does not matter; of course a parameter value has to follow the correspondig parameter name.
And there is no "wrong place" for parameters, the sequence does not matter; of course a parameter value has to follow the correspondig parameter name.
ASKER
Hi Qlemo,
This code doesn't work in azure I will open another case. Thank you for the help.
Regards,
Kelly
This code doesn't work in azure I will open another case. Thank you for the help.
Regards,
Kelly
I'll not be able to help you then, as I cannot perform tests against Azure.