Last active
July 7, 2020 18:33
-
-
Save nshores/0eba281bcfb8d2a4a8329a60129a7531 to your computer and use it in GitHub Desktop.
reboot_az_server_runbook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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