Created
August 4, 2015 17:37
-
-
Save IISResetMe/d32d483969beec0e5af7 to your computer and use it in GitHub Desktop.
Very frail PowerShell string-filter 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-SimpleFilter { | |
param([string]$Filter) | |
$err = @() # TODO inspect parsing errors | |
$Tokens = [System.Management.Automation.PSParser]::Tokenize($Filter,[ref]$err) | |
$Elements = foreach($t in $Tokens){ | |
switch($t.Type){ | |
([System.Management.Automation.PSTokenType]::Command) { | |
'$_.{0}' -f $t.Content | |
} | |
([System.Management.Automation.PSTokenType]::String) { | |
"'{0}'" -f $t.Content | |
} | |
default { | |
$t.Content | |
} | |
} | |
} | |
[scriptblock]::Create("$($Elements -join " ")") | |
} | |
Parse-SimpleFilter "Name -like 'Bob*'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment