【问题标题】:How to make a .ps1 file with multiple commands and Set-ExecutionPolicy Unrestricted -force?如何使用多个命令和 Set-ExecutionPolicy Unrestricted -force 制作 .ps1 文件?
【发布时间】:2016-02-19 12:07:46
【问题描述】:
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"""; */

这些基本上是我想要使用的基本样式代码命令。我一直有一个问题-executionpolicy unrestricted。当我像上面一样在 powershell 中键入它时效果很好。

另外,powershell 中与 cmd.exe /k 类似的编码是什么,以便命令提示符窗口保持打开以进行故障排除?

【问题讨论】:

    标签: c# powershell command administrator


    【解决方案1】:

    Answer found on Stackoverflow(click here)

    ...
    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!");
    }
    

    【讨论】:

      【解决方案2】:

      看到您的错误后,我可以确定该错误来自您正在运行的sfc 命令。命令正在执行中。

      执行策略是关于如何处理脚本文件,因此它对立即运行的命令没有影响(除非您在命令中引用脚本文件)。恐怕这对您的特定问题没有帮助。


      是什么让您认为您的执行策略不起作用?

      由于您没有在未注释的代码中使用-File 参数,因此无论如何执行策略都应该是无关紧要的。

      cmd.exe /k 的类似 powershell 命令是 powershell.exe -NoExit

      【讨论】:

      • "Windows Resource Protection 无法启动修复服务" 这就是显示的错误。我认为 -executionpolicy unrestricted 可以解决这个问题。
      • 我关闭了受信任的安装程序“net stop TrustedInstaller”并认为这可能会有所帮助,但仍然存在“SFC /SCANNOW”的问题
      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 2021-11-25
      • 2012-05-25
      • 2019-10-19
      • 2013-11-24
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多