Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

c# powershell automation in with exchange 2010

$
0
0

Using the following Code:

	    PSCredential psCredential = newPSCredential(username, password)WSManConnectionInfo connectionInfo = newWSManConnectionInfo(newUri("http://myexchangeserver.com/Powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", psCredential);
            connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes.
            connectionInfo.OpenTimeout = 1 * 60 * 1000; // 1 minute.
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos ;Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);Command getmailbox;Pipeline commandPipeLine;try
            {
                runspace.Open();
                getmailbox = newCommand("Get-Mailbox");
                getmailbox.Parameters.Add(newCommandParameter("Identity", mailboxIdentity));
                commandPipeLine = runspace.CreatePipeline();
                commandPipeLine.Commands.Add(getmailbox);Collection<PSObject> getmailboxResults = commandPipeLine.Invoke();if (getmailboxResults.Count > 0)
                {foreach (PSObject getMailboxResult in getmailboxResults)
                    {
                    }
                }
            }catch (Exception exp)
            {string expectionText = exp.Message;string innerExceptionText = exp.InnerException.Message;string stackTrace = exp.StackTrace;
            }

I get an authentication when runspace.Open() is executed. The error text is:

Connecting to remote server failed with the following error message : Logon failure: unknown user name or bad password. For more information, see the about_Remote_Troubleshooting Help topic.

I have confirmed that the username and password are correct. The password is converted to a secure string prior to createding the PSCredential.
I can run EMS commands from the shell on the workstation that I am running this code from.

1. Any Ideas on why my authentication is failing?

2. It seems like a security risk to have credentials required within the program. Is there a way to get the credentials of the currently running user and use those for the remote connection. The EMS uses the current credentials that's what I would like to be able to do as well.

Origionally I was using the following method to setup a Runspace:

RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();PSSnapInException snapInException = null;PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);Runspace runspace = RunspaceFactory.CreateRunspace(rsConfig);

This is the method I have used for exchange 2007 (other than changing the SnapIn to the 2010 version) and it used to work fine. If this method is used the following error occurs when the coomand pipeline is invoked:


Value cannot be null.
Parameter name: parameters

I have set all the parameters for the getmailbox.


Any assistance or suggestions would be great.

Thanks

Bill

Viewing all articles
Browse latest Browse all 7132

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>