powershell - Create Function Parameter Accept Wild Cards -
i have function writing , create wild card process on wmi call. wondering options might be. first thought take parameter value sent , replace asterisk percent signs , have case statement use query string depending on if need use like
statement or not. on complicating , there simpler way have not found?
here top portion of function so
[cmdletbinding()] param( [parameter(mandatory=$false,valuefrompipeline=$true,valuefrompipelinebypropertyname=$true)][string]$name = "", [parameter(mandatory=$false,valuefrompipeline=$true,valuefrompipelinebypropertyname=$true)][string]$computername = $env:computername, [parameter()][string]$port = "", [parameter()][switch]$full ) process { if($computername.trim()) { try { $printers = (get-wmiobject -query "select * win32_printer portname '%$port%' , name '%$name%' " -computername $computername -enableallprivileges -erroraction stop)
this kind of trying no avail.
we clear clutter function show bare minimum needed prove our point. like
's strong point using wildcards comparison still function without them. caveat could performance issue , =
better. point being that, simplicity sake @ least, going optionally add wildcards query string.
function get-printers{ [cmdletbinding()] param( [parameter()][string]$name = "", [parameter()][string]$port = "", [parameter()][switch]$wild = $true ) $wildcard = if($wild){"%"}else{$null} get-wmiobject -query ("select * win32_printer portname '{0}$port{0}' , name '{0}$name{0}'" -f $wildcard) }
so default function use wildcards. in query string use format operator add in whatever $wildcard
determined be. if -wild
true
query this:
select * win32_printer portname '%fax%' , name '%fax%'
else, if false same query this:
select * win32_printer portname 'fax' , name 'fax'
to reiterate, latter query in mind, use of like
without wildcards produce same results if had =
.
Comments
Post a Comment