Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Last active June 14, 2020 00:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save darrenjrobinson/960fdbd63d17569ada6e to your computer and use it in GitHub Desktop.
Save darrenjrobinson/960fdbd63d17569ada6e to your computer and use it in GitHub Desktop.
ParallelRMRGVirtualMachines
param(
[string]$power,
[string]$azureResourceGroup
)
if (!$power){Write-host "No powerstate specified. Use -Power start|stop"}
if (!$azureResourceGroup){Write-host "No Azure Resource Group specified. Use -azureResourceGroup 'ResourceGroupName'"}
# see if we already have a session. If we don't don't re-authN
if (!$AzureRMAccount.Context.Tenant) {
$AzureRMAccount = Add-AzureRmAccount
}
$SubscriptionName = Get-AzureRmSubscription | sort SubscriptionName | Select SubscriptionName
$TenantId = $AzureRMAccount.Context.Tenant.TenantId
Select-AzureRmSubscription -TenantId $TenantId
write-host "Enumerating VM's from AzureRM in Resource Group '"$azureResourceGroup "'"
$vms = Get-AzureRMVM -ResourceGroupName $azureResourceGroup
$vmrunninglist = @()
$vmstoppedlist = @()
Foreach($vm in $vms)
{
$vmstatus = Get-AzureRMVM -ResourceGroupName $azureResourceGroup -name $vm.name -Status
$PowerState = (get-culture).TextInfo.ToTitleCase(($vmstatus.statuses)[1].code.split("/")[1])
write-host "VM: '"$vm.Name"' is" $PowerState
if ($Powerstate -eq 'Running')
{
$vmrunninglist = $vmrunninglist + $vm.name
}
if ($Powerstate -eq 'Deallocated')
{
$vmstoppedlist = $vmstoppedlist + $vm.name
}
}
if ($power -eq 'start') {
write-host "Starting VM's "$vmstoppedlist " in Resource Group "$azureResourceGroup
$vmstoppedlist | Invoke-Parallel -ImportVariables -NoCloseOnTimeout -ScriptBlock {
Start-AzureRMVM -ResourceGroupName $azureResourceGroup -Name $_ -Verbose }
}
if ($power -eq 'stop') {
write-host "Stopping VM's "$vmrunninglist " in Resource Group "$azureResourceGroup
$vmrunninglist | Invoke-Parallel -ImportVariables -NoCloseOnTimeout -ScriptBlock {
Stop-AzureRMVM -ResourceGroupName $azureResourceGroup -Name $_ -Verbose -Force }
}
@gulabpasha
Copy link

Hi,

I hope I'm writing on to a right thread, I tried the above script to Start / Stop Multiple VM Simultaneously but wasn't succeeded.

I have multiple Resource Groups (20+) and each Resource Group has VMs running, I was looking PS script where I can use to turn those all VMs on & off in single go (Simultaneously).

I have a PS script running where it start & stops one after one for all Resource Groups.

I'm looking for a solution where I can turn on & off all VMs in one Subscription or Tenant simultaneously.

Thanks,
Gulab Pasha

@pkvk14
Copy link

pkvk14 commented Jun 13, 2020

Hi,
There is any way to pass VM name in csv file and stop and start vm.
Example :

#Import VMs listed within CSV
$VMs = Import-csv "D:\Az_Powershell_13_06_20\VMstart-05.Csv"

$VMs | foreach-object {

Start-azureRMVM -Name $VM.Name -force

}

@darrenjrobinson
Copy link
Author

Absolutely.
Something like this where;

  • myVMs.csv contains a column named vmname with your VM names

$vm_list = Import-Csv -Path .\myVMs.csv

foreach ($vm in $vm_list) {
Start-azureRMVM -Name $vm.vmname -force
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment