【问题标题】:PowerShell: How to install a PFX certificate on a remote computer in 'CurrentUser' store location?PowerShell:如何在“CurrentUser”存储位置的远程计算机上安装 PFX 证书?
【发布时间】:2016-01-28 07:15:42
【问题描述】:

我已经尝试过使用 Invoke-Command 的 Import-PfxCertificate,但我认为它需要首先在远程服务器上复制证书文件。而且我还认为它需要授权凭证。

根据以下链接,.Net 类不支持“CurrentUser”-

http://blogs.technet.com/b/heyscriptingguy/archive/2011/02/16/use-powershell-and-net-to-find-expired-certificates.aspx

“在这两个证书存储位置中,只有 LocalMachine 可以通过 .NET 类远程访问。由于安全原因,尝试访问 CurrentUser 将导致“访问被拒绝”消息。”

有没有什么方法可以使用 PowerShell 来完成这项工作?

【问题讨论】:

    标签: powershell certificate


    【解决方案1】:

    您可以使用 PSSession 进入远程 PC。

    Enter-PSSession -ComputerName RemoteSystem
       #...Prompt changes and commands are now executing on the remote sysem
       #change the store location to the appropriate store you'd like to put the CERT
       Import-PFXCertificate -CertStoreLocation Cert:\CurrentUser\TrustedPublisher -FilePath \\server\path\to\cert.pfx
     Exit-PSSession
    

    这将是最简单的方法,以及必须在定位系统上执行的任何其他命令。

    如果您需要在跨大量系统的脚本中执行此操作:

    $computers = #get a bunch of computers, either a txt file, csv or whatever
    ForEach ($remoteSystem in $computers){
    
        Enter-PSSession -ComputerName $RemoteSystem
           #Commands below this point will execute remotely
           Import-PFXCertificate -CertStoreLocation Cert:\CurrentUser\TrustedPublisher -FilePath \\server\path\to\cert.pfx
        Exit-PSSession
    }
    

    完成!

    【讨论】:

    • 感谢您的回复,但收到此错误。 “访问被拒绝。0x80070005 (WIN32: 5 ERROR_ACCESS_DENIED)。这可能是远程计算机上需要用户凭据的结果。请参阅 Enable-WSManCredSSP Cmdlet 帮助,了解如何通过 PowerShell 远程处理启用和使用 CredSSP 进行委派。”跨度>
    • Import-PfxCertificate -CertStoreLocation Cert:\CurrentUser\My -FilePath \\172.xx.xx.xx\Shared\Certs\pravesh.com.pfx 。在 Enter-PSSession 之后执行此命令。 .
    • 首先让 PowerShell 远程处理工作。运行 Get-help about_remoting 以获得关于该主题的出色指南。
    • pfx 需要密码怎么办?
    【解决方案2】:

    我在上述解决方案中遇到了双跳身份验证问题。下面的代码对我来说效果很好。希望能帮助到你! :)

            [byte[]]$Pfxinbyts = Get-Content "$FullPathWithFileName.pfx" -Encoding byte
            Invoke-Command -Session $session -ScriptBlock {
        param(
            [byte[]] $PFXCertInByte,
            [string] $CertRootStore,
            [string] $CertStore,
            [string] $X509Flags,
            $PfxPass)
        $Pfx = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
        $Pfx.Import([byte[]]$PFXCertInByte, $PfxPass, $X509Flags)
        $Store = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $CertStore, $CertRootStore
        $Store.Open("MaxAllowed")
        $Store.Add($Pfx)
        if ($?)
        {
            "${Env:ComputerName}: Successfully added certificate."
        }
        else
        {
            "${Env:ComputerName}: Failed to add certificate! $($Error[0].ToString() -replace '[\r\n]+', ' ')"
        }
        $Store.Close()
    } -ArgumentList $Pfxinbyts, "LocalMachine", "My", "Exportable,PersistKeySet", $PFXPassword
    

    【讨论】:

      猜你喜欢
      • 2015-05-08
      • 2022-10-17
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 2013-07-02
      相关资源
      最近更新 更多