Skip to content

Instantly share code, notes, and snippets.

@haray-isao
Last active April 6, 2018 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haray-isao/eebd4a64e3c2101611bc43cf0f47b93f to your computer and use it in GitHub Desktop.
Save haray-isao/eebd4a64e3c2101611bc43cf0f47b93f to your computer and use it in GitHub Desktop.
workflow Stop-Start-AzureVM2
{
Param
(
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
[String]
$credentialName,
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
[String]
$ResourceGroup,
[Parameter(Mandatory=$true)][ValidateSet("Start","Stop")]
[String]
$Action
)
$credential = Get-AutomationPSCredential -Name $credentialName
$tennantId = Get-AutomationVariable -Name 'tennantId'
Login-AzureRmAccount -Credential $credential -Tenantid $tennantId -ServicePrincipal
$AzureVMs = Get-AzureRmVM -ResourceGroup $ResourceGroup
if($Action -eq "Stop")
{
Write-Output "Stopping VMs";
foreach -parallel ($AzureVM in $AzureVMs)
{
$AzureVM | Stop-AzureRmVM -Force
}
}
else
{
Write-Output "Starting VMs";
foreach -parallel ($AzureVM in $AzureVMs)
{
$AzureVM | Start-AzureRmVM
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment