Hi,
I am trying to establish an implicit session with a remote host from my machine from a C# code. I am using runspace API for that. the code snippet is provided below
Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); //constructing the vmname parameter here vmname = useralias + DateTime.Now.ToString(); Pipeline pipeline = runspace.CreatePipeline(); string scripttext = "$secpasswd = ConvertTo-SecureString '222_bbbb' -AsPlainText –Force"; string scripttext1 = "$mycreds = New-object -typename System.Management.Automation.PSCredential('TS-TEST-09\\Administrator',$secpasswd)"; string scripttext2 = "$s = New-PSSession -ComputerName TS-TEST-09 -Credential $mycreds"; //not accepting session string here, only computername acceptable string scripttext3 = "Enter-PSSession -Session $s"; //put invoke command here instead of directly calling out the file execution //Command cmd = new Command(@"C:\mypath\helper.ps1", true); //cmd.Parameters.Add("local_useralias", useralias); //cmd.Parameters.Add("local_vmname", vmname); //cmd.Parameters.Add("local_datastore", datastoredropdown.Text.ToString()); //cmd.Parameters.Add("local_architecture", architecturedropdown.Text.ToString()); pipeline.Commands.AddScript(scripttext); pipeline.Commands.AddScript(scripttext1); pipeline.Commands.AddScript(scripttext2); pipeline.Commands.AddScript(scripttext3); //pipeline.Commands.Add(cmd); Collection<PSObject> results = pipeline.Invoke(); runspace.Close();
this code is expected to enter into an implicit session with machine TS-TEST-09 and invoke the script helper.ps1 existing on that machine(that part is commented out in the code currently).
now the problem is that i can't enter into the session $s using -Session parameter(highlighted) however i can enter into it using -Computername parameter.
In later case, however, the code tries to access helper.ps1 from the local host and not on the remote server.
Is there a method to accomplish this task?
any help will be much appreciated.
Thanks,
Manish
So, first, this isn't implicit remoting. This is plain, explicit, 1-to-1 remoting. You're outright creating the session and entering into it.
The reason I point this out is that I'm not going to be able to help you - I'm the furthest thing from a C# person, and that includes using PowerShell from within C#. I know there's different rules that apply, but I don't know what they are. But I know using the term "implicit" is going to throw off whoever else you ask :).
And what's in $s isn't a session "string;" it's a PSSession object.
I guess the way I'd troubleshoot this is to try it manually, from a PowerShell console. Can you run these commands normally, just in a console window? If not, troubleshoot the problem there (and I can try and help). If they work fine there, then the problem is in moving this into your C# code, and I'm outta my depth (but I can recommend folks who might help).
I can tell you this, though: If you're on Host A, and you're remoting into Host B, and you want Host B to run a script that's on Host A, it won't work. That isn't how Remoting is designed to operate. If that's what you're trying to accomplish, you'd do it using Invoke-Command and the -FilePath parameter. You run the command on Host A, not on Host B. It will connect to Host B (using either a session or computer name), transmit the contents of the script to Host B, let Host B execute it, and then return the resulting deserialized objects. Again, this is something I'd test in PowerShell first, then in C#.
Hi Don,
Thanks a lot for the prompt response!
Yes, I stand corrected on this usage of 'implicit' remoting. Thanks for pointing that out.
$s is treated as a PSSession object in the script as 'enter-PSSession' expects this object and not the string as the parameter.
As for your last scenario, here's what i intend to do. I am on host A and I remote to host B. now I want host B to execute a script file that is on host B along with few parameters that i have passed from my session in host A.
whatever is inside scripttexts has been sequentially and successfully executed many times on a powershell console so lets put aside the validity of these command at least from purely powershell perspective. Also, i have gained some more insight into the exact nature of error and i will explain it to you below.
I think the PSHost i am running my application on doesnt support 'Enter-PSSession $s' command as is apparent from the following exception mesage:
invokedSystem.Management.Automation.CmdletInvokationException : the method or operation is not implemented.
at invokedSystem.Management.Automation.Internal.Host.InteralHost.GetIHostSupportsInteractiveSession()
at invokedSystem.Management.Automation. Internal.Host.InteralHost.PushRunspace(Runspace runspace)
at Microsoft.Powershel.Commands.EnterPSSessionCommand.ProcessRecord()
at System.Management.Automation.Cmdlet.DoProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
end of inner exception stack trace
-----------------------------------------------
so i think the solution here should be adding a custom PSHost and providing support for this cmdlet.
However, I don't trust my reasoning fully here and so I need some insights from you.
Thanks again for taking your time out and responding to my query,
Well, technically, you *are* writing a custom PSHost. Sort of.
As I said, this is something in the difference between using PowerShell from its console host (or the ISE) and using it within C# - you're just out of my experience. I'd recommend jumping on the #powershell IRC chat on Freenode - a lot of the PowerShell dev types hang out there. They also answer questions on StackOverflow, if you prefer that. You'd get a better answer.
Thanks for the redirection provided. I have posted the new query at stackoverflow. link is given below for any future tracking for the users, Please redirect the query to your colleagues who may like to have a look at it.
http://stackoverflow.com/questions/8913254/how-to-establish-and-enter-a-remote-session-using-runspace-in-powershell
I've asked a fellow PowerShell MVP, Oisin Grehan (http://nivot.org/) and he answered it on
http://stackoverflow.com/questions/8913254/how-to-establish-and-enter-a-remote-session-using-runspace-in-powershell/8920515#8920515