首页 文章

从c#运行Powershell脚本

提问于
浏览
8

我正在尝试从C#代码运行PowerShell脚本,但我遇到了一些(可能是环境)问题:

在我尝试运行它的机器上,会发生以下情况:

  • PowerShell以Admin身份启动并加载

  • PowerShell窗口立即关闭(显然)没有错误

注意:

  • 脚本有效 . 当我从ISE运行它时,它运行没有错误 .

  • 如果我右键单击脚本并选择Run with PowerShell,即使我没有在脚本中更改它,我也会收到执行策略错误 .


Set-ExecutionPolicy:Windows PowerShell已成功更新执行策略,但该设置被更具体范围内定义的策略覆盖 . 由于覆盖,您的shell将保留其当前有效的RemoteSigned执行策略 . 键入“Get-ExecutionPolicy -List”以查看执行策略设置 . 有关详细信息,请参阅“Get-Help Set-ExecutionPolicy” . 在行: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
  • 我认为这是环境因素:

  • 几天前它运行良好

  • 它在其他计算机上运行正常

这是我用来调用脚本的代码:

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();
}

脚本本身是无关紧要的,因为我已将其更改为简单

Write-Host "Hello"
$d=Read-Host

我有同样的问题 .

2 回答

  • 3

    问题出在脚本的路径上 . 它在这台特定的机器上有空间,我没有处理过 .

    窗口关闭太快,看不到任何错误,但设置

    process.StartInfo.RedirectStandardOutput = true;
    

    帮助我 grab 它 .

    执行策略与我的错误无关 .

    为了解决这个问题,我改变了c#代码中的路径,如下所述:Executing a Powershell script in CMD.EXE from a location with "Illegal characters in path"

    完整代码:

    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:\windows\system32\windowspowershell\v1.0\powershell.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);
                    }
    
                }
    
  • 0

    当您列出策略时,显然存在针对您的计算机实施的组策略(即使它仍然是本地GP仍然有效),它将 MachinePolicy 更改为"RemoteSigned",这比"RemoteSigned"更加强大,这比"Unrestricted"更强 . 代码执行条款 . 如果您的PC位于域中,则可以以本地管理员身份运行"Resultant set of policy (Logging)"并获取影响您的PC的域策略 . 如果没有,请从“控制面板”/“管理工具”运行本地安全策略并导航"Computer configuration - Administrative Templates - Windows Components - Windows PowerShell"并检查其中的策略值("Enable scenario execution" - 大致从本地化翻译),如果需要,您可以更改其中的值 . 完成后,重新加载Powershell .

相关问题