Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
 
 
Posted 17-Apr-12 19:49pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 425
1 OriginalGriff 184
2 BillWoodruff 170
3 Zoltán Zörgő 165
4 Maciej Los 163
0 OriginalGriff 7,854
1 DamithSL 5,949
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,084
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 18 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100