Running a Powershell script from c#
I'm trying to run a PowerShell script from C# code, but I'm having some (maybe environmental) issues:
On a machine where I try to run it, the following occur:
Notes:
Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope. Due to the override, your shell will retain its current effective execution policy of RemoteSigned. Type "Get-ExecutionPolicy -List" to view your execution policy settings. For more information please see "Get-Help Set-ExecutionPolicy". At line:1 char:46 + if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy Unrestricted
UserPolicy Undefined
Process Bypass
CurrentUser Unrestricted
LocalMachine Unrestricted
I believe that this is environmental because:
This is the code I use to invoke the script:
if (File.Exists("Start.ps1"))
{
string strCmdText = Path.Combine(Directory.GetCurrentDirectory(), "Start.ps1");
var process = System.Diagnostics.Process.Start(@"C:windowssystem32windowspowershellv1.0powershell.exe ", strCmdText);
process.WaitForExit();
}
The script itself is irrelevant, as I have changed it to a simple
Write-Host "Hello"
$d=Read-Host
and I have the same issue.
The problem was in the path of the script. It had spaces on this particular machine and I had not handled that.
The window closed too fast to see any error but setting
process.StartInfo.RedirectStandardOutput = true;
helped me catch it.
The execution policy had nothing to do with my error.
To fix it I changed the path in the c# code like explained here: Executing a Powershell script in CMD.EXE from a location with "Illegal characters in path"
Complete code:
if (File.Exists("Start.ps1"))
{
File.GetAttributes("Start.ps1");
string strCmdText = Path.Combine(Directory.GetCurrentDirectory(), "Start.ps1");
var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = @"C:windowssystem32windowspowershellv1.0powershell.exe";
process.StartInfo.Arguments = ""&'"+strCmdText+"'"";
process.Start();
string s = process.StandardOutput.ReadToEnd();
process.WaitForExit();
using (StreamWriter outfile = new StreamWriter("StandardOutput.txt", true))
{
outfile.Write(s);
}
}
As you list the policies, apparently there is a group policy implemented against your computer (even if it's not in the domain, there's still local GP in effect) that changes MachinePolicy
which is above all the locally set policies to "RemoteSigned", which is more strong policy than "Unrestricted" in terms of code execution. If your PC is in a domain, you can run "Resultant set of policy (Logging)" as local administrator and get the domain policy that affects your PC. If not, run Local Security Policy from Control Panel/Administrative Tools and navigate "Computer configuration - Administrative Templates - Windows Components - Windows PowerShell" and check the value of policies there ("Enable scenario execution" - roughly translated from localized), and if needed, you can change the value in there. After you do, reload Powershell.
下一篇: 从c#运行Powershell脚本