Certificates.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Set-StrictMode -Version Latest

function Import-Certificate
{
    <#
    .SYNOPSIS
        This function imports a certificate into any certificate store on a local computer
    .EXAMPLE
        PS> Import-Certificate -Context LocalMachine -StoreName My -FilePath C:\certificate.cer
 
        This example will import the certificate.cert certificate into the Personal store for the
        local computer
    .EXAMPLE
        PS> Import-Certificate -Context CurrentUser -StoreName TrustedPublisher -FilePath C:\certificate.cer
 
        This example will import the certificate.cer certificate into the Trusted Publishers store for the
        currently logged on user
    .PARAMETER Context
         This is the Context (either CurrentUser or LocalMachine) where the store is located which the certificate
        will go into.
    .PARAMETER StoreName
        This is the certificate store that the certificate will be placed into
    .PARAMETER FilePath
        This is the path to the certificate file that you'd like to import
    #>

    [OutputType()]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateSet('CurrentUser', 'LocalMachine')]
        [string]$Context,
        
        [Parameter(Mandatory = $true)]
        [ValidateScript({
            if ($Context -eq 'CurrentUser')
            {
                (Get-ChildItem Cert:\CurrentUser | Select-Object -ExpandProperty name) -contains $_
            }
            else
            {
                (Get-ChildItem Cert:\LocalMachine | Select-Object -ExpandProperty name) -contains $_
            }
        })]
        [string]$StoreName,
        
        [Parameter(Mandatory = $true)]
        [ValidateScript({ Test-Path $_ -PathType Leaf })]
        [string]$FilePath
    )
    
    begin
    {
        $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
        try
        {
            [void][System.Reflection.Assembly]::LoadWithPartialName('System.Security')
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
    
    process
    {
        try
        {
            $Cert = Get-Item $FilePath
            $Cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $Cert
            $X509Store = New-Object System.Security.Cryptography.X509Certificates.X509Store $StoreName, $Context
            $X509Store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
            $X509Store.Add($Cert)
            $X509Store.Close()
        }
        catch
        {
            Write-Log -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)" -LogLevel '3'
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}