Home

Invoking a PowerShell Script from LINQ

edited October 2012
Hello,

Is it possible to invoke a PowerShell script a server with C# from within LINQPad?

Specifically, I would like to run a PowerShell command like this:
(new-object Net.WebClient).DownloadString('http://anywebsite.net/release/powershellscript.ps1') | iex

Many thanks.

Kind regards,
Dominik

Comments

  • edited October 2012
    There's nothing out of the box, although you could write a method in 'My Extensions' easily enough to help with this, using the PS management types.

    For instance, the following executes the Get-Process command and dumps the results to a data grid with hyperlinks that you can explore further. You could use as a starting poing to write a PSExec method:

    var runspaceConfiguration = RunspaceConfiguration.Create(); var runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); runspace.Open(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace); Pipeline pipeline = runspace.CreatePipeline(); Command myCommand = new Command("Get-Process"); pipeline.Commands.Add(myCommand); pipeline.Invoke().Select (pso => pso.BaseObject).Dump(true);
  • Joe,

    Many thanks for your support, I appreciate it very much.

    I wasn't far away from your solution...

    Regards,
    Dominik
Sign In or Register to comment.