Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
$connectionname = "AzureRunAsConnection"
$servicePrincipalConnection=Get-Automationconnection -Name $connectionname
Login-AzAccount -servicePrincipal -TenantId $servicePrincipalConnection.TenantId -applicationId $servicePrincipalConnection.applicationId -certificateThumbprint $servicePrincipalConnection.certificatethumbprint
if (!$serviceprincipalconnection)
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
#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 $| Where {$_.Tags.keys -contains "Description" -and $_.Tags.Values -contains "Anton Desktop Pool"}
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 $ -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.