调用 Set-ExecutionPolicy 时如何抑制来自脚本的警告消息 [英] How to suppress warning message from script when calling Set-ExecutionPolicy

查看:49
本文介绍了调用 Set-ExecutionPolicy 时如何抑制来自脚本的警告消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本来在使用前准备我们的笔记本电脑.它本质上是安装证书并将执行策略设置为 AllSigned.

I am writing a script to prepare our laptops before use. It essentially installs certificates and sets the executionpolicy to AllSigned.

它通过鼠标右键单击和使用 Powershell 运行"来执行.这是执行策略的 Windows 10 标准绕过,让脚本在未经修改的 Windows 10 机器上运行(至少在我看来是这样).所以我可以执行我的脚本而无需显式更改执行策略.
脚本运行后,机器就设置好了.我只是收到一条警告,我想取消.

It gets executed by right mouse click and "Run with Powershell". This is a windows 10 standard bypass of executionpolicy and lets the script run on unmodified windows 10 machines (That's what it looks like to me at least). So I can execute my script without the need to change the executionpolicy explicitly.
After the script ran the machine is set up. I just get a warning that I want to suppress.

为了在脚本中执行此操作,我使用绕过参数将脚本提升为管理员权限.除了我在设置 AllSigned 执行策略时收到警告之外,这一切正常.它说我在更具体的范围内定义了一个策略.

To do this inside the script I elevate the script to administrator rights with a bypass parameter. This works fine except that I get a warning when setting the AllSigned execution policy. It says that I have a policy defined at a more specific scope.

注意:该命令有效且执行策略已设置.它只是弹出红色,看起来像一个错误.如果其他人执行脚本,我不希望出现问题.

Note: The command worked and the execution policy is set. It just pops up red and looks like an error. If someone else executes the script I don't want to have questions popping up.

--我的问题:--
据我所知,这种行为是有意为之,我不希望出现警告.我怎样才能抑制消息?

--My question:--
As I know that this behavior is intended I don't want the warning from showing up. How can I suppress the message?

我尝试使用开关WarningAction"和ErrorAction"进行各种设置,但它不起作用.

I tried various settings with the switches "WarningAction" and "ErrorAction" but it does not work.

一些细节:
错误信息:

Some Details:
ErrorMessage:

Set-ExecutionPolicy:Windows PowerShell 更新了您的执行策略成功,但该设置被在更具体的范围.由于覆盖,您的外壳将保留其当前有效的 Bypass 执行政策.类型Get-ExecutionPolicy -List"以查看您的执行策略设置.有关更多信息,请参阅Get-Help Set-ExecutionPolicy".在C:\Users\uwe\Desktop\InstallRootCA\InstallRootCertificate.ps1:46字符:5+ Set-ExecutionPolicy AllSigned -Scope LocalMachine -Force+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException+ FullQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope. Due to the override, your shell will retain its current effective execution policy of Bypass. Type "Get-ExecutionPolicy -List" to view your execution policy settings. For more information please see "Get-Help Set-ExecutionPolicy". At C:\Users\uwe\Desktop\InstallRootCA\InstallRootCertificate.ps1:46 char:5 + Set-ExecutionPolicy AllSigned -Scope LocalMachine -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

来自 powershell 脚本的相关代码部分:

Relevant Code parts from powershell script:

在执行前提升脚本:

 if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
     Start-Process PowerShell -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`"";
     exit;
 }

在脚本末尾设置执行策略:

Setting the Executionpolicy at the end of the script:

Set-ExecutionPolicy AllSigned -Scope LocalMachine

我尝试了各种标志

-Force
-WarningAction Ignore|SilentlyContinue
-ErrorAction same

但仍然弹出红色警告.

推荐答案

您可以将此命令放入 try catch 语句中.catch 语句将处理错误,如果它为空,则在 Set-ExecutionPolicy 抛出错误时不会发生任何事情.

You can put this command into a try catch statement. The catch statement will handle the errors and if it is empty, nothing will happen if Set-ExecutionPolicy throws an error.

try{
    Set-ExecutionPolicy AllSigned -Scope LocalMachine
}
catch {
    #Do Nothing
}

请测试一下,让我知道它是否有效,如果有效,请将帖子标记为答案:)

Please test it, let me know if it worked and if it did, please mark the post as the answer :)

这篇关于调用 Set-ExecutionPolicy 时如何抑制来自脚本的警告消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆