Pages

03 August 2021

Create certificates in bulk from .CSR files with PowerShell

This is a bit of a work in progress, if anyone can help me out with the last bit.
I know it can be done with an foreach statement but haven't had the time to test it.

<#
  .Synopsis
     Request certificates from csr's
  .DESCRIPTION
     This script requests certificates from an on-premises CA
     Define you CA, PKI name, and output path
     Then copy the lines below for each certificate and fill the name between the quotes

     $name = ""
     $filename = "$name.cer"
     $status = Submit-CertificateRequest -path "C:\Scripts\Get-CertificateFromCSR\CSR\$name.csr" -CA $PKI -Attribute "CertificateTemplate:Webserver" 
     $ReqID = $status.requestid
     Get-IssuedRequest -RequestID $reqID -CertificationAuthority $PKI | Receive-Certificate -Path $OutPath\$name -Force
     cd $outpath\$name
     $item = ls
     Rename-Item -path $item -newname $filename

  .Created by
     Edwin van Brenk
  .Created for
     vanbrenk.blogspot.com
  .Date
     03-08-2021
  .Version
     1.0
  #>

# Run as Admin
Import-Module pspki

cd C:\Scripts\Get-BulkCertFromCSR\csr

$ca = Connect-CA caname.domain.lan
$PKI = "CAname.domain.lan"
$OutPath = "C:\Scripts\Get-CertificateFromCSR\IssuedCertificates\"

<#
$name = ""
$filename = "$name.cer"
$status = Submit-CertificateRequest -path "C:\Scripts\Get-CertificateFromCSR\CSR\$name.csr" -CA $PKI -Attribute "CertificateTemplate:Webserver" 
$ReqID = $status.requestid
Get-IssuedRequest -RequestID $reqID -CertificationAuthority $PKI | Receive-Certificate -Path $OutPath\$name -Force
cd $outpath\$name
$item = ls
Rename-Item -path $item -newname $filename

#>


$name = "Certificate-01"
$filename = "$name.cer"
$status = Submit-CertificateRequest -path "C:\Scripts\Get-CertificateFromCSR\CSR\$name.csr" -CA $PKI -Attribute "CertificateTemplate:Webserver"
$ReqID = $status.requestid
Get-IssuedRequest -RequestID $reqID -CertificationAuthority $PKI | Receive-Certificate -Path $OutPath\$name -Force
cd $outpath\$name
$item = ls
Rename-Item -path $item -newname $filename

$name = "Certificate-02"
$filename = "$name.cer"
$status = Submit-CertificateRequest -path "C:\Scripts\Get-CertificateFromCSR\CSR\$name.csr" -CA $PKI -Attribute "CertificateTemplate:Webserver"
$ReqID = $status.requestid
Get-IssuedRequest -RequestID $reqID -CertificationAuthority $PKI | Receive-Certificate -Path $OutPath\$name -Force
cd $outpath\$name
$item = ls
Rename-Item -path $item -newname $filename

$name = "Certificate-03"
$filename = "$name.cer"
$status = Submit-CertificateRequest -path "C:\Scripts\Get-CertificateFromCSR\CSR\$name.csr" -CA $PKI -Attribute "CertificateTemplate:Webserver"
$ReqID = $status.requestid
Get-IssuedRequest -RequestID $reqID -CertificationAuthority $PKI | Receive-Certificate -Path $OutPath\$name -Force
cd $outpath\$name
$item = ls
Rename-Item -path $item -newname $filename

No comments:

Post a Comment