如何使用 powershell 脚本安装证书

How to install a Certificates using powershell script

我正在尝试通过 PowerShell 脚本安装证书。我正在使用 Import-Certificate 方法,正如 Microsoft PowerShell 文档中所建议的那样。

这是我的代码:

$script = {
    $file = ( Get-ChildItem -Path  C:\Users\Administrator\Desktop\newCert.cer )
    $file | Import-Certificate -CertStoreLocation cert:\CurrentUser\Root
    echo $file
    }

invoke-command -Credential $clientCred -ComputerName $ClientIP -ScriptBlock $script

我收到以下错误:

UI is not allowed in this operation
    + CategoryInfo          : InvalidArgument: (:) [Import-Certificate], ArgumentException 

我不确定哪里出了问题 - 如果有人能指出正确的方向,那将非常有帮助。

这里的问题是,当您将证书安装到 Cert:\CurrentUser\Root(当前用户帐户中受信任的根 CA)时,底层 CryptoAPI 会调用以下对话框:

这就是错误消息提到 UI 的原因。由于您正试图在远程会话中安装证书,因此无法在远程主机的交互式会话中按下按钮。这就是 UI 对话被禁止的原因。

您可以将证书安装到本地计算机存储区。即安装到Cert:\LocalMachine\Root.

请注意,将根证书安装到本地机器存储时,它会自动传播到该机器上的所有用户帐户。也就是说,可以为用户建立无意的信任,而这种信任可能是不应该的。