Heya Guys, I am trying to connect to a wsman service in VB.Net to admin some exchange accounts. The code I have to perform the connection and make the changes are as follows
Const SHELL_URI As String = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell"
Dim serverUri As New Uri("http://SERVER:5985/wsman")
Dim securepass As New SecureString
For Each c As Char In AppSettings.Get("Password")
securepass.AppendChar(c)
Next
Dim oRemoteCred As New PSCredential(AppSettings.Get("Username"), securepass)
Dim connectionInfo As New WSManConnectionInfo(serverUri, SHELL_URI, oRemoteCred)
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Credssp
Dim oRunspace As Runspace = RunspaceFactory.CreateRunspace(connectionInfo)
Dim sResults As String = ""
Dim exchSecPass As New SecureString
For Each c As Char In AppSettings.Get("otherPassword")
exchSecPass.AppendChar(c)
Next
Dim oExchCred As New PSCredential(AppSettings.Get("otherUsername") & "@domain", exchSecPass)
oRunspace.Open()
Me.InvokePSCommand(oRunspace, "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.admin")
Dim oPipeline As Pipeline = oRunspace.CreatePipeline()
Dim oResults As Collection(Of PSObject)
oPipeline.Commands.Add("Set-Mailbox" + lblOldUserName.Text)
oPipeline.Commands(0).Parameters.Add("DomainController", "DCServer")
oPipeline.Commands(0).Parameters.Add("emailAddresses", txtNewEmail.Text)
oPipeline.Commands(0).Parameters.Add("PrimarySmtpAddress", txtNewEmail.Text)
oPipeline.Commands(0).Parameters.Add("Alias", txtNewUserName.Text)
oResults = oPipeline.Invoke()
For Each oObj As PSObject In oResults
sResults = sResults & oObj.ToString() & vbCrLf
Next
oRunspace.Close()
The line Dim oRunspace As Runspace = RunspaceFactory.CreateRunspace(connectionInfo) is where it errors with the error code.
Unhandled Execution Error
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.Management.Automation.PSInvalidOperationException:
Stack Trace:
[PSInvalidOperationException]
System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.Initialize(Uri connectionUri, WSManConnectionInfo connectionInfo) +1551
System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor(Guid runspacePoolInstanceId, WSManConnectionInfo connectionInfo, PSRemotingCryptoHelper cryptoHelper) +344
System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession session, PSRemotingCryptoHelper cryptoHelper, RunspaceConnectionInfo connectionInfo, URIDirectionReported uriRedirectionHandler) +702
System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePoolInternal rsPool, URIDirectionReported uriRedirectionHandler) +406
System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler.CreateClientRemoteSession(RemoteRunspacePoolInternal rsPoolInternal) +71
System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal clientRunspacePool, TypeTable typeTable) +200
System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo) +505
System.Management.Automation.Runspaces.RunspacePool..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo) +175
System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspacePool(Int32 minRunspaces, Int32 maxRunspaces, RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments) +100
System.Management.Automation.RemoteRunspace..ctor(TypeTable typeTable, RunspaceConnectionInfo connectionInfo, PSHost host, PSPrimitiveDictionary applicationArguments) +607
System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments) +89
System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo) +11
_SearchEdit.Change_Email_SMTP(String OldUsername, String Newusername, String Newemail) in D:\wwwroot\ActiveDirectoryHack\HR_Admin\SearchEditPage.aspx.vb:744
_SearchEdit.bntChangeUserName_Click(Object sender, EventArgs e) in D:\wwwroot\ActiveDirectoryHack\HR_Admin\SearchEditPage.aspx.vb:725
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563