Skip to content

Instantly share code, notes, and snippets.

@p0w3rsh3ll
Created November 10, 2019 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p0w3rsh3ll/28e083113133401a6e3c74bc5cbdf575 to your computer and use it in GitHub Desktop.
Save p0w3rsh3ll/28e083113133401a6e3c74bc5cbdf575 to your computer and use it in GitHub Desktop.
#Requires -Modules PSScriptAnalyzer
Function Test-ObfuscationHunter {
[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]$ObfuscationHunterModuleFilePath=
"$((Get-Module -Name ObfuscationHunter -ListAvailable |
Select-Object -First 1 -ExpandProperty Path) -replace 'psd1','psm1')"
)
Begin {
try {
$HT = @{
CustomizedRulePath = (Resolve-Path -Path $ObfuscationHunterModuleFilePath -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