0

I am writing a plug-in for a software using C#. My plug-in generates a directory, containing a couple of text files, that I need to transfer to a computational cluster. At the moment I am transferring the files manually with the PowerShell (or the Ubuntu plug-in for Windows) using "scp -r -P location/on/windows location/on/cluster", but I would like to do this step automatically by including it in my plug-in.

I have looked around online for guidance and I have found some helpful documentation, but I am still quite lost.

So far I've managed to open the shell, but how to add a sequence of commands I still don't understand.

  if(x)
  }
      OpenPowerShell(@"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe");
  }


  static void OpenPowerShell(string path)
  {
    Process.Start(path);
  }

Could someone please help me out with this or point to some helpful documentation?

Thanks, David

  • Ideally, you should use the [PowerShell Class](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.powershell?view=pscore-6.2.0) to execute PowerShell commands from C#. – boxdog Jan 14 '20 at 16:04
  • @boxdog I am writing a plugin for Grasshopper using Visual Studio and when I try `Powershell.Create(...);` it is not recognized, what am I missing here? – David Andersson Jan 14 '20 at 16:08
  • Did you include `System.Management.Automation.dll` in your project? You can get a copy by running this in the PowerShell console: `Copy ([PSObject].Assembly.Location) "$env:USERPROFILE\Desktop"` – boxdog Jan 14 '20 at 16:11
  • @boxdog that did the trick! Now I just need to figure out how to use this class;) – David Andersson Jan 14 '20 at 16:17
  • Have you thought of using c# version of the code instead of bringing powershell in the mix? like. `Directory.Move(sourceDirName, destDirName);`. If you have to call powershell, then look into using RunspaceFactory and Pipeline – Jawad Jan 14 '20 at 16:49
  • Does this answer your question? [Execute PowerShell Script from C# with Commandline Arguments](https://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments) – Jawad Jan 14 '20 at 16:52
  • @Jawad I first thought so but it actually doesn't. My problem is that I am developing the code for Grasshopper so as far as I understand I need to use the `Process.Start()` and not the dedicated powershell class that comes with C#. – David Andersson Jan 15 '20 at 09:33

0 Answers0