Skip to content

Create cmdlet or parameter set for setting the context for a background job #4970

Closed
@blumu

Description

@blumu

Cmdlet(s)

PowerShell Version

PSVersion 5.1.15063.608

Module Version

Azure Powershell 5.0.1

OS Version

10.0.15063.608

Description

Debug Output

The Set-AzureRmContext cmdlet throws a .Net exception Collection was modified; enumeration operation may not execute. when running within Start-Job:

Note: Two related bugs
#2290 and #1835 were closed but the advertised solution (using the new -AzureRmContext parameter) is not practical: it's not necessarily possible to add the -AzureRmContext switch to every command in the script block, especially when auxiliary scripts are called.

DEBUG: 10:13:40 PM - GetAzureRMContextCommand begin processing with ParameterSet 'GetSingleContext'.
DEBUG: 10:13:40 PM - using account id '****@****.com'...


Name             : ****
Account          : ****@****.com
SubscriptionName : ****
TenantId         : ****
Environment      : AzureCloud

DEBUG: AzureQoSEvent: CommandName - Get-AzureRmContext; IsSuccess - True; Duration - 00:00:00.0138074; Exception - ;
DEBUG: Finish sending metric.
DEBUG: 10:13:40 PM - GetAzureRMContextCommand end processing.
DEBUG: 10:13:40 PM - GetAzureRMContextCommand end processing.


Collection was modified; enumeration operation may not execute.
    + CategoryInfo          : CloseError: (:) [Set-AzureRmContext], InvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand
    + PSComputerName        : localhost

**** Some Azure PS command ****

Name             : ****
Account          : ****@****.com
SubscriptionName : ****
TenantId         : ****
Environment      : AzureCloud

Your Azure credentials have not been set up or have expired, please run Login-AzureRMAccount to set up your Azure credentials.
    + CategoryInfo          : CloseError: (:) [Get-AzureRmStorageAccount], ArgumentException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Management.Storage.GetAzureStorageAccountCommand
    + PSComputerName        : localhost

********

Script/Steps for Reproduction

$DebugPreference="Continue"
Get-AzureRmContext | out-host
$j = Start-Job -ScriptBlock {
        param($context)
        Set-AzureRmContext -Context $context
        Write-Host "**** Some Azure PS command ****"
        Get-AzureRmContext | out-host
        Get-AzureRmStorageAccount
        Write-Host "********"
    } -ArgumentList $c
$j | Receive-Job -Wait

Metadata

Metadata

Assignees

No one assigned

    Labels

    AzureRM 6.xfeature-requestThis issue requires a new behavior in the product in order be resolved.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions