Create a self signed certificate on Windows Domain Controllers – Automated

Windows Certificate Services can create certificates for all devices in your organization. However, at times you may need just one certificate to install on a Domain Controller. In this blog, I am going to explain the process to install a Self Signed Certificate on a domain controller using  an automated way. See the last blog for an automated way –https://www.signifium.com/2020/08/18/create-a-self-signed-certificate-on-windows-domain-controllers-manual

Automated process

Microsoft used to provide a tool to create self signed certificates, however today you can use powershell commands to do the same. In this process, I will explain how to create the certificate using powershell script to prepare your Active Directory for LDAPS or LDAP over SSL.

 

  1. Launch Powershell on the domain controllers as an administrator.
  2. Before you run the script below you need to know following information
    1. certname – This will be the name of the certificate. Use the same name as your domain controller name. In my example I have used “dc1.signifium.com”
    2. cert_years_toexpire – Use an integer like 1, 2 or 3 to set the life of your certificate.
    3. Copy the following script to your domain controller as Install-DC-Cert.ps1  Param (
    4.      
    5.      
    6.       [parameter(Mandatory=$true)]
    7.       [string]$certname,
    8.       [parameter(Mandatory=$true)]
    9.       [ValidateRange(0,3)]
    10.       [string] $cert_years_toexpire
    11.      
    12.        
    13. )
    14. Write-Host “Starting Script … “
    15. $date_now = Get-Date
    16. $cert_expirydate = $date_now.AddYears($cert_years_toexpire)
    17. $certStoreLoc=’HKLM:/Software/Microsoft/Cryptography/Services/NTDS/SystemCertificates/My/Certificates’
    18. $servercert=New-SelfSignedCertificate  -CertStoreLocation cert:/LocalMachine/My -DnsName $certname -NotAfter $cert_expirydate
    19. $thumbprint=($servercert.Thumbprint | Out-String).Trim();
    20. Write-Host “Certificate generated with thumbprint : $thumbprint”
    21. if (!(Test-Path $certStoreLoc)){
    22. New-Item $certStoreLoc -Force;
    23. };
    24. Copy-Item -Path HKLM:/Software/Microsoft/SystemCertificates/My/Certificates/$thumbprint -Destination $certStoreLoc;
    25. Write-Host “Certificate added to the personal store in local computer”
    26. Write-Host “Copying certificate to Trusted Root Certificate Authorities”
    27. $newcert = dir Cert:\LocalMachine\My | where {$_.Thumbprint -eq $thumbprint}
    28. $DestStoreScope = ‘LocalMachine’
    29. $DestStoreName = ‘root’
    30.  
    31. $DestStore = New-Object  -TypeName System.Security.Cryptography.X509Certificates.X509Store  -ArgumentList $DestStoreName, $DestStoreScope
    32. $DestStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
    33. $DestStore.Add($newcert)
    34. $DestStore.Close()
    35. Write-Host “Certificate added to Trusted Root Certificate Authorities”
    36. Write-Host “Script completed.”
       
  1. Run the script (Replace my names with your names)
    .\ Install-DC-Cert.ps1 -certname signi-dc1.signifium.com -cert_years_toexpire 3

It is time to connect using the ADSignify App to verify if SSL is working. Enjoy the freedom to manage AD from anywhere.

Leave a Comment