Hi,
I created a c# executable that calls a powershell cmdlet through powershell.invoke(). The method returns an error "The Remote Procedure Call Failed". However the commandline string for the cmdlet and its parameters is fine in Windows Powershell.
Here is the successfull commandline string in Windows Powershell: Set-PrinterProperty -PrinterName "My v4 Printer" -PropertyName "Config:DuplexUnit" -Value "FALSE".
Here is the equivalent call in c# code:
using (PowerShell powershell = PowerShell.Create())
{
powershell.AddCommand("Set-PrinterProperty");
powershell.AddParameter("PrinterName", "My v4 Printer");
powershell.AddParameter("PropertyName", "Config:DuplexUnit");
powershell.AddParameter("Value", "FALSE");
powershell.Invoke();
if (powershell.HadErrors)
foreach (var error in powershell.Streams.Error)
{
Console.WriteLine("Error: " + error);
}
}
I tried running my executable as administrator via right-click.... Run As Administrator. But it still failed. It seems like the problem is not a process security and access rights issue. Can someone suggest what is missing in the code? Thanks in advance.