vmCalgary
Enthusiast
Enthusiast

Get-VMs where name contains <string>

Jump to solution

I want to get a listing of VMs where the name contains a particular string. For example purposes, the string is: red.ball.ad

Once I get this listing, I want assign a tag (OVA) to them.

 

Thanks in advance for your help.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

In its simplest form that could look something like this


Get-VM -Name "*red.ball.ad*" |
New-TagAssignment -Tag 'OVA' -Confirm:$false

But there are a couple of things that can be improved.
- the Name can be tested with a Where-clause and a -match operator, which allows a RefEX
- you can use Try-Catch to cope with a non-existing Tag (and/or Tag Category)


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

View solution in original post

2 Replies
LucD
Leadership
Leadership

In its simplest form that could look something like this


Get-VM -Name "*red.ball.ad*" |
New-TagAssignment -Tag 'OVA' -Confirm:$false

But there are a couple of things that can be improved.
- the Name can be tested with a Where-clause and a -match operator, which allows a RefEX
- you can use Try-Catch to cope with a non-existing Tag (and/or Tag Category)


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

View solution in original post

vmCalgary
Enthusiast
Enthusiast

Thanks LucD. The syntax works perfectly as you've written it, as none of the vms containing the string have been previously tagged. Your solution was far too obvious and I was making it harder than it needed to be.

0 Kudos