End Google Ads 201810 - BS.net 01 --> 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();