Execute powershell script on a remote computer usi

2020-03-30 04:50发布

I have created a Bot Application using Microsoft Bot Framework and want to achieve the following:

  1. To execute a Powershell script on remote computers without any authentication
  2. The powershell scripts will be hosted on Azure or on a Database (may be any)

I have done following till now:

  1. Was able to execute Powershell scripts on remote computers manually
  2. Was able to execute Powershell scripts locally using C# code

Below is my current code:

WSManConnectionInfo connectioninfo = new WSManConnectionInfo();
connectioninfo.ComputerName = "<remote computer hostname>";
Runspace runspace = RunspaceFactory.CreateRunspace(connectioninfo); 
//runspace.Open();
using (PowerShell ps = PowerShell.Create())
{ 
   var re = ps.AddScript("Get-Service"); 
   var results = re.Invoke(); 
}

1条回答
够拽才男人
2楼-- · 2020-03-30 05:19

I think that you are missing the bind of your PowerShell instance to the runspace.

ps.Runspace = runspace;

Take a look to this for more details.

Alternatively, you can create a Pipeline (as explained here) using the runspace and invoke the commands.

Pipeline pipeline = runspace.CreatePipeline("<COMMAND TO EXECUTE REMOTELY>");

var results = pipeline.Invoke();
查看更多
登录 后发表回答