-
-
Save LichLord91/ab8d3b472708333f083e3e0a0228a18b to your computer and use it in GitHub Desktop.
Install Mkcert RootCA in Windows
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
########################################################### | |
# AUTHOR : LichLord91@github.com | |
# DATE : 10.06.21 | |
# Edit : N/A | |
# COMMENT : This script installs rootCA.pem from the | |
# Mkcert program if it exists and replaces any old certs if | |
# thumbprints are different. | |
# VERSION : 1.0.0 | |
########################################################### | |
# Clear any existing error messages and set all errors to stop script if found | |
$Error.clear() | |
$ErrorActionPreference = "STOP" | |
Set-StrictMode -Version latest | |
##Requires -RunAsAdministrator | |
try { | |
#Load Windows Forms and set TopMost to true for putting forms.messagebox in the foreground | |
[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') > $null | |
$form = New-Object System.Windows.Forms.Form | |
$form.TopMost = $True | |
#Check to see if script is running as admin and if not relaunch it as admin | |
If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { | |
Start-Process PowerShell -WindowStyle Hidden -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`""; | |
Exit 0; | |
} | |
} | |
catch { | |
[System.Windows.Forms.MessageBox]::Show($form,"Error Message:`n$($_.Exception.Message)",'Task Failed Successfully','OK','Error') | |
Exit 1; | |
} | |
try { | |
#Get Current running directory path | |
$RunPath = Split-Path -parent $MyInvocation.MyCommand.Definition | |
$rootCA = "$($RunPath)\rootCA.pem" | |
#Check to see if rooCA.pem file exists | |
If (!(Test-Path "$($rootCA)")) { Throw "$($rootCA) does not exist!"} | |
#Get Certificate(s) properties | |
$OldCert = Get-ChildItem -LiteralPath 'Cert:\CurrentUser\Root' -Recurse | Where-Object {$_.Issuer -like '*mkcert*'}| Select-Object * | |
$NewCert = Get-PfxCertificate -Filepath "$($rootCA)" | |
$TextParse = 'Microsoft.PowerShell.Security\Certificate::' | |
#Check to see if the same cert is already installed by comparing the oldcert (if exists) with the new | |
If ($OldCert -and $OldCert.Thumbprint -eq $NewCert.Thumbprint) | |
{ | |
[System.Windows.Forms.MessageBox]::Show($form,"MKcert rootCA is already installed!`nIssuer:`n$($OldCert.Issuer)`nPath:`n$($OldCert.PsPath.Replace($TextParse,"Cert:"))`n Press Ctrl-C to copy message ",'No Further Action Required','OK','Warning') | |
Exit 0; | |
} | |
#Else compare the thumbprints again and if different uninstall old cert with new cert. | |
elseif ($OldCert -and $OldCert.Thumbprint -ne $NewCert.Thumbprint) | |
{ | |
Remove-Item -Path $OldCert.PSPath -Force | |
Import-Certificate -Path "$($rootCA)" -CertStoreLocation cert:\CurrentUser\Root > $null | |
$CheckCert = Get-ChildItem -LiteralPath 'Cert:\CurrentUser\Root' -Recurse | Where-Object {$_.Issuer -like '*mkcert*'} | Select-Object * | |
[System.Windows.Forms.MessageBox]::Show($form,"New rootCA installed and old one removed `n New Cert Location: $($CheckCert.PSPath.Replace($TextParse,"Cert:")) `n New Cert Thumbprint: $($NewCert.Thumbprint) `n Old Cert Thumbprint: $($OldCert.Thumbprint)`n Press Ctrl-C to copy message",'Certificate Installed Successfully','OK','Exclamation') | |
Exit 0; | |
} | |
#Install new cert if all previous checks are false | |
Import-Certificate -FilePath "$($rootCA)" -CertStoreLocation cert:\CurrentUser\Root > $null | |
$CheckCert = Get-ChildItem -LiteralPath 'Cert:\CurrentUser\Root' -Recurse | Where-Object {$_.Issuer -like '*mkcert*'} | Select-Object * | |
[System.Windows.Forms.MessageBox]::Show($form,"New rootCA installed: `n Location: $($CheckCert.PSPath.Replace($TextParse,"Cert:")) `n Press Ctrl-C to copy message",'Certificate Install Success','OK','Information') | |
Exit 0; | |
} | |
catch { | |
#Catch all errors and prompt user | |
[System.Windows.Forms.MessageBox]::Show($form,"Error Message:`n$($_.Exception.Message)`n`nError in Line:`n$($_.InvocationInfo.Line)`nError in Line Number: $($_.InvocationInfo.ScriptLineNumber) `n Press Ctrl-C to copy message",'Task Failed Successfully','OK','Error') | |
Exit 1; | |
} | |
Finally { | |
#Clear out error variable and set prefernce back to Continue | |
$Error.Clear() | |
$ErrorActionPreference = "Continue" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment