将证书安装到 Active Directory 存储中的 Powershell 脚本

标签 powershell ssl active-directory certificate

我正在尝试编写一个 powershell 脚本来将证书安装到事件目录证书存储中,

以下是手动执行此操作的步骤,如有任何帮助,我们将不胜感激。

在 Windows 2008R2 域 Controller 上,

点击开始->运行

输入MMC

点击确定

点击文件 -> 添加/删除管理单元

选择“证书”->添加

选择“服务帐户”

点击下一步

选择“本地计算机”

点击下一步

选择“Active Directory 域服务”

点击完成

点击确定

我希望脚本将证书安装到:

NTDS\个人

我会张贴图片,但显然我没有足够的“声誉”,所以我只能提供文字说明。

所以基本上我尝试过的是,我使用下面的这个 powershell 函数将证书导入本地机器 -> 个人存储,这是大多数证书所在的位置,并且代码有效。

但我需要将证书安装到域 Controller 上的“NTDS\Personal”存储中,但是 $certRootStore 只接受本地计算机或 CurrentUser,所以我被卡住了:/

function Import-PfxCertificate 
{
    param
    (
        [String]$certPath,
        [String]$certRootStore = "localmachine",
        [String]$certStore = "My",
        $pfxPass = $null
    ) 
    $pfx = new-object System.Security.Cryptography.X509Certificates.X509Certificate2 

    if ($pfxPass -eq $null) 
    {
        $pfxPass = read-host "Password" -assecurestring
    } 

    $pfx.import($certPath,$pfxPass,"Exportable,PersistKeySet") 

    $store = new-object System.Security.Cryptography.X509Certificates.X509Store($certStore,$certRootStore) 
    $store.open("MaxAllowed") 
    $store.add($pfx) 
    $store.close() 
}

Import-PfxCertificate -certPath "d:\Certificate.pfx"

问候亚历克斯

最佳答案

结合使用上面已有的内容和两个证书存储的注册表项,这是可行的。

唯一的另一件事是,当证书存储中有多个证书时,我不知道 NTDS 如何确定使用哪个证书。

function Import-NTDSCertificate {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$PFXFile,

        [Parameter(Mandatory)]
        [string]$PFXPassword,

        #Remove certificate from LocalMachine\Personal certificate store
        [switch]$Cleanup
        )
        begin{
            Write-Verbose -Message "Importing PFX file."
            $PFXObject = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2
            $PFXObject.Import($PFXFile,$PFXPassword,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)

            $thumbprint = $PFXObject.Thumbprint
        }
        process{
            Write-Verbose -Message "Importing certificate into LocalMachine\Personal"
            $certificateStore = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store('My','LocalMachine')
            $certificateStore.Open('MaxAllowed')
            $certificateStore.Add($PFXObject)
            $certificateStore.Close()

            Write-Verbose -Message "Copying certificate from LocalMachine\Personal to NTDS\Personal"
            $copyParameters = @{
                'Path' = "HKLM:\Software\Microsoft\SystemCertificates\MY\Certificates\$thumbprint"
                'Destination' = "HKLM:\SOFTWARE\Microsoft\Cryptography\Services\NTDS\SystemCertificates\My\Certificates\$thumbprint"
                'Recurse' = $true
            }
            Copy-Item @copyParameters
        }
        end{
            if ($Cleanup){
                Write-Verbose -Message "Removing certificate from LocalMachine\Personal"
                $removalParameters = @{
                    'Path' = "HKLM:\SOFTWARE\Microsoft\SystemCertificates\MY\Certificates\$thumbprint"
                    'Recurse' = $true
                }
                Remove-Item @removalParameters
            }
        }
}

关于将证书安装到 Active Directory 存储中的 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21895800/

相关文章:

powershell - "RenewDHCPLease()"和 "IPConfig/renew"之间的区别?

json - Powershell ConvertTo-JSON缺少嵌套级别

python - 从 Windows PowerShell 启动 Python

ssl - SQL Server SSL 加密

Node.js 在移动设备上使用 SSL 通配符中断

c# - 从 .NET 通过电子邮件地址搜索 AD 用户的正确方法

powershell - Remove-AdObject with Import-CSV 错误

python-3.x - 删除 Anaconda3 后,我仍然在 PowerShell 中收到这些错误

ruby-on-rails - 闪光[:notice] not working when redirect_to changes from http to https and vice versa

powershell - 如何从命令提示符或批处理文件将具有长名称的组添加到本地组?