Hi.
I am having trouble running a powershell script saved in another server.
I need to execute the powershell script by C#.
Server 1 is where the C# program is saved
Server 2 is where the powershell script is saved.
Here is my c# method
where variable
serviceList
is the parameter/argument to be passed to the powershell script.
variable
psPath
will contain the string like "\\[serveripaddress]\g$\adminscripts\dotalltool_scripts\stopservice.ps1
public string runScriptPS(string serviceList, string operation)
{
string psFileName = operation == "Stop" ? "stopservice.ps1" : operation == "Start" ? "startservice.ps1" : "restartservice.ps1";
string server = hdnServerSelected.Value;
string psPath = @"\\" + server + @"\g$\AdminScripts\dotalltool_scripts\" + psFileName;
string paramSapArray = (char)34 + serviceList + (char)34;
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
Command psCmd = new Command(psPath);
CommandParameter sapParam = new CommandParameter("SAPArray", paramSapArray);
psCmd.Parameters.Add(sapParam);
pipeline.Commands.Add(psCmd);
Collection<PSObject> results = pipeline.Invoke("Set-ExecutionPolicy Unrestricted");
StringBuilder sb = new StringBuilder();
foreach (PSObject obj in results)
{
sb.AppendLine(obj.ToString());
}
string a = sb.ToString();
return sb.ToString();
}
Here is the error that I am getting
File \\serveripaddress\g$\AdminScripts\dotalltool_scripts\stopservice.ps1 cannot be loaded. The file \\serveripaddress\g$\AdminScripts\dotalltool_scripts\stopservice.ps1 is not digitally signed. You cannot run this script on the current system. For more information about running scripts and setting execution policy, see about_Execution_Policies at http:
What I have tried:
I already indicated execution policy on the pipeline.Invoke()