简体   繁体   English

调用 Azure Powershell cmdlet 时出现“未在上下文中找到订阅”错误

[英]"No subscription found in the context" error when invoking Azure Powershell cmdlets

I am trying to write some simple Powershell scripts to stop, start and restart our Azure web app.我正在尝试编写一些简单的Powershell脚本来停止、启动和重新启动我们的Azure Web 应用程序。 I can see that there are cmdlets called Stop-AzWebApp , Start-AzWebApp and Restart-AzWebApp that do this.我可以看到有所谓的cmdlet Stop-AzWebAppStart-AzWebAppRestart-AzWebApp是做到这一点。 The problem is they all require a parameter called DefaultProfile of type Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer .问题是它们都需要一个名为DefaultProfile的参数,类型为Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer How do I get hold of this?我如何掌握这个?

I have looked around for solutions but it seems confusing as there seem to be several ways of doing this including invoking Connect-AzAccount , but this requires multiple parameters (and I'm not sure which ones I need to supply).我四处寻找解决方案,但似乎很混乱,因为似乎有几种方法可以做到这一点,包括调用Connect-AzAccount ,但这需要多个参数(我不确定我需要提供哪些参数)。

Here is the error I get when I invoke Get-AzSubscription from our TFS build.这是我从 TFS 构建调用Get-AzSubscription时得到的错误。

在此处输入图片说明

Here is the error I get when I invoke Stop-AzWebApp from our TFS build.这是我从 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

Here's the PS script I'm using.这是我正在使用的 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

What's the simplest way to start, stop and restart an Azure web app?启动、停止和重新启动 Azure Web 应用程序的最简单方法是什么?

From the error, you need to run Connect-AzAccount to login to azure powershell, but in TFS, you could not use the interactive way to login, so your option is to use the service principal to login, it is a non-interactive way.从报错来看,你需要运行Connect-AzAccount来登录azure powershell,但是在TFS中,你不能使用交互方式登录,所以你的选择是使用服务主体登录,它是一种非交互方式.

Please follow the steps below.请按照以下步骤操作。

1. Register an AD App in azure ad , then get values for signing in and create a new application secret . 1. 在 azure ad 中注册一个 AD App ,然后获取登录值创建一个新的应用程序密钥

2.Navigate to your subscription in the portal -> Access control (IAM) -> Add role assignment -> seacrh for the name of the AD App and add it as a role eg Owner/Contributor , see this link . 2.在门户中导航到您的订阅 -> Access control (IAM) -> Add role assignment -> 搜索 AD 应用程序的名称并将其添加为角色,例如Owner/Contributor ,请参阅此链接 在此处输入图片说明

3.In your script, use the command below to login, you can get the vaules in step 1, then run the command eg Stop-AzWebApp , it will work fine. 3.在你的脚本中,使用下面的命令登录,你可以在步骤1中获取vaules,然后运行命令例如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>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM