【问题标题】:"No subscription found in the context" error when invoking Azure Powershell cmdlets调用 Azure Powershell cmdlet 时出现“未在上下文中找到订阅”错误
【发布时间】:2020-03-11 09:06:10
【问题描述】:

我正在尝试编写一些简单的 Powershell 脚本来停止、启动和重新启动我们的 Azure Web 应用程序。我可以看到有名为Stop-AzWebAppStart-AzWebAppRestart-AzWebAppcmdlet 可以执行此操作。问题是它们都需要一个名为 DefaultProfile 的参数,类型为 Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer。我如何掌握这个?

我四处寻找解决方案,但似乎令人困惑,因为似乎有几种方法可以做到这一点,包括调用Connect-AzAccount,但这需要多个参数(我不确定我需要提供哪些参数)。

这是我从 TFS 构建中调用 Get-AzSubscription 时遇到的错误。

这是我从 TFS 构建中调用 Stop-AzWebApp 时遇到的错误。

No subscription found in the context Please ensure that the credentials you provided are authorized to access the Azure subscription then run Connect-AzAccount to login

这是我正在使用的 PS 脚本。

$ResourceGroupName = "MyResourceGroup"
$Name = "MyWebAppName"

"Stopping web application " + $Name

"ResourceGroupName: " + $ResourceGroupName
"Name: " + $Name

$SubscriptionId = "xxxx-xxxx-xxxx-xxxx"
"SubscriptionId is: " + $SubscriptionId

$Subscription = Get-AzSubscription -SubscriptionId $SubscriptionId
"Subscription : " + $Subscription

Stop-AzWebApp -ResourceGroupName $ResourceGroupName -Name $Name

启动、停止和重新启动 Azure Web 应用程序的最简单方法是什么?

【问题讨论】:

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


    【解决方案1】:

    从报错中,你需要运行Connect-AzAccount来登录azure powershell,但是在TFS中,你不能使用交互方式登录,所以你的选择是使用服务主体登录,这是一个非- 互动方式。

    请按照以下步骤操作。

    1.Register an AD App in azure ad,然后是get values for signing increate a new application secret

    2.在门户中导航到您的订阅 -> Access control (IAM) -> Add role assignment -> seacrh 获取 AD 应用程序的名称并将其添加为角色,例如Owner/Contributor,见link

    3.在您的脚本中,使用以下命令登录,您可以在步骤 1 中获取值,然后运行命令,例如Stop-AzWebApp,它会正常工作的。

    $azureAplicationId ="<Application-ID>"
    $azureTenantId= "<Tenant-ID>"
    $azurePassword = ConvertTo-SecureString "<Client-secret>" -AsPlainText -Force
    $psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
    Connect-AzAccount -Credential $psCred -TenantId $azureTenantId -ServicePrincipal 
    Set-AzContext -Subscription "<Subscription-id>"
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 2021-11-23
    • 2020-07-15
    • 1970-01-01
    • 2016-11-23
    • 2011-03-20
    • 1970-01-01
    相关资源
    最近更新 更多