New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
KarlGW opened this Issue Nov 13, 2015 · 17 comments

Comments

Projects
None yet
@KarlGW
Copy link

KarlGW commented Nov 13, 2015

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?

@iamshital

This comment has been minimized.

Copy link

iamshital commented Nov 15, 2015

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

@KarlGW

This comment has been minimized.

Copy link

KarlGW commented Nov 17, 2015

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

@matthewbcline

This comment has been minimized.

Copy link

matthewbcline commented Nov 19, 2015

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.

@KarlGW

This comment has been minimized.

Copy link

KarlGW commented Jan 13, 2016

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

@YidingZhou

This comment has been minimized.

Copy link

YidingZhou commented Feb 18, 2016

Same here. Any plan to fix this?

@markcowl

This comment has been minimized.

Copy link
Member

markcowl commented Feb 19, 2016

@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

@KarlGW

This comment has been minimized.

Copy link

KarlGW commented Feb 19, 2016

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

This comment has been minimized.

Copy link

KarlGW commented Feb 19, 2016

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

@markcowl

This comment has been minimized.

Copy link
Member

markcowl commented Mar 11, 2016

@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 markcowl closed this Mar 11, 2016

@SiloReed

This comment has been minimized.

Copy link

SiloReed commented May 22, 2017

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

@CASSMF

This comment has been minimized.

Copy link

CASSMF commented Jun 7, 2017

@SiloReed found any Solution to background Jobs?

@SiloReed

This comment has been minimized.

Copy link

SiloReed commented Jun 7, 2017

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.

@SvenVR73

This comment has been minimized.

Copy link

SvenVR73 commented Jun 30, 2017

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.

@smithkl42

This comment has been minimized.

Copy link

smithkl42 commented Jan 9, 2018

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

@KarlGW

This comment has been minimized.

Copy link

KarlGW commented Jan 10, 2018

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

@pixelicous

This comment has been minimized.

Copy link

pixelicous commented Feb 21, 2018

any solution to this workaround?

@Maqsoodp

This comment has been minimized.

Copy link

Maqsoodp commented May 21, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment