functions/Protect-PowershellScripts.ps1


<#
.SYNOPSIS
Apply code sign to powershell scripts
 
.DESCRIPTION
This function will apply the codesign cert found on the build server to any ps1 file located in a source directory.
 
.PARAMETER Source
location of ps1 files to search, this can be a comma separated list, Is MANDATORY
 
.PARAMETER CertLocation
Location of Certificate either LocalMachine or CurrentUser, certificate is assumed to be in root location of "My", defaults toe LocalMachine
 
.PARAMETER CertPosition
If you are aware of other certs in the store, please change this, else it will default to 0.
 
.EXAMPLE
Protect-PowershellScripts -Source $(Build.SourceDirectory)
 
#>


function Protect-PowershellScripts {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        $Source,
        [ValidateSet("LocalMachine", "CurrentUser")]
        $CertLocation = "LocalMachine",
        $CertPosition = 0
    )
    
    begin {
        
    }
    
    process {
        $certificate = Get-CodeSigningCert -CertLocation $CertLocation -CertPosition $CertPosition
        $items = Get-ChildItem $Source -Recurse -Filter *.ps1
        foreach($item in $items) { 
            Set-AuthenticodeSignature $item.FullName $certificate
        }
    }
    
    end {
        
    }
}