需要帮助在c#中使用两个foreach的powershell脚本 [英] Need help to use powershell script with two foreach in c#
问题描述
大家好,
我想在c#中使用以下的powershell脚本。该脚本有两个foreach循环,第二个foreach循环使用add-member来添加属于$ mailbox变量的值。我设法编写c#代码来完成第一次foreach。我需要帮助来完成第二个foreach循环的c#代码并使用$ mailbox的值。
Powershell脚本:
ForEach(Get-Mailbox中的$ Mailbox){Get-ActiveSyncDeviceStatistics -Mailbox $ Mailbox.Identity -ErrorAction SilentlyContinue |选择DeviceFriendlyName,Devicetype,DeviceUserAgent | ForEach-Object {$ _ | Add-Member -MemberType NoteProperty -NameMailboxIdentity-value $ Mailbox}}
我设法上去了,
c #code:
PowerShellpowershell = PowerShell.Create();
PSCommandcommand = newPSCommand();
command.AddCommand( Get-Mailbox);
command.AddCommand(where-object);
command.AddParameter(Filterscript,scriptBlock.Create(!$ _ .name.startswith(\ DiscoverySearchMailbox\)));
powershell.Commands = command;
powershell.Runspace = CreateRunSpace.GetRunSpace();
varresult = powershell.Invoke();
PSCommandcommand1 = newPSCommand();
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();
我被困在第二个foreach循环中添加成员时添加get-mailbox的输出。感谢您的帮助和建议。谢谢。
Hi All,
I want to use the below powershell script in c#. The script is with two foreach loop and the 2nd foreach loop uses add-member to add the value that is part of $mailbox variable. I managed to write c# code to work upto first foreach. I need help to complete the c# code with 2nd foreach loop and using the value of $mailbox.
Powershell Script :
ForEach ($Mailbox in Get-Mailbox) {Get-ActiveSyncDeviceStatistics -Mailbox $Mailbox.Identity –ErrorAction SilentlyContinue | Select DeviceFriendlyName, Devicetype, DeviceUserAgent | ForEach-Object { $_ | Add-Member –MemberType NoteProperty -Name "MailboxIdentity" -value $Mailbox}}
I managed to go upto,
c# code:
PowerShellpowershell = PowerShell.Create();
PSCommandcommand = newPSCommand();
command.AddCommand("Get-Mailbox");
command.AddCommand("where-object");
command.AddParameter("Filterscript", scriptBlock.Create("!$_.name.startswith(\"DiscoverySearchMailbox\")"));
powershell.Commands = command;
powershell.Runspace = CreateRunSpace.GetRunSpace();
varresult = powershell.Invoke();
PSCommandcommand1 = newPSCommand();
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();
I'm stuck at 2nd foreach loop to add-member while adding the output of get-mailbox. Appreciate your help and suggestions. Thank you.
推荐答案
邮箱变量。我设法编写c#代码来完成第一次foreach。我需要帮助来完成第二个foreach循环的c#代码并使用
mailbox variable. I managed to write c# code to work upto first foreach. I need help to complete the c# code with 2nd foreach loop and using the value of
邮箱的值。
Powershell脚本:< br $>
ForEach(
mailbox.
Powershell Script :
ForEach (
Get-Mailbox中的邮箱){Get-ActiveSyncDeviceStatistics -Mailbox
Mailbox in Get-Mailbox) {Get-ActiveSyncDeviceStatistics -Mailbox
这篇关于需要帮助在c#中使用两个foreach的powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!