如何使用 Azure Runbooks 按计划重启 Azure Web 应用
How to use Azure Runbooks to restart an Azure Web App on a schedule
我们有一个 Azure Web 应用程序,我们希望按计划设置自动重启。如果我想为此使用 Runbook,我该如何添加一个或多个应用程序以在不同的时间表上自动重启?
有多种不同的方法可以执行此操作,其中一些将取决于您拥有的 Azure-Runbook 模块版本。
- 您需要将用户设置为 'RunAsConnection' 用户
- 要获得连接,您可以使用 cmdlet:Get-AutomationConnection
- 然后要添加经过身份验证的帐户,您可以使用:Add-AzureRmAccount
- 如果您有多个订阅,则需要 select
订阅使用:Select-AzureRmSubscription
- 最后,使用Restart-AzureRmWebApp重启网络应用。
如果你设置了一个$result= Restart-AzureRmWebApp
,如果$result
为null,那么它不起作用,否则你会看到运行 webapp的状态。
例如。 $result.State = "Running"
如果成功的话。
To do this on a schedule: go to your Runbook > Schedules > Add
Schedule.
Add the input parameters, and select/create a recurring schedule.
Click Ok and you're done!
*** If you use a parameter for your webAppName, then you can reuse the
runbook, and just add different schedules with different input params
示例代码。
try
{
$servicePrincipalConnection= Get-AutomationConnection -Name "AzureRunAsConnection"
# Logging in to Azure
$account = Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
Select-AzureRmSubscription -SubscriptionName "Azure subscription name"
$result = Restart-AzureRmWebApp `
-ResourceGroupName "Resource Name"
-Name "Name of webapp you want to restart"
if($result)
{
$state = $result.State
Write-Output ("Web app restarted and is now $state") #State should be Running at this point
}
else
{
Write-Output ("Web app did NOT restart")
}
}
catch
{
Write-Output ("Web app did NOT restart")
throw $_.Exception
}
Because Az PowerShell modules now have all the capabilities of AzureRM
PowerShell modules and more, we'll retire AzureRM PowerShell modules
on 29 February 2024
这是 Az PowerShell 模块的更新示例。
filter timestamp {"[$(Get-Date -Format G)]: $_"}
Write-Output "Script started." | timestamp
$resourceGroupName = "your group"
$appServiceName = "your app service"
$subscriptionName = "your subscription"
try
{
$conn = Get-AutomationConnection -Name "AzureRunAsConnection"
Connect-AzAccount -ServicePrincipal -Tenant $conn.TenantID -ApplicationId $conn.ApplicationID -CertificateThumbprint $conn.CertificateThumbprint | out-null
# Select-AzureRmSubscription -SubscriptionName subscriptionName
$result = Restart-AzWebApp -ResourceGroupName $resourceGroupName -Name $appServiceName
if($result)
{
$state = $result.State
Write-Output ("Web app restarted and is now $state") #State should be Running at this point
}
else
{
Write-Output ("Web app did NOT restart")
}
}
catch
{
Write-Output ("Web app did NOT restart")
throw $_.Exception
}
我们有一个 Azure Web 应用程序,我们希望按计划设置自动重启。如果我想为此使用 Runbook,我该如何添加一个或多个应用程序以在不同的时间表上自动重启?
有多种不同的方法可以执行此操作,其中一些将取决于您拥有的 Azure-Runbook 模块版本。
- 您需要将用户设置为 'RunAsConnection' 用户
- 要获得连接,您可以使用 cmdlet:Get-AutomationConnection
- 然后要添加经过身份验证的帐户,您可以使用:Add-AzureRmAccount
- 如果您有多个订阅,则需要 select 订阅使用:Select-AzureRmSubscription
- 最后,使用Restart-AzureRmWebApp重启网络应用。
如果你设置了一个$result= Restart-AzureRmWebApp
,如果$result
为null,那么它不起作用,否则你会看到运行 webapp的状态。
例如。 $result.State = "Running"
如果成功的话。
To do this on a schedule: go to your Runbook > Schedules > Add Schedule.
Add the input parameters, and select/create a recurring schedule. Click Ok and you're done!
*** If you use a parameter for your webAppName, then you can reuse the runbook, and just add different schedules with different input params
示例代码。
try
{
$servicePrincipalConnection= Get-AutomationConnection -Name "AzureRunAsConnection"
# Logging in to Azure
$account = Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
Select-AzureRmSubscription -SubscriptionName "Azure subscription name"
$result = Restart-AzureRmWebApp `
-ResourceGroupName "Resource Name"
-Name "Name of webapp you want to restart"
if($result)
{
$state = $result.State
Write-Output ("Web app restarted and is now $state") #State should be Running at this point
}
else
{
Write-Output ("Web app did NOT restart")
}
}
catch
{
Write-Output ("Web app did NOT restart")
throw $_.Exception
}
Because Az PowerShell modules now have all the capabilities of AzureRM PowerShell modules and more, we'll retire AzureRM PowerShell modules on 29 February 2024
这是 Az PowerShell 模块的更新示例。
filter timestamp {"[$(Get-Date -Format G)]: $_"}
Write-Output "Script started." | timestamp
$resourceGroupName = "your group"
$appServiceName = "your app service"
$subscriptionName = "your subscription"
try
{
$conn = Get-AutomationConnection -Name "AzureRunAsConnection"
Connect-AzAccount -ServicePrincipal -Tenant $conn.TenantID -ApplicationId $conn.ApplicationID -CertificateThumbprint $conn.CertificateThumbprint | out-null
# Select-AzureRmSubscription -SubscriptionName subscriptionName
$result = Restart-AzWebApp -ResourceGroupName $resourceGroupName -Name $appServiceName
if($result)
{
$state = $result.State
Write-Output ("Web app restarted and is now $state") #State should be Running at this point
}
else
{
Write-Output ("Web app did NOT restart")
}
}
catch
{
Write-Output ("Web app did NOT restart")
throw $_.Exception
}