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
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
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
Many thanks!
marius