Skip to content

Instantly share code, notes, and snippets.

@maxkoshevoi
Created October 18, 2019 16:21
Show Gist options
  • Save maxkoshevoi/7dec2445dbd49cf1b0542668ebbd2d58 to your computer and use it in GitHub Desktop.
Save maxkoshevoi/7dec2445dbd49cf1b0542668ebbd2d58 to your computer and use it in GitHub Desktop.
Creates and installs certificate with manual "Issued by" and "Issued to"
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
$filepath = $MyInvocation.MyCommand.Path
$dirpath = Split-Path $filepath
$cred = Get-Credential -UserName 'localhost' -Message "$('Enter site name and password for certificate below')"; $site = $cred.UserName; $pwd = $cred.Password #this will open prompt to enter a password manually
#$site = 'localhost'; $pwd = ConvertTo-SecureString -String "1" -Force -AsPlainText #1 is password for certificate
if ($pwd -and $site){
$cert = New-SelfSignedCertificate -Type Custom -Subject "CN=dev_cert" -CertStoreLocation cert:\localmachine\my -DnsName $site -KeyUsage DataEncipherment -KeyUsageProperty All -KeyAlgorithm RSA -KeyLength 2048 -NotAfter (Get-Date).AddYears(3)
$name = $cert.PSChildName
Export-Certificate -cert cert:\localMachine\my\$name -FilePath $dirpath\$site.cer -type CERT -NoClobber
Export-PfxCertificate -cert cert:\localMachine\my\$name -FilePath $dirpath\$site.pfx -Password $pwd
}else{
Write-Host "Site name or Password was not provided"
[System.Console]::ReadLine()
}
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
$filepath = $MyInvocation.MyCommand.Path
$dirpath = Split-Path $filepath
$files = Get-ChildItem -Path $dirpath\* -Include *.pfx
$files | %{
#$pwd = Get-Credential -UserName 'Enter password below' -Message "$('Enter password for ""'+$_.Name+'""')"; $pwd = $pwd.Password #this will open prompt to enter a password manually
$pwd = ConvertTo-SecureString -String "1" -Force -AsPlainText #1 - password for certificate
if ($pwd){
Import-PfxCertificate -FilePath $_.FullName -CertStoreLocation Cert:\LocalMachine\Root -Password $pwd
}
}
if ($files.length -eq 0){
Write-Host "No *.pfx files found"
[System.Console]::ReadLine()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment