PowerShell workflows in Azure Automation with parallel processing of Azure subscriptions

Copper Contributor

I am trying to utilise 'foreach -Parallel' to perform processing across multiple subscriptions however I've noticed that 'parallel' uses the same 'PSSourceJobInstanceId' as the workflow therefore only supports one subscription context at a time. 

 

Is there a work-around for this or do Azure Automation workflows not support 'Parallel' across subscription contexts?

 

Code example; 

 

workflow WF_Test
{
    $cred = Get-AutomationPSCredential -Name '###'
    $null = Add-AzAccount -Credential $cred
    $subscriptionList = @('ec5f9429-###','ccd47b39-###')
    "* With -Parallel"
    foreach -Parallel ($subscription in $subscriptionList) {        
        Set-AzContext -Subscription $subscription
        "Total VMs $((Get-AzVM).Count)"
    }
    "* Without -Parallel"
    Set-AzContext -Subscription $subscriptionList[0]
    "Total VMs $((Get-AzVM).Count)" 
    Set-AzContext -Subscription $subscriptionList[1]
    "Total VMs $((Get-AzVM).Count)"

}

 

 

Output;

 

* With -Parallel

 

PSComputerName        : localhost

PSSourceJobInstanceId : 2b5feb08-dadb-4b21-a689-aa94f169095b

Name                  : NonProd (ccd47b39-###) - ### - ###

Account               : ###

Environment           : AzureCloud

Subscription          : ccd47b39-###

Tenant                : ###

TokenCache            : Microsoft.Azure.Commands.Common.Authentication.Core.ProtectedFileTokenCache

VersionProfile        :

ExtendedProperties    : {}

PSComputerName        : localhost

 

Total VMs - 98

 

PSSourceJobInstanceId : 2b5feb08-dadb-4b21-a689-aa94f169095b

Name                  : Prod (ec5f9429-###) - ### - ###

Account               : ###

Environment           : AzureCloud

Subscription          : ec5f9429-###

Tenant                : ###

TokenCache            : Microsoft.Azure.Commands.Common.Authentication.Core.ProtectedFileTokenCache

VersionProfile        :

ExtendedProperties    : {}

 

Total VMs - 98

 

* Without -Parallel

 

PSComputerName        : localhost

PSSourceJobInstanceId : 2b5feb08-dadb-4b21-a689-aa94f169095b

Name                  : Prod (ec5f9429-###) - ### - ###

Account               : ###

Environment           : AzureCloud

Subscription          : ec5f9429-###

Tenant                : ###

TokenCache            : Microsoft.Azure.Commands.Common.Authentication.Core.ProtectedFileTokenCache

VersionProfile        :

ExtendedProperties    : {}

 

Total VMs - 98

 

PSComputerName        : localhost

PSSourceJobInstanceId : 2b5feb08-dadb-4b21-a689-aa94f169095b

Name                  : NonProd (ccd47b39-###) - ### - ###

Account               : ###

Environment           : AzureCloud

Subscription          : ccd47b39-###

Tenant                : ###

TokenCache            : Microsoft.Azure.Commands.Common.Authentication.Core.ProtectedFileTokenCache

VersionProfile        :

ExtendedProperties    : {}

 

Total VMs - 31

 

Also tried using a 'runspace' withing the 'foreach -Parallel' however this made no difference;

        InlineScript {
            $Runspace = [runspacefactory]::CreateRunspace()
            $PowerShell = [powershell]::Create()
            $PowerShell.runspace = $Runspace
            $Runspace.Open()
            [void]$PowerShell.AddScript({
                Set-AzContext -Subscription $Using:subscription
                "Total VMs $((Get-AzVM).Count)"
            })
            $PowerShell.Invoke()
            $PowerShell.Dispose()
        }

 

Thanks in advance.

Paul

0 Replies