I'm attempting to pull data from Office 365 Exchange Online in parallel to save time. I'm not able to accomplish what I'm looking to do, the following code is the "closest" I've come so far.
Workflow Get-MailboxStatisticsParallel{
param ($o365cred)
$session = InlineScript {
New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $using:o365cred -Authentication Basic -AllowRedirection
}
InlineScript {
Invoke-Command -Session $session -ScriptBlock {Get-Mailbox "Firstname Middleinitial. Lastname"}
}
}
I defined two inline scripts because I want to setup the remote session once but then call the Get-Mailbox command in parallel to speed up the process. Here is an example of what I would want the workflow to look like.
Workflow Get-MailboxStatisticsParallel{
param ($o365cred, $mailboxes)
$session = InlineScript {
New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $using:o365cred -Authentication Basic -AllowRedirection
}
ForEach -Parallel ($mailbox in $mailboxes){
InlineScript {
Invoke-Command -Session $using:session -ScriptBlock {param ($mailbox); Get-MailboxStatistics $mailbox.UserPrincipalName} -ArgumentList $mailbox
}
}
}
An example run would look like this.
$cred = Get-Credential
$mailboxes = Get-Mailbox -ResultSize Unlimited
Get-MailboxStatisticsParallel -o365cred $cred -mailboxes $mailboxes
Here are the typical results.
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Supply an argument that is not null or empty and then try
the command again.
At Get-MailboxStatisticsParallel:7 char:7
+
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
+ PSComputerName : [localhost]
I can see the $session object is being deserialized, I'm guessing that's my issue. The question is how can I establish a remote PowerShell session with a specific Uri and ConfigurationName and take advantage of the features workflows provide?