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. $_"
    }
}