简体   繁体   English

如何使用多个命令和Set-ExecutionPolicy Unrestricted -force制作.ps1文件?

[英]How to make a .ps1 file with multiple commands and Set-ExecutionPolicy Unrestricted -force?

var newProcessInfo = new System.Diagnostics.ProcessStartInfo();
newProcessInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
newProcessInfo.Verb = "runas";
newProcessInfo.Arguments = @"-executionpolicy unrestricted -Command ""C:\Windows\system32\sfc.exe /scannow""";


/*
newProcessInfo.Arguments = @"-Command ""sfc /scannow""";
newProcessInfo.Arguments = @"-File ""C:\my\script.ps1""";
newProcessInfo.Arguments = @"–ExecutionPolicy Bypass -File ""C:\my\script.ps1"""; */

These are basically the basic style code commands I want to use. 这些基本上是我要使用的基本样式代码命令。 I have been having a problem -executionpolicy unrestricted. 我遇到了一个问题-执行策略不受限制。 It works great when I type it in powershell just like above. 当我像上面一样在powershell中键入它时,它的效果很好。

Also, what is the comparable coding in powershell to cmd.exe /k so the command prompt window stays open for troubleshooting? 另外,powershell中与cmd.exe / k相类似的编码是什么,因此命令提示符窗口保持打开状态以进行故障排除?

After seeing your error, I can say for sure that that error is coming frm the sfc command that you're running. 看到您的错误后,我可以肯定地说该错误是您正在运行的sfc命令引起的。 The command is being executed. 该命令正在执行。

Execution Policy is about how to handle script files, so it has no effect on commands run immediately (unless you reference a script file in a command). 执行策略是关于如何处理脚本文件的,因此它对立即运行的命令没有影响(除非您在命令中引用了脚本文件)。 It won't help with your particular issue I'm afraid. 恐怕您的特定问题无济于事。


What makes you think your execution policy is not working? 是什么让您认为执行策略不起作用?

Since you're not using the -File parameter in the uncommented code, execution policy should be irrelevant anyway. 由于您未在未注释的代码中使用-File参数,因此无论如何执行策略都应该无关紧要。

The analogous powershell command for cmd.exe /k is powershell.exe -NoExit . cmd.exe /k的类似powershell命令是powershell.exe -NoExit

Answer found on Stackoverflow(click here) 在Stackoverflow上找到答案(单击此处)

...
using System.Diagnostics;
...

private void button4_Click(object sender, EventArgs e)
{
    string docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    string snat = Environment.GetEnvironmentVariable("windir") + @"\sysnative\sfc.exe";

    Process a = new Process();
      a.StartInfo.FileName = snat;
      a.StartInfo.Arguments = "/SCANNOW";
      a.StartInfo.UseShellExecute = false;
      a.StartInfo.RedirectStandardOutput = true;
      a.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      a.StartInfo.CreateNoWindow = true;
    a.Start();

    string output = a.StandardOutput.ReadToEnd();
    a.WaitForExit();

    MessageBox.Show("DONE!");
}
catch
{
MessageBox.Show("error!");
}

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

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