Question:
I’m working with SCSM in Powershell but running into an issue with an if statement.
I have a function that collects data based on a criteria that is passed into the function as a variable.
Example:
1 2 |
$JMLs1 = collectTickets -crit $JMLCriteria1 |
collectTickets
is the function, $JMLCriteria1
is the criteria that is passed.
This is the collectTickets
function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function collectTickets { param ( [parameter (Mandatory=$true)] $crit ) $fullDate = Get-Date if ($fullDate.DayOfWeek -eq 'Monday') { $olderThan = $fullDate.AddDays(-4) } elseif ($fullDate.DayOfWeek -eq 'Tuesday') { $olderThan = $fullDate.AddDays(-4) } else { $olderThan = $fullDate.AddDays(-2) } if ($crit -like '$JML*') { $data = Get-SCSMObject -Criteria $crit | select 'Id', 'Title', 'LastModified', 'Priority' } else { $data = Get-SCSMObject -Criteria $crit | Where-Object {($_.LastModified -lt $olderThan)} | select 'Id', 'Title', 'LastModified', 'Priority' } return $data } |
The issue I’m having is with the second if statement, the if ($crit -like '$JML*')
– I’m not sure if we can use wildcards like this against variables or if the syntax is just not correct.
Just to clarify there will be multiple $JML
criteria variables as well as multiple other criteria variables, but it’s only the $JML
criteria variables that I want to treat differently.
Answer:
Use double quotes instead of single. With single quotes, PowerShell thinks you are looking for the the literal string $JML
, not the variable.
1 2 |
if ($crit -like "$JML*") |
Edit: about_Quoting documentation