To manage the Azure application gateway on-demand with PowerShell, you first need to install the Az PowerShell module on your computer. This happens by starting Windows PowerShell as an administrator and running the below command.
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
After you’ve installed the Az module, you are ready to run the script below. It will first connect to your Azure subscription, prompting you for credentials. It will then get the application gateway object and check its status. If the application gateway is running and you’ve specified that you want it stopped, the script will stop the gateway. And vice versa, if the application gateway is stopped and you’ve specified that you want it up and running again, the script will start the gateway.
param(
[Parameter(Mandatory)]
[String]$subscriptionId,
[Parameter(Mandatory)]
[String]$resourceGroupName,
[Parameter(Mandatory)]
[String]$appGatewayName,
[Parameter(Mandatory)]
[Boolean]$enabled
)
Connect-AzAccount -Subscription $subscriptionId
$appGateway = Get-AzApplicationGateway -Name $appGatewayName -ResourceGroupName $resourceGroupName
if ($enabled -eq $true -and $appGateway.OperationalState -eq "Stopped") {
Write-Host "Starting the application gateway."
Start-AzApplicationGateway -ApplicationGateway $appGateway
}
if ($enabled -eq $false -and $appGateway.OperationalState -eq "Running") {
Write-Host "Stopping the application gateway."
Stop-AzApplicationGateway -ApplicationGateway $appGateway
}
view rawset-application-gateway-state.ps1 hosted with ❤ by GitHub
To execute the script, you need to provide the Azure subscription ID, the resource group name, the app application gateway name, and either $true (to start) or $false (to stop) to change the application gateway state. So, for example, to stop the application gateway, you’d run the following command in Windows PowerShell.
. "<local script file directory path>.ps1" -subscriptionId "<your subscription ID>" -resourceGroupName "<your resource group name>" -appGatewayName "<your application gateway name>" -enabled $false