【问题标题】:Azure credentials have not been set up or have expiredAzure 凭据尚未设置或已过期
【发布时间】:2017-09-07 06:51:17
【问题描述】:

我已经安排了一个 PowerShell 脚本来在 Azure 中执行管道。 已使用 Login-AzureRMAccount 生成了登录脚本(ProfileContext.ctx)

下面是安排的代码:

$path = "D:\ProfileContext.ctx"
Import-AzureRmContext -Path $path
$dfn = "salesprod"
$rgn = "sale-dw"
$df=Get-AzureRmDataFactory -ResourceGroupName $rgn -Name $dfn

$ISTstartdate = get-date

#Set Pipeline active period
$UTCstartdate = $ISTstartdate.touniversaltime().addminutes(5)
$UTCenddt = $UTCstartdate.AddHours(5)

$pipelinename = "SalesPipeline"

Set-AzureRmDataFactoryPipelineActivePeriod -ResourceGroupName $rgn -PipelineName $pipelinename -DataFactoryName $dfn -StartDateTime $UTCstartdate -EndDateTime $UTCenddt -Force 

上面的代码可以正常工作 2 或 3 天,但随后我开始遇到以下问题:

您的 Azure 凭据尚未设置或已过期,请运行 Login-AzureRMAccount 以设置您的 Azure 凭据。 在 D:\RunPipeline.ps1 以下是版本号:

  1. PSVersion - 5.0.10586.117
  2. Azure - 4.2.1

【问题讨论】:

  • 您的 Azure Power Shell 版本是什么。在旧版本的 PowerShell 上似乎是已知的 issue
  • 使用Get-Module -ListAvailable -Name Azure -Refresh 列出版本。
  • 我在使用你的命令后得到以下信息:4.2.1 Azure
  • PS版本:PSVersion 5.0.10586.117
  • 问题在 4.2.0 解决。最新版本是 4.3.1。我为您的场景使用该版本,它适用于我。如果可能,您可以安装最新版本。请从link下载。

标签: powershell azure


【解决方案1】:

我通过使用以下解决方法解决了这个问题:这将为我自动登录,然后我可以在没有上下文文件的情况下进行安排:

$accountName = "pqr@xyz.com"
$password = ConvertTo-SecureString "mypwd" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($accountName, $password)
Login-AzureRmAccount -Credential $credential
Get-AzureRmResource 

【讨论】:

  • 它仅适用于 Azure AD 帐户。 Microsoft 帐户不支持此方法。
  • 是的,我使用 Azure AD 帐户登录。
  • 根据您的需要,我更喜欢使用服务主体,这样更安全,不会泄露我的帐户和密码。我将其添加为答案。希望对更多人有所帮助。
【解决方案2】:

添加约瑟夫的答案。您的答案仅适用于 Azure AD 帐户,Microsoft 帐户不支持非交互式登录。根据您的情况,我建议您可以使用 Service Principal,这样更安全,不会泄露您的帐户信息。什么是Service Principal

当您有需要访问资源的应用或脚本时,您可以 为应用程序设置身份并使用自己的身份验证应用程序 证书。此身份称为服务主体。

您可以参考此link 来创建新的服务主体并赋予Contributor 角色。您可以使用以下命令登录您的订阅。

$subscriptionId=""
$tenantid=""
$clientid=""
$password=""
$userPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $clientid, $userPassword
Add-AzureRmAccount -TenantId $tenantid -ServicePrincipal -SubscriptionId $subscriptionId -Credential $userCredential

【讨论】:

    【解决方案3】:

    我今天遇到了同样的问题,但是在将 Azure PowerShell 模块更新到最新版本之后,一切都很好。您可以使用 Update-Module 轻松完成此操作,或者直接使用 Install-Module AzureRm -Force。

    【讨论】:

      猜你喜欢
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2016-05-19
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多