【问题标题】:How to make PowerShell prompt for un-signed scripts如何使 PowerShell 提示未签名的脚本
【发布时间】:2018-11-21 17:53:40
【问题描述】:

我的环境中的执行策略是AllSigned:

PS E:\Temp> 获取执行策略 全签

当我尝试执行不受信任的脚本时,它会抛出错误:

& : 无法加载文件 C:\temp\anz.ps1。文件 C:\temp\any.ps1 不是 数字签名。 您不能在当前系统上运行此脚本。有关更多信息 运行脚本和设置执行策略,参见 about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170。 在行:1 字符:3 + & .\any.ps1 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : 未授权访问

如何让 PowerShell 提示我是否要执行脚本?

类似:

安全警告
仅运行您信任的脚本。虽然来自 Internet 的脚本很有用,但此脚本可能会损害您的计算机。你想运行 .\temp.ps1 吗?
[D] 不要运行 [R] 运行一次 [S] 暂停:

注意:我不想绕过或抑制提示。

【问题讨论】:

  • 执行策略的选项有限,请参阅about_execution_policies。您无法构建自定义策略,因此您必须选择最适合您的选项。
  • 请注意,拥有数字签名并不能使脚本安全。 “我要选择是否运行这个从网上下载的脚本”和“我要选择运行这个未签名的脚本”不是一回事。下载的脚本可以签名,本地创建的脚本可能未签名。检测文件是否已下载是可能的,但正如 James 所说,它与执行策略是分开的。
  • 感谢您的反馈,1) 我看过一些关于脚本未签名时 PowerShell 提示的帖子:stackoverflow.com/questions/728143/… 2) 目前我创建了一个脚本,该脚本会在其他机器上生成上述错误.

标签: powershell executionpolicy


【解决方案1】:

你可以先添加一个函数来检查文件:

function Test-FileSafety {
    # This function simply returns $true when the file is ok or the user decided to
    # go ahead and run it even though he/she was warned.
    # If the user decided it was too tricky and bailed out, the function returns $false
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [ValidateScript({Test-Path $_ -PathType Leaf})] 
        [Alias ('Path')]
        [string]$FileName
    )

    Add-Type -AssemblyName System.Windows.Forms
    $letsGo = $true

    # first test if the file was downloaded from internet (and was not already unblocked)
    if (Get-Item $FileName -Stream zone*) {
        $message  = "Run only scripts that you trust.`r`n"
        $message += "While scripts from the Internet can be useful this script can potentially harm your computer.`r`n`r`n"
        $message += "Do you want to allow '$FileName' ?"

        $result = [System.Windows.Forms.MessageBox]::Show($message, "Security Warning", 3)
        # evaluate the users response and act upon it
        switch ($result) {
            Yes    { <# user wants to run #> Unblock-File -Path $FileName ; break }
            No     { <# user decided not to run the script #> ; $letsGo = $false; break }
            Cancel { <# user bailed out #> ; $letsGo = $false; break }
        }
    }

    # next test if the file is digitally signed or not
    if ($letsGo -and (Get-AuthenticodeSignature -FilePath $FileName).Status -eq 'NotSigned') {
        $message  = "Run only scripts that you trust.`r`n"
        $message += "The script is not digitally signed.`r`n`r`n"
        $message += "Do you still want to run '$FileName' ?"

        $result = [System.Windows.Forms.MessageBox]::Show($message, "Security Warning", 3)
        # evaluate the users response and act upon it
        switch ($result) {
            Yes    { <# user wants to run even though the script is not digitally signed #> ; break}
            No     { <# user decided it was too dangerous and does not want to run the script #> ; $letsGo = $false; break}
            Cancel { <# user bailed out #> ; $letsGo = $false; break}
        }
    }

    return $letsGo
}

并像这样使用它:

if ((Test-FileSafety -FileName "C:\temp\anz.ps1")) {
    # run the script
}
else {
    # do nothing, the user cancelled
}

【讨论】:

  • +1 答案和您的努力 Theo。如果我从客户的角度来看,我只是想向客户建议一些配置,而不是提供 Powershell 脚本。 (..但同样,这是我的观点...)您的方法似乎也很完美。
【解决方案2】:

您可以尝试以管理员身份在 power shell 中运行以下命令

修复方法是运行 Set-ExecutionPolicy 并更改执行策略设置。

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

“绕过”意味着没有任何内容被阻止,并且不会显示任何警告、提示或消息。

【讨论】:

    猜你喜欢
    • 2018-04-11
    • 2013-05-06
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 2021-09-01
    • 2010-11-24
    • 2018-03-25
    • 1970-01-01
    相关资源
    最近更新 更多