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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Qlemo"Batchelor", 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
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
Check Out How Miercom Evaluates Wi-Fi Security!

It's not just about Wi-Fi connectivity anymore. A wireless security breach can cost your business large amounts of time, trouble, and expense. Plus, hear first-hand from Miercom on how WatchGuard's Wi-Fi security stacks up against the competition plus a LIVE demo!

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
Qlemo"Batchelor", 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelly GarciaSenior Systems AdministratorAuthor Commented:
it set as -asjob
0
Qlemo"Batchelor", 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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I'll not be able to help you then, as I cannot perform tests against Azure.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.