Powershell – if $var -like $var*

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:

collectTickets is the function, $JMLCriteria1 is the criteria that is passed.

This is the collectTickets function:

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.

Edit: about_Quoting documentation

Source:

Powershell – if $var -like $var* by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply