Created
September 13, 2023 13:10
-
-
Save mildronize/78352ba7ce6b3e5c06ebbb9389d853fe to your computer and use it in GitHub Desktop.
Restart-AppServiceInstances.ps1
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
Param( | |
[Parameter(Mandatory=$true)] | |
# ID for the Azure subscription containing the App Service | |
[string]$SubscriptionId, | |
[Parameter(Mandatory=$true)] | |
# Name of the App Service on which the restart should be performed | |
[string]$AppServiceName, | |
[Parameter(Mandatory=$false)] | |
# Seconds between each instance restart, defaults to 30 | |
[int]$SecondsBetweenRestarts = 30 | |
) | |
$VerbosePreference = "Continue" | |
$AutomationConnection = Get-AutomationConnection -Name "AzureRunAsConnection" | |
$ConnectionSplat = @{ | |
ServicePrincipal = $true | |
Tenant = $AutomationConnection.TenantID | |
ApplicationId = $AutomationConnection.ApplicationID | |
CertificateThumbprint = $AutomationConnection.CertificateThumbprint | |
} | |
Connect-AzAccount @ConnectionSplat | |
Set-AzContext -Subscription $SubscriptionId | Out-Null | |
$WebApp = Get-AzWebApp -Name $AppServiceName | |
$ResourceGroup = $WebApp.ResourceGroup | |
# This gives you a list of instances for your App Service | |
$InstancesSplat = @{ | |
ResourceGroupName = $ResourceGroup | |
ResourceType = "Microsoft.Web/sites/instances" | |
ResourceName = $AppServiceName | |
ApiVersion = "2018-11-01" | |
} | |
$ApplicationInstances = Get-AzResource @InstancesSplat | |
$InstanceCounter = 0 | |
$FailedRestarts = $false | |
foreach ($Instance in $ApplicationInstances) { | |
$InstanceCounter++ | |
Write-Verbose -Message "Now on instance $InstanceCounter/$($ApplicationInstances.Count)" | |
# This gives you list of processes running on a particular instance, filtered to only IIS processes | |
$ProcessListSplat = @{ | |
ResourceGroupName = $ResourceGroup | |
ResourceType = "Microsoft.Web/sites/instances/processes" | |
ResourceName = "$AppServiceName/$($Instance.Name)" | |
ApiVersion = "2018-11-01" | |
} | |
$ProcessList = Get-AzResource @ProcessListSplat | Where-Object { $_.Properties.Name -eq "w3wp" } | |
foreach ($Process in $ProcessList) { | |
$ProcessWithPropertiesSplat = @{ | |
ResourceGroupName = $ResourceGroup | |
ResourceType = "Microsoft.Web/sites/instances/processes" | |
ResourceName = "$AppServiceName/$($Instance.Name)/$($Process.Properties.id)" | |
ApiVersion = "2018-11-01" | |
} | |
$ProcessWithProperties = Get-AzResource @ProcessWithPropertiesSplat | |
# 'is_scm_site' is a property which is set on the Kudu worker process, which you don't want to restart | |
if ($ProcessWithProperties.Properties.is_scm_site -ne $true) { | |
Write-Verbose -Message "Instance ID $($Instance.Name) has computer name $($ProcessWithProperties.Properties.environment_variables.COMPUTERNAME)" | |
Write-Verbose -Message "Going to stop process with PID $($ProcessWithProperties.Properties.Id)" | |
# Remove-AzResource stops the IIS process, which will then automatically restart | |
$Result = Remove-AzResource -ResourceId $ProcessWithProperties.ResourceId -ApiVersion 2018-11-01 -Force | |
if ($Result -eq $true) { | |
Write-Verbose -Message "Process $($ProcessWithProperties.Properties.Id) stopped" | |
} | |
else { | |
Write-Error -Message "Problem stopping process $($ProcessWithProperties.Properties.Id) on instance $($Instance.Name)." | |
$FailedRestarts = $true | |
} | |
if ($InstanceCounter -lt $ApplicationInstances.Count) { | |
Write-Verbose -Message "Sleeping for $SecondsBetweenRestarts seconds before next reboot..." | |
Start-Sleep -s $SecondsBetweenRestarts | |
} | |
} | |
} | |
} | |
if ($FailedRestarts) { | |
throw "At least one restart failed. Please review output for details." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment