Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Need help to use powershell script with two foreach in c#

$
0
0

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,

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();

varresult1 = 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.


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>