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:
- PowerShell starts and loads as Admin
- PowerShell window immediately closes (apparently) without error
Notes:
- The script works. When I run it from ISE, it runs without errors.
- If I right click the script and choose Run with PowerShell, I get a Execution Policy error even though I don't change it in the script.
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:
- It ran fine a few days ago
- It runs fine on other computers
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:\windows\system32\windowspowershell\v1.0\powershell.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.
-NoProfile -ExecutionPolicy UnRestricted
? – bot_insaneCopy-Item
call so you can trace if it actually does something? Sadly, cannot test the C# code, although I can emulate it in Powershell. EDIT: emulated and in case of my PC, Powershell->Powershell did work with a test script, that is, it waited forRead-Host
to provide data. – Vesper