Public/Add-Signature.ps1

Function Add-Signature {
    param(
        [Parameter(ParameterSetName="File")]
        [string]$FilePath,

        [Parameter(ParameterSetName="Directory")]
        [string]$Directory,

        [Parameter(ParameterSetName="File", Mandatory=$true)]
        [Parameter(ParameterSetName="Directory", Mandatory=$true)]
        [string]$Hash,

        [Parameter(ParameterSetName="File")]
        [Parameter(ParameterSetName="Directory")]
        [string]$Sha1
    )

    try {
        switch ($PSCmdlet.ParameterSetName) {
            "File" {
                $Command = "signtool.exe sign /a /fd $Hash $FilePath"

                if ($Sha1 -ne "") {
                    $Command = "signtool.exe sign /sha1 $Sha1 /fd $Hash $FilePath"
                }

                Invoke-Expression $Command
            }
            "Directory" {
                $ChildItems = Get-ChildItem $Directory -Recurse

                $ChildItems | ForEach-Object {
                    $Command = "signtool.exe sign /a /fd $Hash $($_.FullName) /q"

                    if ($Sha1 -ne "") {
                        $Command = "signtool.exe sign /sha1 $Sha1 /fd $Hash $($_.FullName) /q"
                    }

                    Invoke-Expression $Command
                }                
            }
        }  
    } catch {
        Write-Error "Unable to complete signing. $_"
    }
}
# SIG # Begin signature block
# MIIDOQYJKoZIhvcNAQcCoIIDKjCCAyYCAQExDzANBglghkgBZQMEAgMFADCBmwYK
# KwYBBAGCNwIBBKCBjDCBiTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63
# JNLGKX7zUQIBAAIBAAIBAAIBAAIBADBRMA0GCWCGSAFlAwQCAwUABECqzf/RYI/O
# vp73JIicWfcl4u1uIXXN5xpdQtGKM4XntnbedSRjZT5oe46pIgVY4gwGvVeWn1ZR
# r4FicZUScfG+oIIBRTCCAUEwgeigAwIBAgIUJUrhIyYe0Ijmq8lkl4K8jyNrTDUw
# CgYIKoZIzj0EAwIwIDEeMBwGA1UEAwwVY29ubm9yQG5vdXNlbGVzcy50ZWNoMCAX
# DTI0MDUwNTA0MDUyOVoYDzIyMjIxMjIyMDAwMDAwWjAgMR4wHAYDVQQDDBVjb25u
# b3JAbm91c2V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