0

I am trying to return a PrimarySMTPAddress to a variable, in Powershell the code I run is this:

Get-Mailbox -identity UserName | select PrimarySMTPAddress

And it returns the correct value, I want to get this in my C# Code, I have done the following:

string getPrimarySMTP = "Get-Mailbox -identity " + username + "| select PrimarySMTPAddress";

var runSpace = RunspaceFactory.CreateRunspace(Utility.CreateConnectionInfo());
runSpace.Open();
var pipeline = runSpace.CreatePipeline();

pipeline.Commands.AddScript(getPrimarySMTP);
var primarySmtp = pipeline.Invoke();
runSpace.Dispose();

I would Expect this to return the same data, but it doesn't. I just get an exception:

The term 'select' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Is this the way to return values from a powershell command?

2
  • I know next to nothing about C# but does Select-Object work in its place? Maybe it doe snot konw aliases Commented Mar 4, 2015 at 0:33
  • @JStellato how is it going with this problem? Did either of the answers help? Commented Mar 4, 2015 at 20:45

3 Answers 3

0

For what version of Exchange ? for 2010 up you need to use Remote Powershell see https://msdn.microsoft.com/en-us/library/office/ff326159%28v=exchg.150%29.aspx . (even in 2007 your code would work because you haven't loaded the snapin).

Cheers Glen

Sign up to request clarification or add additional context in comments.

Comments

0

You may need to add an additional space character before the pipe. It' being concatenated to the username, with the resulting string becoming ... -identity UserName| select ..."

Here's the corrected statement:

string getPrimarySMTP = "Get-Mailbox -identity " + username + " | select PrimarySMTPAddress";

Comments

0

Thanks for asking this question, it helped me to lead to the answer I needed. My code resulted in the following using RemoteRunspace to an Exchange 2013 environment:

    try
    {
        var target = new Uri(Uri);
        SecureString PSPassword = new SecureString();
        foreach (char c in ConfigurationManager.AppSettings["Password"])
        {
            PSPassword.AppendChar(c);
        }
        //var cred = (PSCredential)null;
        PSCredential cred = new PSCredential(ConfigurationManager.AppSettings["Username"], PSPassword);
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(target, shell, cred);
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
        connectionInfo.OperationTimeout = 1 * 60 * 1000; // 4 minutes.
        connectionInfo.OpenTimeout = 1 * 30 * 1000; // 1 minute.
        using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
        {
            remoteRunspace.Open();
            using (PowerShell powershell = PowerShell.Create())
            {
                powershell.Runspace = remoteRunspace;
                powershell.AddScript(PSSnapin);
                powershell.Invoke();
                powershell.Streams.ClearStreams();
                powershell.Commands.Clear();
                Pipeline pipeline = remoteRunspace.CreatePipeline();
                Command getMailBox = new Command("Get-Mailbox");
                getMailBox.Parameters.Add("Identity", Username);
                Command cmd = new Command("Select-Object");
                string[] Parameter = new string[] { "PrimarySMTPAddress" };
                cmd.Parameters.Add("Property", Parameter);
                pipeline.Commands.Add(getMailBox);
                pipeline.Commands.Add(cmd);
                Collection<PSObject> results = pipeline.Invoke();
                primarySMTPAddress = results[0].ToString();
                primarySMTPAddress = primarySMTPAddress.ToUpper().Replace("@{PRIMARYSMTPADDRESS=", "");
                primarySMTPAddress = primarySMTPAddress.ToUpper().Replace("}", "");
            }
            remoteRunspace.Close();
        }
        return primarySMTPAddress;
    }
    catch
    {
        return "Error";
    }

Hope this helps anyone in future.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.