Skip to content

Instantly share code, notes, and snippets.

@kort3x
Created October 28, 2018 01:38
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 kort3x/a2aa8a7f8e8695e19fc1203f265a9938 to your computer and use it in GitHub Desktop.
Save kort3x/a2aa8a7f8e8695e19fc1203f265a9938 to your computer and use it in GitHub Desktop.
Parser
function parse-dypCommand ($Command) {
$tokens = $null
$errors = $null
[System.Management.Automation.Language.Parser]::ParseInput($command, [ref]$tokens, [ref]$errors) | Out-Null
foreach($token in $tokens){
$index = (0..($tokens.Count-1)) | where {$tokens[$_] -eq $token}
$token | Add-Member -NotePropertyName Index -NotePropertyValue $index
}
$commands = $tokens | where tokenflags -eq "commandname"
foreach($command in $commands){
$defaultParameterSet = $null
$mandatoryParameterInDefaultSet = $null
try{
$gcm = Get-Command $command -ErrorAction Stop
if ($gcm.CommandType -eq "Alias"){
$gcm = $gcm.ResolvedCommand
}
$defaultParameterSet = $gcm.DefaultParameterSet
$mandatoryParameterInDefaultSet = $gcm.ParameterSets | where name -EQ $defaultParameterSet | Select-Object -ExpandProperty parameters | where ismandatory -EQ $true
}catch{
}finally{
if($mandatoryParameterInDefaultSet){
$command | Add-Member -NotePropertyName HasMandatoryParameter -NotePropertyValue $true
if(($tokens[$command.index+1]).TokenFlags -ne "None"){
$command | Add-Member -NotePropertyName IsMissingMandatoryParameter -NotePropertyValue $true
}else{
$command | Add-Member -NotePropertyName IsMissingMandatoryParameter -NotePropertyValue $false
}
}else{
$command | Add-Member -NotePropertyName HasMandatoryParameter -NotePropertyValue $false
$command | Add-Member -NotePropertyName IsMissingMandatoryParameter -NotePropertyValue $false
}
}
}
if($commands.IsMissingMandatoryParameter -contains $true){
return $false
}else{
return $true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment