@@ -32,7 +32,8 @@ The default value is "WebServer".
3232
3333. PARAMETER CAName
3434Specifies 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
3839Exports 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
339352END {
340353 Remove-ReqTempfiles - tempfiles $inf , $req , $cer , $rsp
341- }
354+ }
0 commit comments