4

I am writing a program that will run a program on a remote machine. I have installed the windows sdk and have successfully uploaded my file with bits now I would like to remotely run a change directory and execute a command using power shell.

The program compiles and runs but in the answer variable it says: "The method or operation is not implemented.\r\n"

// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Enter-PSSession " + host + Environment.NewLine + "cd " + uploadtodir + Environment.NewLine + command + Environment.NewLine + "exit" + Environment.NewLine + "exit" + Environment.NewLine);
// add an extra command to transform the script output objects into nicely formatted strings
// remove this line to get the actual objects that the script returns. For example, the script
// "Get-Process" returns a collection of System.Diagnostics.Process instances.

pipeline.Commands.Add("Out-String");
// execute the script

Collection<PSObject> results = new Collection<PSObject>();
try
{
    results = pipeline.Invoke();
}

catch (Exception ex)
{
    results.Add(new PSObject((object)ex.Message));
}

// close the runspace
runspace.Close();
// convert the script result into a single string

StringBuilder stringBuilder = new StringBuilder();

foreach (PSObject obj in results)
{
    stringBuilder.AppendLine(obj.ToString());
}

string answer = stringBuilder.ToString();

I tried to use another command in the pipeline but it fails also but with no error output.

**pipeline.Commands.AddScript("$sessions = New-PSSession -ComputerName " + host + Environment.NewLine + "Invoke-Command -session $sessions -ScriptBlock {cd " + uploadtodir+"}" + Environment.NewLine+"Invoke-Command -session $sessions -ScriptBlock {"+command+ "}" + Environment.NewLine + "Remove-PSSession -Session $sessions" + Environment.NewLine + "exit" + Environment.NewLine + "exit" + Environment.NewLine);**
John Saunders
  • 157,405
  • 24
  • 229
  • 388
  • FYI, see http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts/ – John Saunders Feb 06 '12 at 18:29
  • The second pipline command did fix it the command itself now is running out of memory –  Feb 06 '12 at 19:27
  • increase the memory for winrm by Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1000 then Restart-Service winrm –  Feb 06 '12 at 20:46
  • @Kiquenet i'll post the whole source code for this monday or tuesday when we start back to work :) –  May 26 '12 at 20:35

2 Answers2

4

This line fixed it.

pipeline.Commands.AddScript("$sessions = New-PSSession -ComputerName " + host + Environment.NewLine 
+ "Invoke-Command -session $sessions -ScriptBlock {cd " + uploadtodir+"}" + Environment.NewLine
+"Invoke-Command -session $sessions -ScriptBlock {"+command+ "}" + Environment.NewLine 
+ "Remove-PSSession -Session $sessions" + Environment.NewLine 
+ "exit" + Environment.NewLine + "exit" + Environment.NewLine);
halfbit
  • 54,462
  • 46
  • 195
  • 426
0

It may be that since CD is an Alias you can try using Set-Location (the full CMDlet) and see if that helps. I would err on not using Aliases as I am not sure what environment gets setup when creating a pipeline via that method.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
alistek
  • 391
  • 1
  • 4
  • i tried Set-location but there was no change thanks for the help though –  Feb 06 '12 at 19:11