Skip to content

Commit deeeb59

Browse files
dra27J0F3
authored andcommitted
Request-Certificate: better default for -CAName
* Improved default for -CAName Query the directory to get a list of available CAs (this is the same list with which the user will be prompted) and if there's only 1 then use it. This matches more closely the behavior of Get-Certificate.
1 parent fd444a8 commit deeeb59

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

Request-Certificate.ps1

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ The default value is "WebServer".
3232
3333
.PARAMETER CAName
3434
Specifies the name of the CA to send the request to in the format FQDN\CAName
35-
If the CAName is not specified the user becomes a prompt to choose a enterprise CA from the local Active Directory.
35+
If the CAName is not specified, then the directory is queried for a list of enterprise CAs.
36+
If more than one is returned the user is prompted to choose an enterprise CA from the local Active Directory.
3637
3738
.PARAMETER Export
3839
Exports the certificate and private key to a pfx file instead of installing it in the local computer store.
@@ -264,14 +265,26 @@ CertificateTemplate = "$TemplateName"
264265
write-verbose "Sending certificate request to CA"
265266
Write-Debug "CAName = $CAName"
266267

267-
if ($PSBoundParameters.ContainsKey('CAName')) {
268-
Write-Debug "certreq -submit -config `"$CAName`" `"$req`" `"$cer`""
269-
Invoke-Expression -Command "certreq -submit -config `"$CAName`" `"$req`" `"$cer`""
268+
if (!$PSBoundParameters.ContainsKey('CAName')) {
269+
$rootDSE = [System.DirectoryServices.DirectoryEntry]'LDAP://RootDSE'
270+
$searchBase = [System.DirectoryServices.DirectoryEntry]"LDAP://$($rootDSE.configurationNamingContext)"
271+
$CAs = [System.DirectoryServices.DirectorySearcher]::new($searchBase,'objectClass=pKIEnrollmentService').FindAll()
272+
273+
if($CAs.Count -eq 1){
274+
$CAName = "$($CAs[0].Properties.dnshostname)\$($CAs[0].Properties.cn)"
275+
}
276+
else {
277+
$CAName = ""
278+
}
270279
}
271-
else {
272-
Invoke-Expression -Command "certreq -submit `"$req`" `"$cer`""
280+
281+
if (!$CAName -eq "") {
282+
$CAName = " -config `"$CAName`""
273283
}
274284

285+
Write-Debug "certreq -submit$CAName `"$req`" `"$cer`""
286+
Invoke-Expression -Command "certreq -submit$CAName `"$req`" `"$cer`""
287+
275288
if (!($LastExitCode -eq 0)) {
276289
throw "certreq -submit command failed"
277290
}
@@ -338,4 +351,4 @@ CertificateTemplate = "$TemplateName"
338351

339352
END {
340353
Remove-ReqTempfiles -tempfiles $inf, $req, $cer, $rsp
341-
}
354+
}

0 commit comments

Comments
 (0)