在Linux上使用Powershell脚本检查文件是否已签名

问题描述

我编写了一个简单的Powershell脚本,如果未签名文件,该脚本将引发错误:

[Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
$fileSignature = Get-AuthenticodeSignature -FilePath <file that I want to check>
if ($fileSignature.Status -eq "NotSigned")
{
    throw "File is unsigned"
}

这在Windows上完美运行,但是我需要移植脚本才能在Linux上运行。我仍然可以在那里使用Powershell,但是尚未为Linux实现Get-AuthenticodeSignature。我可以使用其他方法执行相同的测试吗?

解决方法

首先,您正在使用两个不同的PowerShell环境。

  1. Windows上的Windows PowerShell。 (powershell.exe / powershel_ise.exe)-完整.Net
  2. Linux / OSX上的
  3. PowerShell Core。 (pwsh.exe)-.Net Core

您已经发现,可用的cmdlet是特定于PowerShell版本的。 Windows外部将无法使用任何需要完整.Net的cmdlet(嗯,任何东西)。

您不应以这种方式检查此状态。 PowerShell提供了一种强制使用签名的内置方法。这就是ExecutionPolciy设置的作用。默认情况下,在PowerShell上安装的EP是RemoteSigned,因此将运行已签名或未签名的本地脚本。

如果您想始终强制使用签名脚本,请以这种方式设置EP。

Set-ExecutionPolicy -ExecutionPolicy AllSigned

所有签名

脚本可以运行。

要求所有脚本和配置文件都必须由 受信任的发布者,包括您在本地编写的脚本 电脑。在运行来自您的发布者的脚本之前提示您 尚未归类为受信任或不受信任。风险已签收, 但恶意的脚本。

在所需范围内设置

MachinePolicy,UserPolicy,Process,CurrentUser或LocalMachine

about_Execution_Policies - PowerShell | Microsoft Docs

Get-ExecutionPolicy (Microsoft.PowerShell.Security

Set-ExecutionPolicy (Microsoft.PowerShell.Security

通过强制执行“ AllSigned”策略,然后可以对文件夹中的所有脚本文件运行循环并尝试运行它们,而没有签名的文件将失败。因此,只需使用try / catch报告这些情况。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...