我有下一段代码可以调用远程PowerShell脚本(脚本进入远程系统),但我想将参数发送到脚本:
c#方法:
public void RemoteConnection()
{
connectionInfo = new WSManConnectionInfo(false, remoteMachineName, 5985, "/wsman", shellUri, credentials);
runspace = RunspaceFactory.CreateRunspace(connectionInfo);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline(path);
var results = pipeline.Invoke();
foreach (PSObject obj in results)
Console.WriteLine(obj.ToString());
}
我尝试使用CommandParameter发送参数但是我收到了一条错误消息:
Pipeline pipeline = runspace.CreatePipeline();
Command myCommand = new Command(path);
CommandParameter testParam0 = new CommandParameter("suma");
myCommand.Parameters.Add(testParam0);
CommandParameter testParam = new CommandParameter("x", "89");
myCommand.Parameters.Add(testParam);
CommandParameter testParam2 = new CommandParameter("y", "11");
myCommand.Parameters.Add(testParam2);
pipeline.Commands.Add(myCommand);
错误讯息:
{"Cannot perform operation because operation \"NewNotImplementedException at offset 76 in file:line:column <filename unknown>:0:0\r\n\" is not implemented."}
我可以用这种方式调用我的powershell脚本(进入我的远程系统):
PS C:\grace\powershell> .\script1.ps1 -suma -x 9 -y 19
28
PS C:\grace\powershell> .\script1.ps1 -suma "9" "19"
28
如何通过我的powershell脚本的c#程序参数发送?
答案 0 :(得分:1)
它对我有用:
public void RemoteConnection()
{
connectionInfo = new WSManConnectionInfo(false, remoteMachineName, 5985, "/wsman", shellUri, credentials);
runspace = RunspaceFactory.CreateRunspace(connectionInfo);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline(path);
Command myCommand = new Command(path);
CommandParameter testParam0 = new CommandParameter("-suma");
myCommand.Parameters.Add(testParam0);
CommandParameter testParam = new CommandParameter("x", "34");
myCommand.Parameters.Add(testParam);
CommandParameter testParam2 = new CommandParameter("y", "11");
myCommand.Parameters.Add(testParam2);
pipeline.Commands.Add(myCommand);
var results = pipeline.Invoke();
foreach (PSObject obj in results)
Console.WriteLine(obj.ToString());
}
注意我将路径发送到CreatePipeline并创建新命令(可能需要进一步审核)
答案 1 :(得分:0)