Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.
Posted
Updated 27-Jul-13 7:37am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900