Joseph
Joseph

Reputation: 570

Azure credentials have not been set up or have expired

I have scheduled a PowerShell script to execute a pipeline in Azure. Have generated the login script(ProfileContext.ctx) using Login-AzureRMAccount

Below is the code to schedule:

$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 

Above code works fine for 2 or 3 days but then I start getting below issue:

Your Azure credentials have not been set up or have expired, please run Login-AzureRMAccount to set up your Azure credentials. At D:\RunPipeline.ps1 Below are the version nos:

  1. PSVersion - 5.0.10586.117
  2. Azure - 4.2.1

Upvotes: 1

Views: 4445

Answers (3)

Niel
Niel

Reputation: 61

I came across with same issue today but after update Azure PowerShell Modules to the latest version, all good. You can do this while easily using Update-Module or just go ahead and use Install-Module AzureRm -Force.

Upvotes: 0

Shui shengbao
Shui shengbao

Reputation: 19195

Add Joseph's answer. Your answer only works on Azure AD account, Microsoft account does not support non-interactive login. According to your scenario, I suggest you could use Service Principal, it is more safer and not leak your account information. What is Service Principal?

When you have an app or script that needs to access resources, you can set up an identity for the app and authenticate the app with its own credentials. This identity is known as a service principal.

You could refer to refer to this link to create a new service principal and give Contributor role. You could use the following command to login your subscription.

$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

Upvotes: 1

Joseph
Joseph

Reputation: 570

I resolved it by using below work around: This would auto login for me and then I can schedule without a context file:

$accountName = "[email protected]"
$password = ConvertTo-SecureString "mypwd" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($accountName, $password)
Login-AzureRmAccount -Credential $credential
Get-AzureRmResource 

Upvotes: 4

Related Questions