I have a simple console app trying to create a distribution group in Exchange via PowerShell and add a few members to it.
class Program
{
static void Main(string[] args)
{
string userName = "foo";
string password = "pwd";
// Encrypt password using SecureString class
SecureString securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
PSCredential credential = new PSCredential(userName, securePassword);
// Connection information object required to connect to the service
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
new Uri("https://ps.outlook.com/powershell"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange",
credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
connectionInfo.MaximumConnectionRedirectionCount = 2;
// Create runspace on remote Exchange server
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using(PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;
Command newDG = new Command("New-DistributionGroup");
newDG.Parameters.Add(new CommandParameter("Name", "Test"));
ps.Commands.AddCommand(newDG);
Command addDGMember1 = new Command("Add-DistributionGroupMember");
addDGMember1.Parameters.Add(new CommandParameter("Identity", "Test"));
addDGMember1.Parameters.Add(new CommandParameter("Member", "[email protected]"));
ps.Commands.AddCommand(addDGMember1);
Command addDGMember2 = new Command("Add-DistributionGroupMember");
addDGMember2.Parameters.Add(new CommandParameter("Identity", "Test"));
addDGMember2.Parameters.Add(new CommandParameter("Member", "[email protected]"));
ps.Commands.AddCommand(addDGMember2);
try
{
// Invoke command and store the results in a PSObject
Collection<PSObject> results = ps.Invoke();
if (ps.Streams.Error.Count > 0)
{
foreach (ErrorRecord error in ps.Streams.Error)
{
Console.WriteLine(error.ToString());
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Operation completed.");
}
}
}
Console.ReadKey();
}
}
When I run my app it throws this error: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
But the distribution group actually gets created.
Also, I notice that when I comment out the command to create a new distribution group and run the commands to add the distribution group members, only the first member is added. I'm really confused as to how I should proceed on this and I have the following questions:
How can I get my code to run all the commands successfully?
What would be the best way to do multiple remote PowerShell commands? Do I run each command separately, check the return object if it was successful, and then proceed to the next command. Any performance issues to be aware of?
Does the runspace run only one command at a time?
When I try the following code, I run into this error: The syntax is not supported by this runspace. This might be because it is in no- language mode.
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using(PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;
Pipeline pipe = runspace.CreatePipeline();
pipe.Commands.AddScript("New-DistributionGroup -Name Test2");
try
{
Collection<PSObject> results = pipe.Invoke();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}