Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从WPF应用程序启用PS ExecutionPolicy_C#_Wpf_Powershell - Fatal编程技术网

C# 从WPF应用程序启用PS ExecutionPolicy

C# 从WPF应用程序启用PS ExecutionPolicy,c#,wpf,powershell,C#,Wpf,Powershell,我想从脚本本身将powershell执行设置为不受限制 我在控制台应用程序中有工作版本。它看起来像这样: public static void SetPowerShellExecuteUnrestricted() { try { RunspaceInvoke scriptInvoker = new RunspaceInvoke(); scriptInvoker.Invoke("Set-ExecutionPol

我想从脚本本身将powershell执行设置为不受限制

我在控制台应用程序中有工作版本。它看起来像这样:

public static void SetPowerShellExecuteUnrestricted()
    {
        try
        {
            RunspaceInvoke scriptInvoker = new RunspaceInvoke();
            scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }
string strCmdText = "/C PowerShell.exe Set-ExecutionPolicy Unrestricted";
            Process.Start("CMD.exe", strCmdText);
ps.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted").AddParameter("Scope", "CurrentUser");
但现在我需要在WPF应用程序中使用相同的方法。上面的代码不起作用。 所以我尝试了不同的版本,例如: 通过RunspaceFactory:

Runspace rs;
            rs = RunspaceFactory.CreateRunspace();
            rs.Open();

            using (PowerShell ps = PowerShell.Create())
            {
                ps.AddCommand("Set-ExecutionPolicy Unrestricted");
                ps.Runspace = rs;
                ps.Invoke();
            }
这将捕获异常:“Set ExecutionPolicy”未被识别为cmdlet、函数、脚本文件或可操作程序的名称。但此cmdlet通常在powershell控制台中工作

我甚至试着在windows cmd上这样做:

public static void SetPowerShellExecuteUnrestricted()
    {
        try
        {
            RunspaceInvoke scriptInvoker = new RunspaceInvoke();
            scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }
string strCmdText = "/C PowerShell.exe Set-ExecutionPolicy Unrestricted";
            Process.Start("CMD.exe", strCmdText);
ps.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted").AddParameter("Scope", "CurrentUser");
但通过这种方式,cmd窗口只做一次闪存,然后断开纸张,但不会更改执行策略

是否有人处理过类似的问题并能提供帮助

谢谢

//编辑:2019年8月25日21:10 又尝试了一件事,但没有成功:

void CreateScript()
            {
                string path = @"C:\Windows\Temp\ExecPolicy.ps1";
                if (!File.Exists(path))
                {
                    using (StreamWriter sw = new StreamWriter(File.Open(path, FileMode.CreateNew), Encoding.Unicode))
                    {
                        sw.WriteLine("Set-ExecutionPolicy Unrestricted");
                    }
                }

            }

            void RunPowershellScript(string PathToScript)
            {
                string strCmdText;
                strCmdText = PathToScript;
                Process.Start("C:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe ", strCmdText);
            }

            CreateScript();
            System.Threading.Thread.Sleep(2000);
            RunPowershellScript(@"C:\Windows\Temp\ExecPolicy.ps1");
使用脚本创建文件,然后使用powershell运行它。但没有成功。您无法运行它,因为该ExecutionPolicy已禁用它

//编辑:2019年8月25日21:55

好了,伙计们,我知道了

以下是解决方案: 您需要制定如下命令:

public static void SetPowerShellExecuteUnrestricted()
    {
        try
        {
            RunspaceInvoke scriptInvoker = new RunspaceInvoke();
            scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }
string strCmdText = "/C PowerShell.exe Set-ExecutionPolicy Unrestricted";
            Process.Start("CMD.exe", strCmdText);
ps.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted").AddParameter("Scope", "CurrentUser");

很高兴听到你找到了解决办法。为了让其他人能更容易地找到它,请把它作为一个答案,最好是一个解释。48小时后,你将能够接受自己的答案,以指导未来的读者。很高兴听到你找到了解决方案。为了让其他人能更容易地找到它,请把它作为一个答案,最好是一个解释。48小时后,你将能够接受自己的答案,以指导未来的读者。