Question

I have a problem with c# code which invokes a command on other computer using powershell. When I enter it into the Powershell, works fine, but when I try to invoke it using C# UI application, it throws an exception Method or operation not implemented. Here is the code:

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();

And here is the part of powershell script which handles parameters:

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")
)

Help, please! Thanks!

Was it helpful?

Solution

If I'm understanding this correctly, you're invoking the command on the remote machine. However, you're using a file containing the command. When Invoke gets called, it's trying to find .\remoteSchedule.ps1 on the remote system, and failing miserably.

Trivial Solution is to ensure remoteSchedule.ps1 is on a drive that all the systems can access, and you make sure the ExecutionPolicy is set to allow unsigned scripts.

Non-trivial is to rewrite the entire script to do everything in the script (like a scriptblock), and Invoke that instead of the file directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top