VMware Cloud Community
MariusRoma
Expert
Expert
Jump to solution

Getting exact names of VMs from a .csv file

I apologize for the vary basic question.

I have a script that reads a list of VM names from a .csv file and performs some actions over each VM.

The script looks like:

Get-Module -Name VMware* -ListAvailable | Import-Module

Connect-VIServer <IP_Address>  

Import-Csv $csvName | % {

$vm = Get-View -ViewType VirtualMachine -Filter @{"Name"=$_.VMname}

$vm.name

}

My .csv name looks like:

VMname

Smith

Doe

Jones

When I print the names of the VMs I see other VMs as well with similar names, like:

Smith

Smithson

Doe

John_Doe

Jones

Jones_replica

Jones2

and so on.

How can I modify the scriopt in order to get only the VMs whose name exactly matdhes the names in the .csv file?

Regards

marius

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The right-side operand for the Filter hash is a RegEx expression.

So you can use the RegEx locators to specify that you want an exact match

Get-Module -Name VMware* -ListAvailable | Import-Module

Connect-VIServer <IP_Address> 

Import-Csv $csvName | % {

    $vm = Get-View -ViewType VirtualMachine -Filter @{"Name"="^$($_.VMname)$"}

    $vm.name

}


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

The right-side operand for the Filter hash is a RegEx expression.

So you can use the RegEx locators to specify that you want an exact match

Get-Module -Name VMware* -ListAvailable | Import-Module

Connect-VIServer <IP_Address> 

Import-Csv $csvName | % {

    $vm = Get-View -ViewType VirtualMachine -Filter @{"Name"="^$($_.VMname)$"}

    $vm.name

}


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

MariusRoma
Expert
Expert
Jump to solution

Many thanks!

marius

0 Kudos