2

我想在远程机器上运行 Powershell 命令。这是我正在使用的方法(localhost:131 是因为我使用隧道到远程机器的端口 5985):

  public string RunRemotePowerShellCommand(string command)
    {
           System.Security.SecureString password = new System.Security.SecureString();
            foreach (char c in _password.ToCharArray())
            {
                password.AppendChar(c);
            }

            string schema = "http://schemas.microsoft.com/powershell/Microsoft.Powershell";

            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false,
                "localhost", 131, "/wsman", schema, new PSCredential(_domain + @"\" + _userName, password));

            using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
            {
                remoteRunspace.Open();
                using (PowerShell powershell = PowerShell.Create())
                {
                    powershell.Runspace = remoteRunspace;
                    powershell.AddCommand(command);
                    powershell.Invoke();

                    Collection<PSObject> results = powershell.Invoke();

                    // convert the script result into a single string
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (PSObject obj in results)
                    {
                        stringBuilder.AppendLine(obj.ToString());
                    }
                    return stringBuilder.ToString();
                }
            }
    }

我正在尝试运行以下命令:

D:\FolderName\scriptName.ps1 -action editbinding -component "comp1","comp2","comp3","comp4"

像这样:

RunRemotePowerShellCommand(@"D:\FolderName\scriptName.ps1 -action editbinding -component ""comp1"",""comp2"",""comp3"",""comp4""");

但我得到:

Error: System.Management.Automation.RemoteException: The term 'D:\FolderName\scriptName.ps1 -action editbinding -component "comp1","comp2","comp3","comp4"' is not recognized as a name of cmdlet, function, script file, or operable program. Check the spelling of the name, or if the path is included, verify that the path is correct and try again.

该方法适用于简单的命令,并且当我在远程机器上运行它时,我想要运行的命令是可以的。

提前致谢。

问候,杜桑

4

2 回答 2

0

您需要使用该powershell.AddParameter()方法为您的命令添加参数。AddCommand() 调用应该只命名命令:cmdlet 名称、函数名称、脚本路径等。来自文档:

PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-Process");
ps.AddArgument("wmi*");
ps.AddCommand("Sort-Object");
ps.AddParameter("descending");
ps.AddArgument("id");
于 2012-04-17T16:14:29.020 回答
0

我有类似的要求。

我的解决方案是在 C# 代码中创建一个 powershell 函数,并在 powershell 远程会话中使用它。

using System;
using System.Management.Automation;

namespace PowerShellTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string func = @"function Test { Write-Host 'hello' };";
            PowerShell ps = PowerShell.Create();
            ps.AddScript(func);
            ps.Invoke();
            ps.AddCommand("Test");
            ps.Invoke();
            Console.WriteLine("Successfully executed function");
            Console.ReadLine();
        }
    }
}
于 2014-01-19T23:58:53.483 回答