Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Remote powershell / WinRM to Server 2012

Remote powershell / WinRM to Server 2012

Scheduled Pinned Locked Moved C#
comsysadminwindows-admintestingtools
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JD86
    wrote on last edited by
    #1

    I've hit a road block and can't figure out why I am getting this response back. It appears that it connects to the server but it returns: "System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server server2.domain.local failed with the following error message: An invalid argument was supplied". I enabled everything I thought on the remote computer:

    winrm set winrm/config/service/auth @{Basic="true"}
    winrm set winrm/config/service @{AllowUnencrypted="true"}
    winrm set winrm/config/client @{TrustedHosts="*"}

    I'm trying to remote powershell so I can run Citrix powershell commands from another computer and here is what I have:

    try
    {
    XenDesktop7 xd = new XenDesktop7("http://server2.domain.local:5985/wsman", @"DOMAIN\Administrator", "#######");
    string[] blah = xd.GetCatalogs();

                foreach (var b in blah)
                {
                    Console.WriteLine(b);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
    

    public CitrixPowershell(string uri, string username, string password)
    {
    this._connection = GetConnection(uri, username, password);
    PSSnapInException snapinException;
    _runspace = RunspaceFactory.CreateRunspace(_connection);
    _runspace.Open();

            \_runspace.RunspaceConfiguration.AddPSSnapIn("Citrix.\*.Admin.V\*", out snapinException);
    
            \_powershell = PowerShell.Create();
            \_powershell.Runspace = \_runspace;
            
        }
    
        private WSManConnectionInfo GetConnection(string uri, string username, string password)
        {
            SecureString pwd = new SecureString();
            foreach (char x in password)
                pwd.AppendChar(x);
    
            PSCredential ps = new PSCredential(username, pwd);
    
            WSManConnectionInfo wsinfo = new WSManConnectionInfo(false, "server2.domain.local", 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", ps);
            wsinfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
            wsinfo.ProxyAuthentication = AuthenticationMechanism.Negotiate;
            return wsinfo;
        }
    
    L 1 Reply Last reply
    0
    • J JD86

      I've hit a road block and can't figure out why I am getting this response back. It appears that it connects to the server but it returns: "System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server server2.domain.local failed with the following error message: An invalid argument was supplied". I enabled everything I thought on the remote computer:

      winrm set winrm/config/service/auth @{Basic="true"}
      winrm set winrm/config/service @{AllowUnencrypted="true"}
      winrm set winrm/config/client @{TrustedHosts="*"}

      I'm trying to remote powershell so I can run Citrix powershell commands from another computer and here is what I have:

      try
      {
      XenDesktop7 xd = new XenDesktop7("http://server2.domain.local:5985/wsman", @"DOMAIN\Administrator", "#######");
      string[] blah = xd.GetCatalogs();

                  foreach (var b in blah)
                  {
                      Console.WriteLine(b);
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.ToString());
              }
      

      public CitrixPowershell(string uri, string username, string password)
      {
      this._connection = GetConnection(uri, username, password);
      PSSnapInException snapinException;
      _runspace = RunspaceFactory.CreateRunspace(_connection);
      _runspace.Open();

              \_runspace.RunspaceConfiguration.AddPSSnapIn("Citrix.\*.Admin.V\*", out snapinException);
      
              \_powershell = PowerShell.Create();
              \_powershell.Runspace = \_runspace;
              
          }
      
          private WSManConnectionInfo GetConnection(string uri, string username, string password)
          {
              SecureString pwd = new SecureString();
              foreach (char x in password)
                  pwd.AppendChar(x);
      
              PSCredential ps = new PSCredential(username, pwd);
      
              WSManConnectionInfo wsinfo = new WSManConnectionInfo(false, "server2.domain.local", 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", ps);
              wsinfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
              wsinfo.ProxyAuthentication = AuthenticationMechanism.Negotiate;
              return wsinfo;
          }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      JD86 wrote:

      failed with the following error message: An invalid argument was supplied".

      Somewhere in your application there is a line of code that has a bad parameter. Identify that and you are on your way to a solution.

      J 1 Reply Last reply
      0
      • L Lost User

        JD86 wrote:

        failed with the following error message: An invalid argument was supplied".

        Somewhere in your application there is a line of code that has a bad parameter. Identify that and you are on your way to a solution.

        J Offline
        J Offline
        JD86
        wrote on last edited by
        #3

        But thats the issue. I've compared it with numerous code examples and this should be working. Example: https://com2kid.wordpress.com/2011/09/22/remotely-executing-commands-in-powershell-using-c/[^] [^] The only thing I can think of is that this is a Server 2012 R2 box and wondering if there is something funky with the security. I haven't tried to a 2008 R2 box yet but plan on doing that today.

        L 1 Reply Last reply
        0
        • J JD86

          But thats the issue. I've compared it with numerous code examples and this should be working. Example: https://com2kid.wordpress.com/2011/09/22/remotely-executing-commands-in-powershell-using-c/[^] [^] The only thing I can think of is that this is a Server 2012 R2 box and wondering if there is something funky with the security. I haven't tried to a 2008 R2 box yet but plan on doing that today.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          JD86 wrote:

          this should be working.

          Maybe so, but you still need to do some debugging to find out why it isn't.

          J 1 Reply Last reply
          0
          • L Lost User

            JD86 wrote:

            this should be working.

            Maybe so, but you still need to do some debugging to find out why it isn't.

            J Offline
            J Offline
            JD86
            wrote on last edited by
            #5

            Well it turns out the code actually does work. I'm not able to get it to work running it from my Server 2012 R2 lab server but from my 2008 R2 lab server it runs fine. I'll have to do some more investigating but at least I know the code is right! Thanks!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups