AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 107028
In Process
Jesse Weigert
Jesse Weigert
Asked: 2010-01-28 16:39:31 +0800 CST2010-01-28 16:39:31 +0800 CST 2010-01-28 16:39:31 +0800 CST

使用脚本将一堆证书导入正确的证书存储

  • 772

我在 p7b 文件中有一组证书,我想根据证书模板自动将每个证书导入正确的存储区。使用脚本执行此操作的最佳方法是什么?

我尝试使用certutil -addstore root Certificate.p7b,这将正确地将所有根 CA 放入根存储,但如果遇到任何其他类型的证书,它会返回错误。

我愿意使用批处理脚本、vbscript 或 powershell 来完成这项任务。谢谢!

windows ssl certificate
  • 2 2 个回答
  • 17733 Views

2 个回答

  • Voted
  1. WebFooL
    2012-05-08T23:15:17+08:002012-05-08T23:15:17+08:00

    我使用CertMgr.exe一个简单的 bat 文件来导入证书。

    certmgr.exe -add -c ca.cer -s -r localMachine root  >> log.txt
    certmgr.exe -add -c test.cer -s -r localMachine root  >> log.txt
    certmgr.exe -add -c edu.cer -s -r localMachine root  >> log.txt
    

    这是一篇 TechNet 文章,其中记录了您可以使用 certmgr.exe 执行哪些命令/用法

    • 2
  2. Guido van Brakel
    2011-03-12T10:59:33+08:002011-03-12T10:59:33+08:00

    我还没有找到一个脚本来根据它的模板将它导入证书到正确的存储中。我认为您自己制作了该脚本,因为它根本不存在。我发现的是一个 PowerShell 脚本,它从目录导入证书,并且在命令中您必须自己指定正确的存储。我认为它可能对你有用:

    如何使用脚本 函数导入安全证书。

    注意:要获取可用商店名称的列表,请运行以下命令:dir cert: | 选择 - 扩展商店名称

    示例用法: Import-Certificate -CertFile "VeriSign_Expires-2028.08.01.cer" -StoreNames AuthRoot, Root -LocalMachine

    Import-Certificate -CertFile "VeriSign_Expires-2018.05.18.p12" -StoreNames AuthRoot -LocalMachine -CurrentUser -CertPassword Password -Verbose

    dir -Path C:\Certs -Filter *.cer | 导入证书 -CertFile $_ -StoreNames AuthRoot, Root -LocalMachine -Verbose

    脚本本身:

    #requires -Version 2.0
    
    function Import-Certificate
    {
        param
        (
            [IO.FileInfo] $CertFile = $(throw "Paramerter -CertFile [System.IO.FileInfo] is required."),
            [string[]] $StoreNames = $(throw "Paramerter -StoreNames [System.String] is required."),
            [switch] $LocalMachine,
            [switch] $CurrentUser,
            [string] $CertPassword,
            [switch] $Verbose
        )
    
        begin
        {
            [void][System.Reflection.Assembly]::LoadWithPartialName("System.Security")
        }
    
        process 
        {
            if ($Verbose)
            {
                $VerbosePreference = 'Continue'
            }
    
            if (-not $LocalMachine -and -not $CurrentUser)
            {
                Write-Warning "One or both of the following parameters are required: '-LocalMachine' '-CurrentUser'. Skipping certificate '$CertFile'."
            }
    
            try
            {
                if ($_)
                {
                    $certfile = $_
                }
                $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $certfile,$CertPassword
            }
            catch
            {
                Write-Error ("Error importing '$certfile': $_ .") -ErrorAction:Continue
            }
    
            if ($cert -and $LocalMachine)
            {
                $StoreScope = "LocalMachine"
                $StoreNames | ForEach-Object {
                    $StoreName = $_
                    if (Test-Path "cert:\$StoreScope\$StoreName")
                    {
                        try
                        {
                            $store = New-Object System.Security.Cryptography.X509Certificates.X509Store $StoreName, $StoreScope
                            $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
                            $store.Add($cert)
                            $store.Close()
                            Write-Verbose "Successfully added '$certfile' to 'cert:\$StoreScope\$StoreName'."
                        }
                        catch
                        {
                            Write-Error ("Error adding '$certfile' to 'cert:\$StoreScope\$StoreName': $_ .") -ErrorAction:Continue
                        }
                    }
                    else
                    {
                        Write-Warning "Certificate store '$StoreName' does not exist. Skipping..."
                    }
                }
            }
    
            if ($cert -and $CurrentUser)
            {
                $StoreScope = "CurrentUser"
                $StoreNames | ForEach-Object {
                    $StoreName = $_
                    if (Test-Path "cert:\$StoreScope\$StoreName")
                    {
                        try
                        {
                            $store = New-Object System.Security.Cryptography.X509Certificates.X509Store $StoreName, $StoreScope
                            $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
                            $store.Add($cert)
                            $store.Close()
                            Write-Verbose "Successfully added '$certfile' to 'cert:\$StoreScope\$StoreName'."
                        }
                        catch
                        {
                            Write-Error ("Error adding '$certfile' to 'cert:\$StoreScope\$StoreName': $_ .") -ErrorAction:Continue
                        }
                    }
                    else
                    {
                        Write-Warning "Certificate store '$StoreName' does not exist. Skipping..."
                    }
                }
            }
        }
    
        end
        { }
    }
    

    来源:进口证书

    • 0

相关问题

  • 您最喜欢的云计算提供商是什么?[关闭]

  • Vanilla Powershell 是否足以成为 Windows 和 DB 服务器管理员的语言?

  • 为什么添加新驱动器后我的磁盘驱动器访问速度如此之慢?

  • 在 Windows Server 2003 下使用 wscipt 从 .asp 文件运行 .exe

  • 最佳混合环境(OS X + Windows)备份?[关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve