Skip to content

Instantly share code, notes, and snippets.

@nshores
Last active July 7, 2020 18:33
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 nshores/0eba281bcfb8d2a4a8329a60129a7531 to your computer and use it in GitHub Desktop.
Save nshores/0eba281bcfb8d2a4a8329a60129a7531 to your computer and use it in GitHub Desktop.
reboot_az_server_runbook
$connectionname = "AzureRunAsConnection"
try
{
$servicePrincipalConnection=Get-Automationconnection -Name $connectionname
Login-AzAccount -servicePrincipal -TenantId $servicePrincipalConnection.TenantId -applicationId $servicePrincipalConnection.applicationId -certificateThumbprint $servicePrincipalConnection.certificatethumbprint
}
catch{
if (!$serviceprincipalconnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
#http://techgenix.com/azure-automation-runbooks/
#Get Scale Sets
$ss = get-azvmss
#Get VM In Scale Set (Modify to loop if moree than oen scale set)
$vms = Get-AzVmssVM -ResourceGroupName $ss.ResourceGroupName -VMScaleSetName $ss.name| Where {$_.Tags.keys -contains "Description" -and $_.Tags.Values -contains "Anton Desktop Pool"}
#https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-manage-powershell
ForEach ($vm in $vms)
{
$VMStatus = Get-AzVmssVM -ResourceGroupName $vm.ResourceGroupName -VMScaleSetName $ss.Name -InstanceId $vm.InstanceId -InstanceView
$VMPS = $VMStatus.Statuses[1].DisplayStatus
If ($VMPS-eq "VM Running")
{
Restart-AzVmss -ResourceGroupName $vm.resourcegroupname -VMScaleSetName $ss.name -InstanceId $vm.instanceid -WhatIf
"$VMName is restarting"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment