Search code examples
powershellworkflowpowershell-remoting

PowerShell Workflow - InlineScript Remoting


How can you use PowerShell workflows to interact with Exchange Online via remote PowerShell and take advantage of the workflow features such as parallel foreach, retries, etc.?

#

I could never find specific examples of this and finally got it working so I wanted to share. This PowerShell workflow allows you to query Exchange Online (could be Exchange on-premises as well) in parallel, automatically retries on error and throttles itself.

Hopefully this is of benefit to others (and is an appropriate way to post a question/answer), if you have other examples of PowerShell workflows using remoting I would love to see them.


Solution

  • workflow Test-ExchangeQuery {
        <#
        .Synopsis
           Short description
        .DESCRIPTION
           Long description
        .EXAMPLE
           Example of how to use this cmdlet
        .EXAMPLE
           Another example of how to use this cmdlet
        #>
        Param
        (
            # Username of account
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            [string[]]
            $Identity,
    
            # Exchange / AD Credentials
            [Parameter(Mandatory=$true)]
            [System.Management.Automation.PSCredential]
            [System.Management.Automation.Credential()]
            $Credential
        )
    
        Set-PSWorkFlowData -PSAllowRedirection $true
    
        ForEach -Parallel -ThrottleLimit (2) ($user in $Identity) {
            InlineScript {
                Get-Mailbox -Identity $using:user | Select-Object Name, PrimarySmtpAddress
            } -DisplayName "Querying Exchange" `
                -PSCredential $Credential `
                -PSConnectionUri "https://ps.outlook.com/powershell/" `
                -PSConfigurationName "Microsoft.Exchange" `
                -PSComputerName $null `
                -PSAuthentication Basic `
                -PSConnectionRetryCount 3 `
        }
    }