Instantly share code, notes, and snippets.

Embed
What would you like to do?
#Requires -Modules PSScriptAnalyzer
Function Test-InjectionHunter {
[CmdletBinding(DefaultParameterSetName='File')]
Param (
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,ParameterSetName = 'File',Position = 0)]
[ValidateNotNullOrEmpty()]
[Alias('FilePath', 'FileName')]
[string[]]$Path = ('.\*.ps1', '.\*.psm1'),
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName = 'Code',Position=0)]
[ValidateNotNull()]
[Alias('Script', 'ScriptBlock')]
[string[]]$Code,
[Parameter(Position=1)]
[string]$InjectionHunterModuleFilePath=
"$((Get-Module -Name InjectionHunter -ListAvailable |
Select-Object -First 1 -ExpandProperty Path) -replace 'psd1','psm1')"
)
Begin {
try {
$HT = @{
CustomizedRulePath = (Resolve-Path -Path $InjectionHunterModuleFilePath -ErrorAction Stop)
ExcludeRule = 'PS*'
}
} catch {
Write-Warning -Message "Failed because $($_.Exception.Message)"
}
}
Process {
if ($HT) {
try {
if ($PsBoundParameters['Code']) {
PSScriptAnalyzer\Invoke-ScriptAnalyzer -ScriptDefinition `
"$(([System.Management.Automation.Language.Parser]::ParseInput($Code, [ref]$null, [ref]$null)).Extent)" @HT
} else {
Get-ChildItem -Path $Path -ErrorAction SilentlyContinue |
ForEach-Object {
PSScriptAnalyzer\Invoke-ScriptAnalyzer -Path $_.FullName @HT
}
}
} catch {
Write-Warning -Message "Failed because $($_.Exception.Message)"
}
}
}
End {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment