Reputation: 589
I am having code where I am searching VM details using Get-VM -Name $VM_Name
.
It is not giving any output in below case where I have 2 entries for same VM name like below
001_MBM1P
001_MBM1P_Clone_Prj_Win
was trying something like blow but getting error
get-vm $VM_Name | where {($_.Name -match $VM_Name) -and ($VM_Name -notcontains 'Clone*')}
get-vm : 1/10/2022 11:36:52 AM Get-VM VM with name '001_MBM1P' was not found using the specified filter(s).
Please let me know how can I filter the search which will work in both cases.
Upvotes: 0
Views: 1415
Reputation: 27756
The -contains
and -notcontains
operators work on arrays only, they are not string operations.
You may use the efficient String::Contains()
method:
get-vm | where {($_.Name -match $VM_Name) -and (-not $_.Name.Contains('Clone'))}
Alternatively use the -notlike
operator:
get-vm | where {($_.Name -match $VM_Name) -and ($_.Name -notlike '*Clone*')}
Note that Contains()
is case-sensitive whereas -like
and -notlike
are not. This Q/A shows some ways for case-insensitive "contains" operation using String
methods.
Upvotes: 2