pf-WQL.ps1
function Join-WQLFilterArguments($pairedArgs) { function ConvertToWQLLike($value) { if ($value.Contains('*')) { $value = $value.Replace('*','%') } if ($value.Contains('?')) { $value = $value.Replace('?','_') } $value } $filterList = foreach ($pairArg in $pairedArgs.GetEnumerator()) { $paramName = $pairArg.Name $paramFilterList = foreach($paramValue in $pairArg.Value) { if ( $paramValue -is [string] ) { $paramValue = ConvertToWQLLike $paramValue "$paramName LIKE '$paramValue'" } else { "$paramName = $paramValue" } } $paramFilter = $paramFilterList -join ' OR ' if (-not $paramFilter) { "( $paramName IS NOT NULL )" continue } $paramFilter = "( $paramFilter )" $paramFilter } $filter = $FilterList -join ' AND ' $filter } function Join-WQLFilterArguments:::Test{ function AnyCmd($pa,$pz) { $myInv = $MyInvocation $myArgs = Get-CmdUnboundArgs $myInv.UnboundArguments $allArgs = [Ordered]@{} $allArgs += $myInv.BoundParameters $allArgs += $myArgs Join-WQLFilterArguments $allArgs } $res = AnyCmd -pa 1 -pc 2, 3 -pd -pz 100 -pe:@(1,2,3) -pf:false -pg 'val1' -ph 4 5 -pi:$true $res } function Invoke-WQL { param( $from, [String[]]$fields ) begin{ $fieldStr = if ($fields) { $fields -join ', ' } else { '*' } $myInv = $MyInvocation $filterArgs = Get-CmdUnboundArgs $myInv.UnboundArguments $filter = Join-WQLFilterArguments $filterArgs $basequery = "SELECT $fieldStr FROM $from" if ($filter) { $basequery += " WHERE $filter" } } process { $result = Get-WmiObject -Query $basequery if ($fieldStr -ne '*' ) { $result = $result | Select-Object -Property $fields } $result } } function Invoke-WQL:::Example { $ieProcesses = Invoke-WQL -from Win32_Process -fields ProcessId, ParentProcessId, Name -name Iexplore* Write-Debug $ieProcesses } |