【问题标题】:How to restart an Azure appservice from Azure Powershell如何从 Azure Powershell 重新启动 Azure 应用服务
【发布时间】:2017-11-15 02:23:47
【问题描述】:

如何在 Azure 的 ARM 订阅中的自动化帐户中的 Runbook 中从 Azure 的 Powershell 重新启动 AppService?

我认为方法是:

Restart-AzureWebsite -Name "your-appservice-name"

但是得到:

Restart-AzureWebsite : No default subscription has been designated.
Use select-AzureSubscription -Default #<subscriptionName> to set the default subscription.

Azure PowerShell 中没有可用的 Restart-AzureRmWebApp。

以下所有组合都会导致一堆其他错误消息:

$Cred = Get-AutomationPSCredential -Name 'your-credentials-name'
Add-AzureAccount -Credential $Cred
Add-AzureRMAccount -Credential $Cred
Get-AzureSubscription –SubscriptionName 'your-subscription-name' | Select-AzureSubscription -Default
Restart-AzureWebsite -Name "your-appservice-name"

【问题讨论】:

  • Restart-AzureWebsite 这是一个经典的 cmdlet。您的 Azure Power Shell 的版本是什么? Get-Module -ListAvailable -Name Azure -Refresh
  • 该命令甚至无法在 Azure Powershell 中运行;它返回:Runbook 作业尝试了 3 次,但每次都失败。 Runbook 作业失败的常见原因可在此处找到:docs.microsoft.com/en-us/azure/automation/…
  • 在runbook中,需要导入模块AzureRM.Websites,docs.microsoft.com/en-us/azure/automation/…
  • Import-Module AzureRM.Websites 导致:Import-Module :未加载指定的模块“AzureRM.Websites”,因为在任何模块目录中都找不到有效的模块文件。
  • 您好,您只需将模块导入您的自动化帐户,然后您就可以使用 cmdlet。无需在 Runbook 中使用 Import-Module AzureRm.Websites

标签: powershell azure azure-web-app-service


【解决方案1】:

Azure PowerShell 中没有可用的 Restart-AzureRmWebApp。

正如Walter - MSFT提到我们可以导入AzureRM.Websites,在此之前我们需要更新AzureRM.Profile4.0,更多细节可以参考截图.

在此之前,我们可以在本地创建 Azure AD 服务主体。 如何创建服务主体我们可以参考这个document

 Login-AzureRmAccount
 $sp = New-AzureRmADServicePrincipal -DisplayName exampleapp -Password "password"
 Sleep 20
 New-AzureRmRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $sp.ApplicationId

在 Runbook 中运行 Restart-AzureRmWebApp 命令。

$azureAplicationId ="Application Id"
$azureTenantId= "tenant Id"
$azurePassword = ConvertTo-SecureString "password" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Add-AzureRmAccount -Credential $psCred -TenantId $azureTenantId  -ServicePrincipal 
Restart-AzureRmWebApp -ResourceGroupName "ResourceGroup" -Name "WebApp Name" 

【讨论】:

  • 这些命令(Login-AzureRmAccount 和 Restart-AzureRmWebApp)在 Runbook 中均不可用。
  • 我已经更新了如何为自动化帐户添加模块的答案。
【解决方案2】:

此 Powershell 脚本在 Azure 自动化运行手册中运行:

Invoke-AzureRmResourceAction -ResourceGroupName "<your-resource-group-name>" -ResourceName "<your-resource-name>" -ResourceType 'Microsoft.Web/sites' -Action 'Restart' -Force

编辑

但是下一个脚本可能会更好;它依赖于@Tom Sun 上面的回答,即

  1. 升级模块 - 转到自动化帐户/模块/更新 Azure 模块。
  2. 导入 AzureRm.Websites 模块 - 转到自动化帐户/模块/浏览库。
  3. 在自动化帐户/凭据下创建。

    $Cred = Get-AutomationPSCredential -Name '<your-credentials>'
    Add-AzureRMAccount -Credential $Cred
    
    Get-AzureRmSubscription –SubscriptionName '<your-subscription-name>' | Select-AzureRmSubscription
    
    Restart-AzureRmWebApp -ResourceGroupName "office" -Name "<your-appservice-name>"
    

【讨论】:

    猜你喜欢
    • 2020-06-09
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多