Public/Convert-Sf15to18.ps1

<#
    .SYNOPSIS
    Converts a Salesforce 15 character identifier to 18 characters

    .DESCRIPTION

    .INPUTS
    The salesforce 15 character Id

    .OUTPUTS
    The salesforce 18 character id

    .PARAMETER Id
    The salesforce 15 character Id


    .EXAMPLE
    C:\PS> ConvertSf15To18 "a173t00000LOyZn"

    .LINK
    .NOTES
#>

function Convert-Sf15To18 {

    [CmdletBinding()]
    [OutputType([String])]
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Id
    )

    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }

    process {
        Write-Debug "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"

        if ($Id.Length -ne 15) {
            throw "Id is not 15 characters"
        }

        $InChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345"
        $InUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray() | % { [int][char]$_ }
        $SalesForceID_Rev = $Id | % { -join $_[$_.Length..0] }
        $SalesForceID_RevArray = $SalesForceID_Rev.ToCharArray() | % { [int][char]$_ }
        $Checksum = ""
        $InCnt = 0
        $pos = 15
        foreach ($Char in $SalesForceID_RevArray) {
            $InCnt = (2 * $InCnt) + $InUpper.Contains($Char)
            if ($pos % 5 -eq 1) {
                $Checksum = $InChars.Substring($InCnt, 1) + $Checksum
                $InCnt = 0
            }
            $pos = $pos - 1
        }
        Write-Output "$($Id)$($Checksum)"
    }
}