Last active
April 16, 2019 05:16
-
-
Save emreozkangit/0535c07b6c55c624c6f8d40b6992d415 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Workflow Stop-Start-AzureVM | |
{ | |
Param | |
( | |
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] | |
[String] | |
$AzureSubscriptionId, | |
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] | |
[String] | |
$AzureVMList="All", | |
[Parameter(Mandatory=$true)][ValidateSet("Start","Stop")] | |
[String] | |
$Action | |
) | |
$credential = Get-AutomationPSCredential -Name 'automationname' | |
Login-AzureRmAccount -Credential $credential | |
Select-AzureRmSubscription -SubscriptionId $AzureSubscriptionId | |
if($AzureVMList -ne "All") | |
{ | |
$AzureVMs = $AzureVMList.Split(",") | |
[System.Collections.ArrayList]$AzureVMsToHandle = $AzureVMs | |
} | |
else | |
{ | |
$AzureVMs = (Get-AzureRmVM).Name | |
[System.Collections.ArrayList]$AzureVMsToHandle = $AzureVMs | |
} | |
foreach($AzureVM in $AzureVMsToHandle) | |
{ | |
if(!(Get-AzureRmVM | ? {$_.Name -eq $AzureVM})) | |
{ | |
throw " AzureVM : [$AzureVM] - Does not exist! - Check your inputs " | |
} | |
} | |
if($Action -eq "Stop") | |
{ | |
Write-Output "Stopping VMs"; | |
foreach -parallel ($AzureVM in $AzureVMsToHandle) | |
{ | |
Get-AzureRmVM | ? {$_.Name -eq $AzureVM} | Stop-AzureRmVM -Force | |
} | |
} | |
else | |
{ | |
Write-Output "Starting VMs"; | |
foreach -parallel ($AzureVM in $AzureVMsToHandle) | |
{ | |
Get-AzureRmVM | ? {$_.Name -eq $AzureVM} | Start-AzureRmVM | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment