@@ -155,13 +155,19 @@ function Add-DbaComputerCertificate {
155155 if (" NonExportable" -in $Flag ) {
156156 $flags = ($Flag | Where-Object { $PSItem -ne " Exportable" -and $PSItem -ne " NonExportable" } ) -join " ,"
157157
158- # It needs at least one flag
158+ # Ensure the correct store is used
159159 if (-not $flags ) {
160160 if ($Store -eq " LocalMachine" ) {
161161 $flags = " MachineKeySet"
162162 } else {
163163 $flags = " UserKeySet"
164164 }
165+ } else {
166+ if ($Store -eq " LocalMachine" ) {
167+ $flags += " ,MachineKeySet"
168+ } else {
169+ $flags += " ,UserKeySet"
170+ }
165171 }
166172 } else {
167173 $flags = $Flag -join " ,"
@@ -198,9 +204,11 @@ function Add-DbaComputerCertificate {
198204 $plainPassword = [System.Runtime.InteropServices.Marshal ]::PtrToStringUni($ptr )
199205 }
200206
207+ Write-Message - Level Verbose - Message " Importing Path: $Path "
201208 try {
202209 # Import using plain text password (or null for non-password-protected certificates)
203210 # Works reliably in all PowerShell versions v3+
211+ # This import intentionally doesn't use $flags to allow re-export
204212 $null = $certCollection.Import ($fileBytes , $plainPassword , " Exportable, PersistKeySet" )
205213
206214 # Export the entire collection as a single PFX to preserve the chain
@@ -234,9 +242,9 @@ function Add-DbaComputerCertificate {
234242
235243 # Use X509Certificate2Collection to import the full certificate chain
236244 $certCollection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
245+ Write-Verbose - Message " Importing certificate chain to $Folder \$Store using flags: $flags "
237246 $certCollection.Import ($CertificateData , $PlainPassword , $flags )
238247
239- Write-Verbose - Message " Importing certificate chain to $Folder \$Store using flags: $flags "
240248 $tempStore = New-Object System.Security.Cryptography.X509Certificates.X509Store($Folder , $Store )
241249 $tempStore.Open (" ReadWrite" )
242250
@@ -321,4 +329,4 @@ function Add-DbaComputerCertificate {
321329 }
322330 }
323331 }
324- }
332+ }
0 commit comments