我有具有以下命令的脚本文件sample.ps1:如何将参数传递给C#程序的powershell脚本?
copy-item C:\source -destination C:\destination.
而不是硬编码的源和目标我想将它作为参数传递给脚本的价值。
copy-item $source -destination $destination.
我想从独立客户端调用此脚本,并将源和目标作为参数传递。 我有以下程序来执行脚本文件:
string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
string userName = "MachineName\\Administrator";
string password = "Password";
SecureString securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
PSCredential credential = new PSCredential(userName, securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "machinename", 5985, "/wsman", shellUri, credential);
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
String file = "C:\\scripts\\Sample.ps1";
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(System.IO.File.ReadAllText(file));
}
我想通过C#程序来传递参数给脚本Sample.ps1。这怎么可能?
FYI:我还没有尝试过。我基于[这个问题](http://social.technet.microsoft.com/Forums/en-US/0c8b4d50-e87c-48f6-807c-f338a32103fc/powershell-pass-connection-string-as-a-参数换一个脚本)。它应该工作,但我仍然猜测。 – quetzalcoatl
我试过上面的方法没有参数,但我得到以下错误:'未处理的异常:System.Management.Automation.RemoteException:术语'C:\ scripts \ Sample.ps1'不被识别为cmdlet的名称,功能,文件或可操作程序。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。 –