我对使用 powershell 在其他计算机上调用命令的 c# 代码有疑问。当我将它输入到 Powershell 时,工作正常,但是当我尝试使用 C# UI 应用程序调用它时,它会引发异常Method or operation not implemented.
这是代码:
Command cmd = new Command(@".\remoteSchedule.ps1");
//determine if there is computer name or IP address
if (Char.IsDigit(cboMachine.Text.ToString().FirstOrDefault()))
cmd.Parameters.Add("ComputerName", cboScheduleMachine.Text);
else
cmd.Parameters.Add("ComputerName", cboScheduleMachine.Text + "." + ConfigurationManager.AppSettings["Domain"]);
cmd.Parameters.Add("UserName", @txtScheduleUsername.Text);
cmd.Parameters.Add("Password", txtSchedulePassword.Text);
cmd.Parameters.Add("TaskName", "\"" + txtScheduleTaskName.Text + "\"");
cmd.Parameters.Add("Command", "\"" + @txtScheduleCommand.Text + "\"");
cmd.Parameters.Add("arguments", "\"" + txtScheduleArguments.Text + "\"");
cmd.Parameters.Add("startDate", dpkScheduleStartDate.SelectedDate.Value.ToString("MM-dd-yyyy"));
cmd.Parameters.Add("startTime", txtScheduleStartTime.Text);
cmd.Parameters.Add("workingDirectory", "\"" + @txtWorkingFolder.Text + "\"");
cmd.Parameters.Add("run", chkScheduleRun.IsChecked);
pipeline.Commands.Add(cmd);
// execute the script
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(DateTime.Now.ToString() + ": ");
stringBuilder.AppendLine(obj.ToString());
}
txtScheduleOutput.SelectionStart = 0;
txtScheduleOutput.Text = stringBuilder.ToString();
这是处理参数的powershell脚本的一部分:
Param(
[string]$ComputerName = $(throw "Missing parameter ComputerName"),
[string]$UserName = $(throw "Missing parameter UserName"),
[string]$Password = $(throw "Missing parameter Password"),
[string]$TaskName = $(throw "Missing parameter taskName"),
[string]$Command = $(throw "Missing parameter Command"),
[string]$arguments = $(throw "Missing parameter arguments"),
[datetime]$startDate = $(throw "Missing parameter startDate"),
[string]$startTime = $(throw "Missing parameter startTime"),
[string]$workingDirectory = $(throw "Missing parameter workingDirectory"),
[bool]$run = $(throw "Missing parameter run")
)
请帮忙!谢谢!