使用 AccessToken 验证 powershell 脚本

Authenticate powershell script using AccessToken

我有一个调用 PowerShell 脚本的发布管道,它有 'Select-AzureSubscription' 语句(在一个循环中,我必须将不同的订阅分配为当前并执行一些过程)。当它运行时,我收到以下错误,因为我没有添加 'Add-AzureAccount'.

Select-Azure订阅:订阅名称不存在。

有什么方法可以使用管道访问令牌来验证或执行 Add-AzureAccount。

代码:-

Select-AzureSubscription -SubscriptionName $Subscription -Current 
CloudServices = Get-AzureService | select ServiceName 

您可以尝试以下步骤(我无法为您测试,因为我没有未启用 MFA 的用户帐户)。

1.In devops,导航到 Project Settings -> Service connections -> New service connection -> Azure Classic.

然后输入信息,你可以在azure portal -> Subscriptions中获取订阅名称和id。

注意: UsernamePassword 需要是未启用 MFA 的用户帐户。

2.After 创建服务连接,在管道中,使用 Task version 3.*、select 创建 Azure powershell 任务,Azure Connection Type 使用 Azure Classic,select Azure Classic Subscription 使用在步骤 1 中创建的服务连接。

然后使用脚本:

Select-AzureSubscription -SubscriptionName <subscription-name> -Current 
CloudServices = Get-AzureService | select ServiceName