Created
November 8, 2015 23:57
-
-
Save richardszalay/9f28efcff249cc622658 to your computer and use it in GitHub Desktop.
Powershell module for creating trusted self-signed certificates
This file contains hidden or 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
#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