我有一个脚本文件sample.ps1,其中包含以下命令:
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。这是否可行?