locked
PS Remoting from C# RRS feed

  • Question

  • I am trying to do PS remoting from my test application. I have created my own PowerShell cmdlet (which returns all running processes on the machine). This cmdlet access an input of type class 'GetProcInputs' which has two properties in it. When I run this cmdlet on the local machine, it works fine. But when I do PS remoting and try to run this cmdlet on another machine, it fails with the error below.

    Cannot bind parameter 'Inputs'. Cannot convert the "InputParameters.GetProcInputs" value of type "Deserialized.InputParameters.GetProcInputs" to type "InputParameters.GetProcInputs".

     

    Here is my C# code if that helps:

     

    public static string RunScript(string scriptText)
    		{
    			scriptText = "Get-Process";
    			Runspace remoteRunspace = null;
    			openRunspace("http://host:5985/wsman",
    				 "http://schemas.microsoft.com/powershell/Microsoft.PowerShell",
    				 "username",
    				 "password",
    				 ref remoteRunspace);
    
    			StringBuilder stringBuilder = new StringBuilder();
    			using (PowerShell powershell = PowerShell.Create())
    			{
    				powershell.Runspace = remoteRunspace;
    				powershell.AddCommand("add-pssnapin");
    				powershell.Commands.Commands[0].Parameters.Add("Name", "EpicSnapIn");
    				//powershell.AddCommand("Get-Proc");
    				//powershell.Invoke();
    				Collection<PSObject> results = powershell.Invoke();
    				powershell.Commands.Clear();
    				powershell.Runspace = remoteRunspace;
    				powershell.AddCommand("Get-Proc");
    				GetProcInputs inp = new GetProcInputs("w", true);
    				powershell.Commands.Commands[0].Parameters.Add(new CommandParameter("Inputs", inp));
    				results = powershell.Invoke();
    				remoteRunspace.Close();
    				foreach (PSObject obj in results)
    				{
    					Process p = new Process();
    					PSObject pso = new PSObject(p);
    					stringBuilder.AppendLine(obj.ToString());
    				}
    			}
    
    			return stringBuilder.ToString();
    		}
    
    		public static void openRunspace(string uri, string schema, string username, string livePass, ref Runspace remoteRunspace)
    		{
    			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;
    			//rri.ProxyAuthentication = AuthenticationMechanism.Negotiate;
    			remoteRunspace = RunspaceFactory.CreateRunspace(rri);
    			remoteRunspace.Open();
    		}
    



    MP
    • Moved by Paul Zhou Wednesday, October 26, 2011 7:45 AM off-topic (From:.NET Base Class Library)
    Saturday, October 22, 2011 12:37 AM

Answers

  • Hi,

     

    Welcome to the MSDN forum.

     

    According to your description, I'd like to move your thread to off-topic because it is a problem related to Power Shell development. You may post thread in PowerShell forum. There are more experts in that forum so that you may get more useful help there.

     

    Thank you for your posting and understanding.

    Have a nice day.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 26, 2011 7:45 AM

All replies

  • http://msdn.microsoft.com/en-us/library/windows/desktop/ee706578(v=vs.85).aspx

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Saturday, October 22, 2011 5:24 PM
  • Hey Rudedog2, Thanks for the response. But this is not what I am looking for.

    I want to create a custom cmdlet which takes an object (of type that I created. It is class with two properties). When I try to run that cmdlet on local machine and pass an object of that class as input, it works. But when I do PS remoting (using C#), I get the error below when I try to pass object as the parameter value. [See my original post above if you want to look at my c# code]

     

    Cannot bind parameter 'Inputs'. Cannot convert the "InputParameters.GetProcInputs" value of type "Deserialized.InputParameters.GetProcInputs" to type "InputParameters.GetProcInputs".


    MP
    Saturday, October 22, 2011 6:40 PM
  • It almost sounds like you have the same type name defined in two places.  The reported problem appears to be an implicit casting error.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    • Edited by Rudedog2 Saturday, October 22, 2011 6:50 PM
    Saturday, October 22, 2011 6:49 PM
  • I have verified that I don't have same type defined twice. I don't know where this "Deserialized.InputParameters.*" is coming from.

     

    I made some changes to my cmdlet and now I am passing an object of type PSObject which wraps an object of type 'InputParameters' (my custom class). When the PSObject is received on the remote machine, the 'TypeNames' property of this PSObject has following two values:

     

    psObject.TypeNames:

    "Deserialized.InputParameters.GetProcInputs"

    "Deserialized.System.Object"

     

    Update: I came across this post (http://blogs.msdn.com/b/powershell/archive/2010/01/07/how-objects-are-sent-to-and-from-remote-sessions.aspx). Does this mean that we cannot pass custom objects while in remote session?


    MP

    Saturday, October 22, 2011 7:27 PM
  • Hi,

     

    Welcome to the MSDN forum.

     

    According to your description, I'd like to move your thread to off-topic because it is a problem related to Power Shell development. You may post thread in PowerShell forum. There are more experts in that forum so that you may get more useful help there.

     

    Thank you for your posting and understanding.

    Have a nice day.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 26, 2011 7:45 AM