首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Azure Runbook按计划重新启动Azure Web应用程序

如何使用Azure Runbook按计划重新启动Azure Web应用程序
EN

Stack Overflow用户
提问于 2018-06-09 05:57:35
回答 1查看 1K关注 0票数 0

我们有一个Azure Web App,我们想要按计划设置自动重启。如果我想使用Runbook来做这件事,我如何添加一个或多个应用程序来按不同的计划自动重启?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-09 07:13:49

有许多不同的方法可以做到这一点,其中一些将取决于您拥有的Azure-Runbook模块版本。

要获得连接,您可以使用cmdlet:Get-AutomationConnection

  • Then添加经过身份验证的帐户,您可以使用:Add-AzureRmAccount

  • If如果您有多个订阅,您将需要选择要使用的订阅:Select-AzureRmSubscription

  • Finally,使用Restart-AzureRmWebApp重新启动

如果您设置了一个$result= Restart-AzureRmWebApp,如果$result为空,则它不起作用,否则您将看到正在运行的webapp的状态。例如:如果工作成功,则返回$result.State = "Running"

要在 Schedule 上执行此操作,请转到Runbook > Schedules > Add Schedule。

添加输入参数,并选择/创建循环计划。单击Ok,您就完成了!

*如果对webAppName使用参数,则可以重用runbook,只需添加具有不同输入参数的不同计划

示例代码。

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50768891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档