none
WSManConnectionInfo

    Question

  • I try to make a Runspace to a remote computer but I don´t find any examples of the WSManConnectionInfo class. Anyone know where to find that?
    I just want to be able to execute Powershell-scripts on the remote computer same as the Powershell 2.0 application.
    I use the new Win7 SDK on a Win7 machine with VS 2008.

    /Christer
    Saturday, September 12, 2009 10:12 PM

Answers


  • Short Answer:
    Confirm that the port number is set correctly; it is probably not 80.
    - From PowerShell, run WSMan:\localhost\Listener\Listener_*\*


    Long Answer:
    1 - Enable Remoting: run the Enable-PSRemoting cmdlet on each box. (answer 'yes', and 'yes', to the prompts)
    2 - On your target machine, retrieve the connection info: dir WSMan:\localhost\Listener\Listener_*\*
    3 - Try the following code:

    WSManConnectionInfo ci = new WSManConnectionInfo(
        <TRANSPORT=SSL?>,
        <COMPUTERNAME>,
        <PORTNUMBER>,
        "/wsman",
        "
    http://schemas.microsoft.com/powershell/Microsoft.PowerShell",
        (PSCredential)null);

    Replace the <COMPUTERNAME> and <PORTNUMBER> from data retrieve from step 2 above.

    • Marked as answer by Figo Fei Monday, April 12, 2010 1:50 AM
    Friday, January 29, 2010 4:43 AM
  • I found this set of examples to be useful:
    http://umniedziala.pl/PowershellRemotingWC.aspx

    Specificaly this:

             public void openRunspace(string uri, string schema, string username, string livePass)

            {

                System.Security.SecureString password = new System.Security.SecureString();

                foreach (char c in livePass.ToCharArray())

                {

                    password.AppendChar(c);

                }

                PSCredential psc = new PSCredential(username, password);

                WSManConnectionInfo rri = new WSManConnectionInfo(new Uri(uri), schema, psc);

                rri.AuthenticationMechanism = AuthenticationMechanism.Basic;

                Runspace runspace = RunspaceFactory.CreateRunspace(rri);

                runspace.Open();

                Application["runspace"] = runspace;

            }


    I had better luck with Basic then with Default autentication.

    Thanks,
    ~Veneta
    • Marked as answer by Figo Fei Monday, April 12, 2010 1:50 AM
    Thursday, October 01, 2009 2:41 AM

All replies

  • I am still trying to execute a powershell script on a remote computer.

    I use the following C# code

     

    SecureString ss = new SecureString();
    ss.AppendChar(
    '1');
    ss.AppendChar(
    '2');
    ss.AppendChar(
    '3');
    ss.AppendChar(
    '4');
    PSCredential credential = new PSCredential("TestWin7", ss);

     

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://testwin7-pc/wsman"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", credential);

    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
    connectionInfo.ProxyAuthentication =
    AuthenticationMechanism.Negotiate;
    Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
    // open it
    runspace.Open();



    I get an PSRemoteTransportException from the runscript.Open() method.
    Connecting to remote server failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.

    I Copy and Paste the ConnectionInfo object from the QuikcWatch so you can see the values of its properties.

      AppName :                                            : "/wsman"
      AuthenticationMechanism                        : Default 
      CertificateThumbprint                             : null 
      ComputerName                                     : "testwin7-pc" 
      ConnectionUri                                        : {http://testwin7-pc/wsman} 
      Credential                                             : {System.Management.Automation.PSCredential} 
      MaximumConnectionRedirectionCount      : 0 
      MaximumReceivedDataSizePerCommand : null 
      MaximumReceivedObjectSize                  : null 
      NoEncryption                                         : false
      NoMachineProfile                                    : false
      Port                                                      : 80 
      ProxyAccessType                                   : None 
      ProxyAuthentication                                : Negotiate 
      ProxyCredential                                      : null 
      Scheme                                                 : "http" 
      ShellUri                                                  : "http://schemas.microsoft.com/powershell/Microsoft.PowerShell
      SkipCACheck                                          : false
      SkipCNCheck                                          : false
      SkipRevocationCheck                               : false
      UseCompression                                      : true
      UseUTF16                                                : false 

    If I do this from a PowerShell window it works just as I would expect.
    I type in the following lines and press enter after each one.
    $cred = get-credential
    $session = New-PSSession -computername TestWin7-PC -cred $cred
    $session.Runspace.ConnectionInfo

    And I get the following output.

    ConnectionUri                                        : http://testwin7-pc/wsman
    ComputerName                                     : testwin7-pc
    Scheme                                                : http
    Port                                                      : 80
    AppName                                              : /wsman
    Credential                                             : System.Management.Automation.PSCredential
    ShellUri                                                 : http://schemas.microsoft.com/powershell/Microsoft.PowerShell
    AuthenticationMechanism                        : Default
    CertificateThumbprint                             :
    MaximumConnectionRedirectionCount      : 0
    MaximumReceivedDataSizePerCommand :
    MaximumReceivedObjectSize                  :
    UseCompression                                    : True
    NoMachineProfile                                    : False
    ProxyAccessType                                   : None
    ProxyAuthentication                                : Negotiate
    ProxyCredential                                     :
    SkipCACheck                                         : False
    SkipCNCheck                                         : False
    SkipRevocationCheck                              : False
    NoEncryption                                          : False
    UseUTF16                                               : False
    Culture                                                  : sv-SE
    UICulture                                               : en-US
    OpenTimeout                                          : 180000
    CancelTimeout                                        : 60000
    OperationTimeout                                    : 180000
    IdleTimeout                                            : 240000

    If I type
    $session | enter-PSSession
    and press enter the promt changes to
    [testwin7-pc]: PS C:\Users\TestWin7\Documents>
    I type
    get-content env:computername
    And I get
    TESTWIN7-PC
    back. It works as I expected. Why doesn´t it work from C#?

    My PowerShell window is not running as an Administrator.

    I have tried to run the .Net app both as Administrator and not.

    Someone who spot what I do wrong?

    /Christer
    Sunday, September 13, 2009 8:26 PM
  • I found this set of examples to be useful:
    http://umniedziala.pl/PowershellRemotingWC.aspx

    Specificaly this:

             public void openRunspace(string uri, string schema, string username, string livePass)

            {

                System.Security.SecureString password = new System.Security.SecureString();

                foreach (char c in livePass.ToCharArray())

                {

                    password.AppendChar(c);

                }

                PSCredential psc = new PSCredential(username, password);

                WSManConnectionInfo rri = new WSManConnectionInfo(new Uri(uri), schema, psc);

                rri.AuthenticationMechanism = AuthenticationMechanism.Basic;

                Runspace runspace = RunspaceFactory.CreateRunspace(rri);

                runspace.Open();

                Application["runspace"] = runspace;

            }


    I had better luck with Basic then with Default autentication.

    Thanks,
    ~Veneta
    • Marked as answer by Figo Fei Monday, April 12, 2010 1:50 AM
    Thursday, October 01, 2009 2:41 AM

  • Short Answer:
    Confirm that the port number is set correctly; it is probably not 80.
    - From PowerShell, run WSMan:\localhost\Listener\Listener_*\*


    Long Answer:
    1 - Enable Remoting: run the Enable-PSRemoting cmdlet on each box. (answer 'yes', and 'yes', to the prompts)
    2 - On your target machine, retrieve the connection info: dir WSMan:\localhost\Listener\Listener_*\*
    3 - Try the following code:

    WSManConnectionInfo ci = new WSManConnectionInfo(
        <TRANSPORT=SSL?>,
        <COMPUTERNAME>,
        <PORTNUMBER>,
        "/wsman",
        "
    http://schemas.microsoft.com/powershell/Microsoft.PowerShell",
        (PSCredential)null);

    Replace the <COMPUTERNAME> and <PORTNUMBER> from data retrieve from step 2 above.

    • Marked as answer by Figo Fei Monday, April 12, 2010 1:50 AM
    Friday, January 29, 2010 4:43 AM
  • Did you guys ever figure this out?  I'm getting other errors, any help would be awesome:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/0d8d1052-4e3c-4cf2-b25b-2a13a24ba0e1

    Sunday, April 11, 2010 6:55 PM
  • Trick for me was to use ProxyAuthentication = AuthenticationMechanism.Negotiate on the WSManConnectionInfo object.

    Found the hint at: http://scorpiotek.com/blog/?p=770

    This worked from a workgroup machine connecting to a domain-joined server.

    Oops - just realized the blog URL resembles the person asking the last question, oh well I guess this closes the loop on the forum thread ;-)


    CraigMartin – Edgile, Inc. – http://identitytrench.com
    • Edited by Craig Martin Tuesday, February 08, 2011 9:52 AM Duh - pointing the response to the person's own blog
    Tuesday, February 08, 2011 9:50 AM