Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.