【问题标题】:Install certificate with PowerShell on remote server在远程服务器上使用 PowerShell 安装证书
【发布时间】:2014-02-12 05:58:37
【问题描述】:

我想在远程服务器上安装使用 makecert.exe 创建的证书 (X.509)。我无法使用 psexec 或类似的东西,但必须使用 PowerShell。

  • 服务器操作系统:Windows Server 2008 R2
  • PowerShell 版本:4

问题:如何在远程服务器上使用 PowerShell 安装证书。

【问题讨论】:

  • 远程服务器上的操作系统和 PSH 版本是什么?
  • 我已将信息添加到原始问题中。
  • 您是否在两台机器上都启用了 PowerShell 远程处理并且两台机器相互信任”?

标签: powershell certificate


【解决方案1】:

场景:ServerA 有 SSL 证书,ServerB 想要导入 SSL 证书

  1. 定义两个变量(仅限ServerB):

    $afMachineName = "SomeMachineNameOrIp"
    $certSaveLocation = "c:\temp\Cert.CER"
    
  2. 在两台机器(ServerA 和 ServerB)上启用信任:

    Function enableRemotePS() {
        Enable-PSRemoting -Force
        Set-Item wsman:\localhost\client\trustedhosts $afMachineName -Force
        Restart-Service WinRM
    }
    
  3. 保存证书(仅限ServerB):

    Function saveCert([string]$machineName,[string]$certSaveLocation) {
        Invoke-Command -ComputerName $machineName -ArgumentList $certSaveLocation -ScriptBlock {
            param($certSaveLocation)
            $cert = dir Cert:\LocalMachine\Root | where {$_.Subject -eq "CN=YOURCERTNAME" };
            $certBytes = $cert.Export("cert");
            [system.IO.file]::WriteAllBytes($certSaveLocation, $certBytes);
        }
    
        Copy-Item -Path \\$machineName\c$\temp\CertAF.CER -Destination $certSaveLocation
    }
    
  4. 导入证书(仅限ServerB)

    Function importCert([string]$certSaveLocation) {
        $CertToImport = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $certSaveLocation
    
        $CertStoreScope = "LocalMachine"
        $CertStoreName = "Root"
        $CertStore = New-Object System.Security.Cryptography.X509Certificates.X509Store $CertStoreName, $CertStoreScope
    
        # Import The Targeted Certificate Into The Specified Cert Store Name Of The Specified Cert Store Scope
        $CertStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
        $CertStore.Add($CertToImport)
        $CertStore.Close()
    }
    

【讨论】:

    【解决方案2】:

    要导入 PFX 文件,您可以使用 Import-PfxCertificate,例如

    Import-PfxCertificate -FilePath YOUR_PFX_FILE.pfx -Password (ConvertTo-SecureString -String "THE_PFX_PASSWORD" -AsPlainText -Force)
    

    要在远程计算机上执行此操作,您可以使用 Invoke-Command -ComputerName(并为 PFX 文件使用 UNC 路径)。

    【讨论】:

      猜你喜欢
      • 2015-05-08
      • 2011-03-19
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      相关资源
      最近更新 更多