Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Powershell module for creating trusted self-signed certificates
#requires -Version 2.0
#region Exported Cmdlets
<#
.SYNOPSIS
Creates a self-signed certificate and copies it into the trusted store.
.DESCRIPTION
Creates a self-signed certificate and copies it into the trusted store.
.PARAMETER DnsName
The DNS name for which a certicate should be issued. eg mysite.local
.EXAMPLE
# New-TrustedSelfSignedCertificate mysite.local
Description
-----------
Creates a self-signed certificate for mysite.local
#>
function New-TrustedSelfSignedCertificate {
[CmdletBinding()]
param (
[Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[String] $DnsName,
[switch] $LocalMachine = $false
)
process {
$ErrorActionPreference = "Stop"
$cert = New-SelfSignedCertificate -DnsName $DnsName -CertStoreLocation Cert:\LocalMachine\My
if ($LocalMachine) {
$CertLocation = "LocalMachine";
} else {
$CertLocation = "CurrentUser";
}
# Cert provider does not support Copy-Item, so we'll copy it manually
$dstStore = New-Object System.Security.Cryptography.X509Certificates.X509Store("Root", $CertLocation)
$dstStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$dstStore.Add($cert)
$dstStore.Close()
}
}
#endregion
#region Module Interface
Export-ModuleMember New-TrustedSelfSignedCertificate
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment