Skip to content

Instantly share code, notes, and snippets.

@emreozkangit
Last active April 16, 2019 05:16
Show Gist options
  • Save emreozkangit/0535c07b6c55c624c6f8d40b6992d415 to your computer and use it in GitHub Desktop.
Save emreozkangit/0535c07b6c55c624c6f8d40b6992d415 to your computer and use it in GitHub Desktop.
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