Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Created August 4, 2015 17:37
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 IISResetMe/d32d483969beec0e5af7 to your computer and use it in GitHub Desktop.
Save IISResetMe/d32d483969beec0e5af7 to your computer and use it in GitHub Desktop.
Very frail PowerShell string-filter parser
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