这是我想在 c# 中运行的 powershell 脚本:
ForEach ($Mailbox in Get-Mailbox) {Get-ActiveSyncDeviceStatistics -Mailbox
$Mailbox.Identity –ErrorAction SilentlyContinue |
SelectDeviceFriendlyName,Devicetype,DeviceUserAgent | ForEach-Object { $_ | Add-Member –
MemberType NoteProperty -Name "MailboxIdentity" -value $Mailbox}}
我可以在 powershell 脚本中完成 c# 编码直到第一个 foreach 循环,但我不知道如何实现使用 $mailbox 变量的第二个 foreach 以及下面给出的编码。
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("Get-Mailbox");
command.AddCommand("where-object");
command.AddParameter("Filterscript", ScriptBlock.Create("!$_.name.startswith \"DiscoverySearchMailbox\")"));
powershell.Commands = command;
powershell.Runspace = CreateRunSpace.GetRunSpace();
var result = powershell.Invoke();
PSCommand command1 = new PSCommand();
command1.AddCommand("write-output");
command1.AddParameter("InputObject", result);
command1.AddCommand("Foreach-Object");
command1.AddParameter("Process", ScriptBlock.Create("Get-ActiveSyncDeviceStatistics -Mailbox $_.Identity"));
powershell.Commands = command1;
powershell.Runspace =
CreateRunSpace.GetRunSpace();
var result1 = powershell.Invoke();
在添加 get-mailbox 的输出时,我被困在添加成员的第二个 foreach 循环中。提前感谢您的帮助和建议。谢谢你。