【发布时间】:2014-01-07 18:41:36
【问题描述】:
我有一个脚本文件 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。这怎么可能?
【问题讨论】:
标签: c# powershell parameter-passing