我正在构build一个脚本,将下载并安装一堆程序。 其中一个程序取决于虚拟驱动程序,该驱动程序需要导入其证书才能运行。 通常,安装程序会在GUI中为您执行此操作,并popup确认对话框询问您是否信任此驱动程序。 但是,这个中断对于我正在编写的脚本是不可接受的。
我已经find了一种通过文件属性GUI导出证书的方法,并且可以用脚本导入该文件,从而允许我在没有任何用户交互的情况下进行安装。 但是,为了部署和完全自动化此脚本,我还需要能够通过脚本从安装程序中导出证书文件。 可以这样做吗?
整个过程可以使用.Net框架中提供的X509Certificate类在PowerShell中完成。
您需要做的第一件事是从签名文件中获取证书文件。 这是使用CreateFromCertFile函数完成的。 只要注意,该function只能采取完整的path,而不是相对的。
然后您可以打开本地计算机上的证书存储并导入证书。 要写入本地计算机存储,这需要以pipe理员身份执行。
Add-Type -AssemblyName System.Security # Create a new certificate extracted from the signed file. $certificate = [System.Security.Cryptography.X509Certificates.X509Certificate]::CreateFromCertFile('c:\temp\SetupVirtualCloneDrive5450.exe') # Open the Trusted Publishers cert store and add the certificate in. $cert_store = New-Object System.Security.Cryptography.X509Certificates.X509Store -ArgumentList 'TrustedPublisher', 'LocalMachine' | ForEach-Object { $_.Open('ReadWrite') $_.Add($certificate) $_.Close() }
仅供参考,我使用这种方法来安装也有驱动程序提示的VirtualBox Guest Additions。