Last active
November 19, 2023 02:23
-
-
Save acast15/1d5bf9c784cb5c4576bb90f23f9c92a2 to your computer and use it in GitHub Desktop.
PowerShell function to trust a certificate
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
function Trust-Certificate { | |
# Prompt the user to input the certificate's path | |
$certPath = Read-Host "Enter path to certificate" | |
# Create a new X.509 certificate object | |
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 | |
try { | |
# Import the certificate from the specified path | |
$cert.Import($certPath) | |
# Create a new X.509 store for Trusted Root Certification Authorities on the Local Machine | |
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("Root", "LocalMachine") | |
try { | |
# Open the store, add the certificate to the store, and close the store | |
$store.Open("ReadWrite") | |
$store.Add($cert) | |
} | |
finally { | |
$store.Close() | |
} | |
Write-Host "Certificate imported successfully." | |
} | |
catch { | |
Write-Host "Error importing the certificate: $_" | |
} | |
} | |
# Uncomment the below if you wish to immediately call the function | |
# Trust-Certificate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment