azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Azure does not keep session/login in a background job, version 1.0.1

Open KarlGW opened this issue 11 years ago • 17 comments

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?

KarlGW avatar Nov 13 '15 16:11 KarlGW

I'm also facing this issue. Please fix this.

iamshital avatar Nov 15 '15 17:11 iamshital

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).

KarlGW avatar Nov 17 '15 10:11 KarlGW

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.

matthewbcline avatar Nov 19 '15 17:11 matthewbcline

The same problem as me then. Doesn't seem to be fixed yet either with the new versions of the Azure Module.

KarlGW avatar Jan 13 '16 10:01 KarlGW

Same here. Any plan to fix this?

YidingZhou avatar Feb 18 '16 05:02 YidingZhou

@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

markcowl avatar Feb 19 '16 03:02 markcowl

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.

KarlGW avatar Feb 19 '16 08:02 KarlGW

@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 avatar Feb 19 '16 10:02 KarlGW

@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.

markcowl avatar Mar 11 '16 23:03 markcowl

Looks like version 3.0.0 of the AzureRM.profile module removes Select-​Azure​Rm​Profile and Save-​Azure​Rm​Profile. 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-​Azure​Rm​Profile

SiloReed avatar May 22 '17 18:05 SiloReed

@SiloReed found any Solution to background Jobs?

CASSMF avatar Jun 07 '17 02:06 CASSMF

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.

SiloReed avatar Jun 07 '17 15:06 SiloReed

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.

SvenVR73 avatar Jun 30 '17 09:06 SvenVR73

Yeah, running into this same problem. Any workarounds greatly appreciated.

smithkl42 avatar Jan 09 '18 23:01 smithkl42

@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.

KarlGW avatar Jan 10 '18 19:01 KarlGW

any solution to this workaround?

thepixelabs avatar Feb 21 '18 14:02 thepixelabs

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

Maqsoodp avatar May 21 '18 05:05 Maqsoodp