Skip to content

Instantly share code, notes, and snippets.

@richardszalay
Created November 8, 2015 23:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save richardszalay/9f28efcff249cc622658 to your computer and use it in GitHub Desktop.
Save richardszalay/9f28efcff249cc622658 to your computer and use it in GitHub Desktop.
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