Skip to content
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

Remove DefaultProfile usage from constructor in ResourceManipulationCmdletBase #5705

Closed
huntr01 opened this issue Mar 8, 2018 · 3 comments
Closed
Assignees
Milestone

Comments

@huntr01
Copy link

huntr01 commented Mar 8, 2018

Description

Passing in context using the AzureRmContext (DefaultProfile) optional variable does not work for Set-AzureRmResource. This works for other *-AzureRmResource commands (eg get-azurermresource).

I am calling the cmdlet via a powershell job which needs authentication/tenant details provided by the context object as described here: https://docs.microsoft.com/en-us/powershell/azure/context-persistence?view=azurermps-5.4.0

As mentioned, this works for other *-AzureRmResource commands and the documentation suggested it accepts the AzureRMContext parameter

Script/Steps for Reproduction

Create a script to run as a job called testingjobs.ps1

param($myresource, $AzureCtx)
$DebugPreference = "Continue"


#Get-AzureRmResource -ResourceId $myresource.ResourceId -AzureRmContext $AzureCtx #This works
Set-AzureRmResource -Tag $myresource.Tags -ResourceId $myresource.ResourceId -Force -AzureRmContext $AzureCtx #this does not work
Resolve-AzureRmError

Then call the script via a job, passing in the Azure Context so the cmdlets can use it to authenticate to azure. Ensure a valid ResourceID is specified in the Get-AzureRmResource

Add-AzureRmAccount
$myresource = Get-AzureRmResource -ResourceId /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/Default-RecoveryServices-ResourceGroup-westeurope/providers/microsoft.recoveryservices/vaults/XXXresourcenameXXX
 
Start-Job -FilePath .\testingjobs.ps1 -ArgumentList ($myresource,(Get-AzureRmContext))
Get-job | Wait-Job
Get-Job | Receive-Job -Keep 

Module Version

Get-Module -Name AzureRM -ListAvailable

ModuleType Version    Name                                ExportedCommands                                                                                                                                                                                                     
---------- -------    ----                                ----------------                                                                                                                                                                                                     
Script     5.4.1      AzureRM                                                

Environment Data

$PSVersionTable
Name                           Value                                                                                                                                                                                                                                           
----                           -----                                                                                                                                                                                                                                           
PSVersion                      5.1.14393.2068                                                                                                                                                                                                                                  
PSEdition                      Desktop                                                                                                                                                                                                                                         
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                                                         
BuildVersion                   10.0.14393.2068                                                                                                                                                                                                                                 
CLRVersion                     4.0.30319.42000                                                                                                                                                                                                                                 
WSManStackVersion              3.0                                                                                                                                                                                                                                             
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                                                             
SerializationVersion           1.1.0.1   

Debug Output

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
61     Job61           BackgroundJob   Completed     True            localhost            param($myresource, $Az...
Run Connect-AzureRmAccount to login.
    + CategoryInfo          : InvalidOperation: (:) [], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation
    + PSComputerName        : localhost
 
DEBUG: 14:29:45 - ResolveError begin processing with ParameterSet 'AnyErrorParameterSet'.

RunspaceId       : 5f14658c-ec3b-4f01-a272-926104c58558
InnerException   : False
Exception        : System.Management.Automation.PSInvalidOperationException: Run Connect-AzureRmAccount to login.
                      at Microsoft.Azure.Commands.ResourceManager.Common.AzureRMCmdlet.get_DefaultContext()
                      at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManipulationCmdletBase..ctor()
                      at lambda_method(Closure )
                      at System.Management.Automation.CommandProcessor.Init(CmdletInfo cmdletInformation)
Message          : Run Connect-AzureRmAccount to login.
StackTrace       :    at Microsoft.Azure.Commands.ResourceManager.Common.AzureRMCmdlet.get_DefaultContext()
                      at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.ResourceManipulationCmdletBase..ctor()
                      at lambda_method(Closure )
                      at System.Management.Automation.CommandProcessor.Init(CmdletInfo cmdletInformation)
HelpLink         : 
ErrorDetails     : 
ErrorCategory    : InvalidOperation: (:) [], PSInvalidOperationException
InvocationInfo   : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 6

DEBUG: AzureQoSEvent: CommandName - Resolve-AzureRmError; IsSuccess - True; Duration - 00:00:00.0590233; Exception - ;
DEBUG: Finish sending metric.
DEBUG: 14:29:46 - ResolveError end processing.
DEBUG: 14:29:46 - ResolveError end processing.

@huntr01
Copy link
Author

huntr01 commented Mar 8, 2018

Found a workaround for the moment. Add the following to the testingjobs.ps1 script causes the context to be saved for the process / session so that each command does not need to be authenticated.

Enable-AzureRmContextAutosave -Scope Process -DefaultProfile $AzureCtx

@markcowl
Copy link
Member

@huntr01 Good catch - it looks like this particular cmdlet is trying to usethe context before the parameter is bound here: https://github.com/Azure/azure-powershell/blob/preview/src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Implementation/CmdletBase/ResourceManipulationCmdletBase.cs#L123

Need to move this code into BeginProcessing or ProcessRecord

@markcowl markcowl changed the title Set-AzureRmResource not accepting AzureRMContext (DefaultProfile) Remove DefaultProfile usage from constructor in ResourceManipulationCmdletBase Mar 14, 2018
@markcowl markcowl modified the milestones: 2018-03-23, 2018-04-06 Mar 26, 2018
@maddieclayton maddieclayton modified the milestones: 2018-05-18, 2018-06-01 May 22, 2018
@sphibbs sphibbs assigned cormacpayne and unassigned praries880 May 29, 2018
@sphibbs sphibbs modified the milestones: 2018-06-01, 2018-06-15 May 29, 2018
@maddieclayton maddieclayton modified the milestones: 2018-06-15, 2018-07-13 Jun 19, 2018
@maddieclayton maddieclayton modified the milestones: 2018-07-13, 2018-07-27 Jul 16, 2018
@markcowl markcowl modified the milestones: 2018-07-27, 2018-08-10 Jul 26, 2018
@cormacpayne
Copy link
Member

This has been fixed

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

No branches or pull requests

6 participants