Dou*_*ley 3 powershell workflow powershell-remoting office365 powershell-3.0
我正在尝试并行从Office 365 Exchange Online中提取数据以节省时间.我无法完成我想要做的事,下面的代码是我到目前为止"最接近"的代码.
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"}
}
}
Run Code Online (Sandbox Code Playgroud)
我定义了两个内联脚本,因为我想设置一次远程会话,然后并行调用Get-Mailbox命令以加快进程.以下是我希望工作流程看起来像的示例.
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
示例运行将如下所示.
$cred = Get-Credential
$mailboxes = Get-Mailbox -ResultSize Unlimited
Get-MailboxStatisticsParallel -o365cred $cred -mailboxes $mailboxes
Run Code Online (Sandbox Code Playgroud)
以下是典型结果.
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]
Run Code Online (Sandbox Code Playgroud)
我可以看到$ session对象正在被反序列化,我猜这是我的问题.问题是如何使用特定的Uri和ConfigurationName建立远程PowerShell会话并利用工作流程提供的功能?
我试图让这项工作,但没有找到办法.
看一下在工作流中运行Windows PowerShell命令,它说:"要在远程计算机上的inlinescript活动中运行命令,您需要在inlinescript块中建立和管理与远程计算机的连接,然后远程运行命令. "
假设我正确理解了该语句,我认为您不能在InlineScript块之外建立会话,然后在块内使用它.我尝试了几种尝试将$ session传递给InlineScript的方法,但它总是$ null.这个限制对我来说很有意义,因为我没有看到工作流如何使用一个远程会话或连接来同时在远程服务器上执行多个命令.我认为您将需要多个远程连接来执行多个远程命令.
我的猜测是,最好的办法是建立一系列与Office 365的连接,然后使用作业并行使用每个会话执行命令.
| 归档时间: |
|
| 查看次数: |
3369 次 |
| 最近记录: |