Private/Split-PemChain.ps1

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
function Split-PemChain {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$ChainFile
    )

    $PEM_BEGIN = '-----BEGIN *'
    $PEM_END   = '-----END *'

    # resolve relative path
    $ChainFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($ChainFile)

    $chainLines = Get-Content $ChainFile

    for ($i=0; $i -lt $chainLines.Count; $i++) {
        if ($chainLines[$i] -like $PEM_BEGIN) {
            $iStart = $i
            continue
        }
        if ($chainLines[$i] -like $PEM_END) {
            $pemLines = $chainLines[$iStart..$i]
            Write-Output @(,$pemLines)
            continue
        }
    }

}