简体   繁体   English

使用 Process.Start 启动 PowerShell 脚本会关闭 PowerShell 且没有错误

[英]Starting PowerShell Script with Process.Start closes PowerShell with no error

I'm writing a small app in C# that calls various PowerShell scripts to configure a computer.我正在 C# 中编写一个小应用程序,它调用各种 PowerShell 脚本来配置计算机。

I'm having an issue now that I don't think I was having just a couple weeks back when I worked on it last.我现在遇到了一个问题,我认为我上次处理它时只是几周前。

When I call these scripts from C# a PowerShell window opens extremely briefly and then closes.当我从 C# 调用这些脚本时,PowerShell window 打开非常短暂,然后关闭。 I thought perhaps there was an error in my PS script, but no- if I do a Start-Transcript at the beginning I still get no output, so it's clearly not even starting to run my scripts.我想我的 PS 脚本可能有错误,但是没有——如果我在开始时执行 Start-Transcript,我仍然没有得到 output,所以它显然甚至没有开始运行我的脚本。

My code:我的代码:

Process.Start(@"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe ", @"C:\Thunder\Scripts\3-BitLockerEnable.ps1");

I thought maybe Execution Policy was the problem, but it's set to unrestricted.我想也许执行策略是问题所在,但它设置为不受限制。 I made sure the file path was right- simply removing the PowerShell call lets me check, and it opens the ps1 file in Notepad fine without that line.我确保文件路径是正确的——只需删除 PowerShell 调用就可以让我检查,它在没有该行的情况下在记事本中打开 ps1 文件。

Any help appreciated, I'm not sure where I've made a mistake.任何帮助表示赞赏,我不确定我在哪里犯了错误。 I could have sworn this exact code was working fine a couple weeks ago.我可以发誓这个确切的代码在几周前运行良好。 Thanks.谢谢。

Use the built in PowerShell object:使用内置的 PowerShell object:

PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"C:\Thunder\Scripts\3-BitLockerEnable.ps1")).Invoke();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM