Skip to content

Commit 4e33461

Browse files
author
Tajo Fittkau
committed
Adding the ability to use a password on PFX export
1 parent e8d84f8 commit 4e33461

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

Request-Certificate.ps1

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ Specifies the name of the CA to send the request to in the format FQDN\CAName
4040
If the CAName is not specified, then the directory is queried for a list of enterprise CAs.
4141
If more than one is returned the user is prompted to choose an enterprise CA from the local Active Directory.
4242
43-
.PARAMETER Export
44-
Exports the certificate and private key to a pfx file instead of installing it in the local computer store.
45-
By default the certificate will be installed in the local computer store.
46-
47-
.PARAMETER ExportPath
48-
Path to wich the pfx file should be saved when -Export is specified.
49-
5043
.PARAMETER Country
5144
Specifies two letter for the optional country value in the subject of the certificate(s).
5245
e.g. CH
@@ -67,6 +60,16 @@ e.g. jofe.ch
6760
Specifies the optional department value in the subject of the certificate(s).
6861
e.g. IT
6962
63+
.PARAMETER Export
64+
Exports the certificate and private key to a pfx file instead of installing it in the local computer store.
65+
By default the certificate will be installed in the local computer store.
66+
67+
.PARAMETER ExportPath
68+
Path to wich the pfx file should be saved when -Export is specified.
69+
70+
.PARAMETER Password
71+
Specify the Password (as plain String or SecureString) used on the export.
72+
7073
.INPUTS
7174
System.String
7275
Common name for the subject, SAN , Country, State etc. of the certificate(s) as a string
@@ -154,7 +157,7 @@ www.jfe.cloud
154157
155158
#>
156159

157-
[CmdletBinding()]
160+
[CmdletBinding(DefaultParametersetname="NoExport")]
158161
Param(
159162
[Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
160163
[string]$CN,
@@ -168,11 +171,6 @@ Param(
168171
[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)]
169172
[string]$CAName,
170173
[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)]
171-
[switch]$Export,
172-
[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)]
173-
[ValidateScript( {Resolve-Path -Path $_})]
174-
[string]$ExportPath,
175-
[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)]
176174
[string]$Country,
177175
[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)]
178176
[string]$State,
@@ -181,7 +179,16 @@ Param(
181179
[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)]
182180
[string]$Organisation,
183181
[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)]
184-
[string]$Department
182+
[string]$Department,
183+
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $True, ParameterSetName='Export')]
184+
[switch]$Export,
185+
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $True, ParameterSetName='Export')]
186+
[ValidateScript( {Resolve-Path -Path $_})]
187+
[string]$ExportPath,
188+
[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True, ParameterSetName='Export')]
189+
[ValidateScript( {$_.getType().name -eq "SecureString" -or $_.getType().name -eq "String"})]
190+
$Password
191+
185192
)
186193
BEGIN {
187194
#internal function to do some cleanup
@@ -229,6 +236,8 @@ ProviderName = "Microsoft Enhanced Cryptographic Provider v1.0"
229236
CertificateTemplate = "$TemplateName"
230237
"@
231238

239+
Write-Debug "Inf-File: $file"
240+
232241
#check if SAN certificate is requested
233242
if ($PSBoundParameters.ContainsKey('SAN')) {
234243
#each SAN must be a array element
@@ -332,7 +341,14 @@ CertificateTemplate = "$TemplateName"
332341
Write-Debug "Certificate found in computerstore: $cert"
333342

334343
#create a pfx export as a byte array
335-
$certbytes = $cert.export([System.Security.Cryptography.X509Certificates.X509ContentType]::pfx)
344+
if($Password) {
345+
Write-Debug "Exporting with password"
346+
$certbytes = $cert.export([System.Security.Cryptography.X509Certificates.X509ContentType]::pfx, $Password)
347+
} else {
348+
Write-Debug "Exporting without password"
349+
$certbytes = $cert.export([System.Security.Cryptography.X509Certificates.X509ContentType]::pfx)
350+
}
351+
336352

337353
#write pfx file
338354
if ($PSBoundParameters.ContainsKey('ExportPath')) {

0 commit comments

Comments
 (0)