Last active
February 1, 2023 19:09
-
-
Save mcc85s/ed0844542f2aae23562b85d2ad74cc70 to your computer and use it in GitHub Desktop.
To import an NTDS Store Certificate... not yet tested.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bennett @ https://stackoverflow.com/questions/21895800/powershell-script-to-install-certificate-into-active-directory-store | |
# Modified/Simplified | |
Function Import-NTDSCertificate | |
{ | |
[ CmdletBinding () ] Param ( | |
[ Parameter ( Mandatory ) ] [ String ] $File , | |
[ Parameter ( Mandatory ) ] [ String ] $Password , | |
#Remove certificate from LocalMachine\Personal certificate store | |
[ Switch ] $Cleanup ) | |
Begin | |
{ | |
IEX "Using Namespace System.Security.Cryptography.X509Certificates" | |
Write-Verbose -Message "Importing PFX file." | |
$PFX = [ X509Certificate2 ]::new() | |
$PFX | % { $_.Import( $File , $Password , [ X509KeyStorageFlags ]::Exportable ) } | |
$PFX.Thumbprint | % { | |
If ( $_ -ne $Null ) | |
{ | |
$Paths = ForEach ( $I in "" , "\Cryptography\Services\NTDS" ) | |
{ | |
"HKLM:\Software\Microsoft$I\SystemCertificates\MY\Certificates\$_" | |
} | |
} | |
} | |
} | |
Process | |
{ | |
Write-Verbose -Message "Importing certificate into LocalMachine\Personal" | |
$Store = [ X509Store ]::new( "My" , "LocalMachine" ) | |
$Store | % { | |
$_.Open( 'MaxAllowed' ) | |
$_.Add( $PFX ) | |
$_.Close() | |
} | |
Write-Verbose -Message "Copying certificate from LocalMachine\Personal to NTDS\Personal" | |
$Splat = @{ | |
Path = $Paths[0] | |
Destination = $Paths[1] | |
Recurse = $True | |
} | |
CP @Splat | |
} | |
End | |
{ | |
If ( $Cleanup ) | |
{ | |
Write-Verbose -Message "Removing certificate from LocalMachine\Personal" | |
$Splat = @{ | |
Path = $Paths[0] | |
Recurse = $True | |
} | |
RI @Splat | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment