Created
October 28, 2018 01:38
-
-
Save kort3x/a2aa8a7f8e8695e19fc1203f265a9938 to your computer and use it in GitHub Desktop.
Parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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