Azure does not keep session/login in a background job, version 1.0.1
Hello,
I've noticed that Login-AzureRmAccount does not set credentials that are carried over to a background job. In the old version, whenever I did a Add-AzureAccount and provided the credentials and got logged in to Azure, the credentials were passed on to the background job as well. This is no longer the case. Now there is an exception that tells me to "Run Login-AzureRmAccount" even though I am logged in in the console session that executes the command.
Is this a known issue, or is this intended?
I'm also facing this issue. Please fix this.
Yes. It would be great if it worked like before. This really makes automation VMs a bit of a hassle (background jobs are good for creating several VMs at the same time).
I have been affected by this as well.
I tried using Save-AzureRmProfile and Select-AzureRmProfile, but still received the error.
Here is the (abbreviated) contents of my deployment PS1 script:
$deploy = {
Select-AzureRmProfile -Path "$PSScriptRoot\.azureprofile"
# deployment steps
}
Save-AzureRmProfile -Path "$PSScriptRoot\.azureprofile"
Start-Job -ScriptBlock $deploy
The script runs successfully if I don't execute $deploy as a job. But using Start-Job, I get the exception "Run Login-AzureRmAccount to login" - even though i'm doing Select-AzureRmProfile in the script block.
The same problem as me then. Doesn't seem to be fixed yet either with the new versions of the Azure Module.
Same here. Any plan to fix this?
@KarlGW @YidingZhou @iamshital @matthewbcline
So, there are two unrelated issues here. First, the profile is no longer automatically persisted to disk, so it cannot be read by background jobs unless you explicitly save it. We plan to implement auto-saving of the profile in a release in the near term, but meanwhile, you should be able to get your background jobs to work perfectly well using Save-AzureRMProfile and Select-AzureRMProfile, for example:
$deploy = {
Select-AzureRmProfile -Path .\profile1.json
Get-AzureRMResourceGroup
}
Save-AzureRmProfile -Path .\profile1.json
$job = Start-Job -ScriptBlock $deploy
Wait-Job $job
Receive-Job $job
However, you'll notice I'm not using any variables in the script block. Since the scriptblock is actually run in a separate appdomain in a child job, the variables that you define in your parent session are not transferred. For example, the following script will fail because $path is null:
$path = ".\profile.json"
$deploy = {
Select-AzureRmProfile -Path $path
Get-AzureRMResourceGroup
}
Save-AzureRmProfile -Path $path
$job = Start-Job -ScriptBlock $deploy
Wait-Job $job
Receive-Job $job
will fail with:
Cannot bind argument to parameter 'Path' because it is null.
+ CategoryInfo : InvalidData: (:) [Select-AzureRmProfile], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Commands.Profile.SelectAz
ureRMProfileCommand
+ PSComputerName : localhost
Run Login-AzureRmAccount to login.
+ CategoryInfo : InvalidOperation: (:) [Get-AzureRmResourceGroup], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.Resources.GetAzureResourceGroupCommand
+ PSComputerName : localhost
You can fix this by adding the variable as a parameter and using an argument list in your call to Start-Job:
$path = ".\profile.json"
$deploy = {
Param($path)
Select-AzureRmProfile -Path $path
Get-AzureRMResourceGroup
}
Save-AzureRmProfile -Path $path
$job = Start-Job -ScriptBlock $deploy -ArgumentList $path
Wait-Job $job
Receive-Job $job
If you alter your script above to not use variables, you should be fine. You can see a more detailed discussion of transmitting variables to jobs here:http://stackoverflow.com/questions/10196489/variables-in-start-job
Thank you for your reply and information. I will look into this and try out to pass the path to the AzureRm-profile as a parameter to my functions that runs as background jobs. I'm just have to find a nice way to send it in first.
@markcowl It worked like a charm! Thank you so much for this advice. Back in the day before I gave up I found nothing that pointed to this, or rather maybe I missed it. But your ELI5 on it certainly made me able to send my "credentials" to the background process job. Thanks!
@KarlGW Glad that helped. The eventual solution is to add profile persistence and to make the profile fully serializable so you can pass it as a variable to a script. This is coming in a near term release.
Looks like version 3.0.0 of the AzureRM.profile module removes Select-AzureRmProfile and Save-AzureRmProfile. I had these working with background jobs but now it's broken with the latest modules. Struggling to find a workaround. Apparently the new Import-AzureRmContext is not equivalent to the old Select-AzureRmProfile
@SiloReed found any Solution to background Jobs?
Yes, the workaround I found was to sign into AzureRM with a service principal with certificate authentication. Then the Import-AzureRmContext call works correctly in the background job, most likely because there is no user password when using certificate based authentication. My guess is that Save-AzureRmContext doesn't save the user's password, and/or Import-AzureRmContext doesn't use the password correctly.
any advice on how to run parallel tasks in Azure (like creaton / stopping / starting VM's is highly appreciated. Like others mentioned, background tasks now fail with message "Run-Login-AzureRMAccount to login". I must login with 2factor authentication. I cannot work around this.
Yeah, running into this same problem. Any workarounds greatly appreciated.
@smithkl42 Use:
$contextPath = <path-to-file>
Save-AzureRmContext -Path $contextPath.
Pass the $contextPath in the argumentlist of the job and use
Import-AzureRmContext -Path $contextPath
inside the job.
This works as a workaround for the moment.
any solution to this workaround?
With Azure Powershell 4.4.1 onwards , Microsoft has Enable-AzureRmContextAutosave
Refer:: https://docs.microsoft.com/en-us/powershell/azure/context-persistence?view=azurermps-4.4.1#how-the-context-autosave-setting-is-remembered