VMware Cloud Community
Leffingwell
Contributor
Contributor
Jump to solution

Can you use * in conditionals?

Hey All,

Simple scenario.

We have VM's that somewhere in their name have 'TR' to designate them as a training machine.

I have a script that already filters VM's from the Get-VM cmdlet, but I was recently asked to tailor it to grab the TR VM's and make a special note in a log.

I figured this would be simple, but my logic is appearing not to work, just looking for some feedback Smiley Happy

Get-VM |
    %{
        if ($_.Vm.Name -eq '*tr*')

        {

<ommitted>

But for some reason it doesn't pick up any VM's out of the whole list with 'TR' in them.

Thanks in advance!

Kindest Regards,

ALAN

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can use the -like

if ($_.Vm.Name -like '*tr*')

or -match parameter

if ($_.Vm.Name -match 'tr')


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

You can use the -like

if ($_.Vm.Name -like '*tr*')

or -match parameter

if ($_.Vm.Name -match 'tr')


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Leffingwell
Contributor
Contributor
Jump to solution

Good morning LucD!

The first one you posted - no dice

The second idea you had worked like a charm!

Thanks as usual for your snappy response,

Kindest Regards,

ALAN

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Oops, typo. That should have been asterisk instead of percent sign


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Leffingwell
Contributor
Contributor
Jump to solution

Well ...... I THINK I can forgive you Smiley Happy  -ALAN

0 Kudos