Hi Everyone,
I am revamping my web services to create new mailboxes in Exchange 2007 with PowerShell (using teh exchange snap-in of course). Everything is going hunky doory except for the plain fact that no matter what I do I cannot seem to pass a secure string at all! I am pretty sure I am doing this corectly. Below if my code that I am using to convert and pass the SecureString to PowerShell:
string pass = "password";
SecureString secString = new SecureString();
char[] pwChars = pass.ToCharArray();
foreach (char c in pwChars)
secString.AppendChar(c);
secString.MakeReadOnly(); //closes the string so that nothing else can be passed into it.
results = ems.RunspaceInvoke("New-mailbox -UserPrincipalName chris@contoso.com -alias chris -database 'server1\\database1\\mailboxes' -Name ChrisAshton -OrganizationalUnit mailuser -password " + secString + " -FirstName Chris -LastName Ashton -DisplayName ChrisAshton");
foreach (PSObject item in results)
{
TextBox5.Text = item.Members["Name"].Value.ToString();
}
As you can see from my code above, I simply take the plain string and convert it into a character array. I then cycle through each character and append the SecureString with that converted character. The process repeats until we are through all the characters. I then make the SecureString a read-only so that it doesnt get copied into multiple memory space. I then pass the string into my powershell command and PRESTO! I get the following error when executing:
Invalid cast from 'System.String' to 'System.Security.SecureString'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'System.Security.SecureString'.
What do you think could be the issue here? When I debug I see the string being passed as a SecureString, so why am I getting this error from powershell?
-Timothy