简体   繁体   English

从C#调用远程powershell命令

[英]Invoke remote powershell command from C#

I'm trying to run an invoke-command cmdlet using C# but I can't figure out the right syntax. 我正在尝试使用C#运行invoke-command cmdlet,但我无法找出正确的语法。 I just want to run this simple command: 我只想运行这个简单的命令:

invoke-command -ComputerName mycomp.mylab.com -ScriptBlock {"get-childitem C:\windows"}

In C# code, I have done the following: 在C#代码中,我完成了以下操作:

InitialSessionState initial = InitialSessionState.CreateDefault();
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddCommand("invoke-command");
ps.AddParameter("ComputerName", "mycomp.mylab.com");
ps.AddParameter("ScriptBlock", "get-childitem C:\\windows");
foreach (PSObject obj in ps.Invoke())
{
   // Do Something
}

When I run this, I get an exception: 当我运行它时,我得到一个例外:

Cannot bind parameter 'ScriptBlock'. Cannot convert the "get-childitem C:\windows" value of type "System.String" to type "System.Management.Automation.ScriptBlock".

I'm guessing I need to use ScriptBlock type here somewhere, but don't know how to. 我猜我需要在某处使用ScriptBlock类型,但不知道该怎么做。 This is just a simple example to get started with, the real use case would involve running a larger script block with multiple commands in it, so any help on how to do this would be highly appreciated. 这只是一个简单的示例,真正的用例将涉及运行一个包含多个命令的更大的脚本块,因此任何有关如何执行此操作的帮助都将受到高度赞赏。

Thanks 谢谢

Ah, the parameter to ScriptBlock itself needs to be of type ScriptBlock. 啊,ScriptBlock本身的参数需要是ScriptBlock类型。

full code: 完整代码:

InitialSessionState initial = InitialSessionState.CreateDefault();
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddCommand("invoke-command");
ps.AddParameter("ComputerName", "mycomp.mylab.com");
ScriptBlock filter = ScriptBlock.Create("Get-childitem C:\\windows");
ps.AddParameter("ScriptBlock", filter);
foreach (PSObject obj in ps.Invoke())
{
   // Do Something
}

Putting the answer here if someone finds it useful in the future 如果有人发现将来有用,请在此处给出答案

scriptblock字符串应与“{...}”格式匹配。使用以下代码即可:

ps.AddParameter("ScriptBlock", "{ get-childitem C:\\windows }");

你使用短格式:

ps.AddParameter("ScriptBlock", ScriptBlock.Create("Get-childitem C:\\Windows"));

An alternative approach in case it maybe more appropriate in some cases. 如果在某些情况下可能更合适,则采用另一种方法。

        var remoteComputer = new Uri(String.Format("{0}://{1}:5985/wsman", "HTTP", "ComputerName"));
        var connection = new WSManConnectionInfo(remoteComputer, null, TopTest.GetCredential());

        var runspace = RunspaceFactory.CreateRunspace(connection);
        runspace.Open();

        var powershell = PowerShell.Create();
        powershell.Runspace = runspace;

        powershell.AddScript("$env:ComputerName");

        var result = powershell.Invoke();

https://blogs.msdn.microsoft.com/schlepticons/2012/03/23/powershell-automation-and-remoting-ac-love-story/ https://blogs.msdn.microsoft.com/schlepticons/2012/03/23/powershell-automation-and-remoting-ac-love-story/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 PSCmdlet 类 (C#) 在主机 Powershell 中调用原始命令 - Invoke Raw Command in Host Powershell from PSCmdlet class (C#) 使用不同的凭据从C#调用Powershell命令 - Invoke Powershell command from C# with different credential Powershell addscript和从C#调用不在命令行上显示结果 - Powershell addscript and invoke from C# not displaying result on command line Powershell 调用无效命令 output C# - Powershell Invoke invalid command output C# 我如何从c#调用使用“图形命令窗口”(显示命令)的Powershell - How can i invoke powershell that uses “graphical command window” (show-command) from c# 使用C#执行远程交换Powershell命令 - Execute remote exchange powershell command with C# 如何从C#中的PowerShell命令获取Invoke-Command详细信息或调试输出 - How to get Invoke-Command verbose or debug output from a PowerShell command in C# 如何从 c# 调用带有“格式列表”和“输出文件”管道的 powershell 命令? - how to invoke the powershell command with “format-list” and “out-file” pipeline from c#? Powershell Invoke() 在 C# 中为 bolt 命令运行返回零 - Powershell Invoke() returning zero in C# for bolt command run Powershell调用命令ASPX / C#与命令行 - Powershell Invoke-Command ASPX/C# vs. Commandline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM