start all VM's immediately

Hi All,

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"

}
} 

Open in new window


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.
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I've not had the need to use workflows yet, so no experience about that ;-).
The AsJob switch is available with Azure RM PowerShell Module 5 (5.1.1), but not earlier, as I can now see.
However, that should not really matter. we can use start-job instead. But that requires more effort.
$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-Job -ScriptBlock { param($VMName, $RscGrp) Start-Azurermvm -Name $VMName -ResourceGroupName $RscGrp -ErrorAction Continue } -ArgumentList $VM.Name, $ResourceGroupName
  }
  Else
  {
    Write-output "Not starting the VM $VM.Name"
  }
} 
$jobs | Wait-Job | Remove-Job -Force

Open in new window

0
 
johnsoneSenior Oracle DBACommented:
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.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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

Open in new window

0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
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?
0
 
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
If  you loop through all VMs once, then each VM will only have one attempted start.
0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
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?
0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
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
 

Open in new window

0
 
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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.
0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
it set as -asjob
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
Hi Qlemo,

This code doesn't work in azure I will open another case. Thank you for the help.

Regards,
Kelly
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I'll not be able to help you then, as I cannot perform tests against Azure.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.