none
Which one to use PSCommand or Command with PowerShell object ? RRS feed

  • Question

  • Hi All,

    I have a C# application to connect to exchange 2010 and manage mailboxes. We have more than 1 domain in the forest but only 1 exchange server installed.

    Domain1.com contains exchanges server installed.
    Domain2.com has only distribution groups.

    I am using remote powershell to connect to the exchange server. But while executing command, we have 2 options, use either Command like below...

    Powershell powershell = PowerShell.Create()
    Command cmd = new Command("Get-DistributionGroup");
    cmd.Parameters.Add("ReadFromDomainController");
    powershell.AddCommand(cmd.CommandText);
    foreach (CommandParameter cmdParam in cmd.Parameters)
    {
        powershell.AddParameter(cmdParam.Name, cmdParam.Value);
    }


    OR use PSCommand like below...

    PSCommand psCmd = new PSCommand();
    psCmd.AddCommand("Get-DistributionGroup");
    psCmd.AddParameter("Identity", user);
    psCmd.AddParameter("ReadFromDomainController");
    powershell.Commands = psCmd;

    Both commands get same information for all properties except for 'OriginatingServer'.

    If I use Command, I get OriginatingServer value as DC belonging to Domain1.com (incorrect) but when I use PSCommand, I get Dc belonging to Domain2.com (correct)...

    Why is this? Which is the right way ? To use Command OR PSCommand ?

    /M

    Monday, November 5, 2012 10:28 AM

All replies

  • Hi All,

    Is there any information about this issue?

    Or at least which method does Microsoft recommend? I see them saying both can be used...

    help..!


    /M

    Tuesday, November 6, 2012 9:40 AM
  • I don't really know the difference between the two, but I notice that in the second bit of code you have an extra parameter, "Identity". Perhaps that is enough to make a difference? What results do you get in an actual PS console if you type your command in, both with and without that Identity parameter?

    Mobile OWA For Smartphone
    www.leederbyshire.com
    email a@t leederbyshire d.0.t c.0.m

    Tuesday, November 6, 2012 1:30 PM
  • sorry I missed the Identity parameter while typing here.. also it should be groupName (a local variable)

    Powershell powershell = PowerShell.Create()
    Command cmd = new Command("Get-DistributionGroup");
    cmd.Parameters.Add("Identity",groupName);
    cmd.Parameters.Add("ReadFromDomainController");
    powershell.AddCommand(cmd.CommandText);
    foreach (CommandParameter cmdParam in cmd.Parameters)
    {
        powershell.AddParameter(cmdParam.Name, cmdParam.Value);
    }

    Second case should be ....

    PSCommand psCmd = new PSCommand();
    psCmd.AddCommand("Get-DistributionGroup");
    psCmd.AddParameter("Identity", groupName);
    psCmd.AddParameter("ReadFromDomainController");
    powershell.Commands = psCmd;

    I am searching for a distribution group here, so 'Identity' parameter is required. I get correct value (DC belonging to domain2.com) for OriginatingServer property when I execute the below command in the shell...

    > Get-DistributionGroup -Identity <group_Name> -ReadFromDomainController | Select OriginatingServer


    /M

    Tuesday, November 6, 2012 3:20 PM
  • I've never tried it, but perhaps you could use something like start-transcript to log the commands that are sent in both cases.

    Mobile OWA For Smartphone
    www.leederbyshire.com
    email a@t leederbyshire d.0.t c.0.m


    Tuesday, November 6, 2012 3:34 PM