I am trying out powercli, it has been helpful for gathering information about VM's.
I have 2 questions, how do I show connected vsphere servers, I can see the hosts, but not the vsphere server itself.
(I want to be able to find what vsphere server manages particular VM's, so I can have a webpage report that server, and someone then can use their vsphere client GUI to open the VM. In the future we will be consolidating down to 1, but for now there are several.)
Sometimes the reported name of a VM is wrong, so I'd like to use -like to find the VM easier. What would a simple lookup be that used -like?
THX!!
The connected vSPhere server are in the variable $global:defaultviservers.
In the Name parameter of the Get-VM cmdlet you can already use some masking.
For example '-Name VM*' will return all VMs whose displayname starts with VM.
There is also a -like operator.
The mask is the same that you would use in the -Name parameter.
For example: Get-VM | where{$_.Name -like "VM*"}
It will return all VMs whose displayname starts with VM.
But the operator with the most possibilities is the -match operator.
It allows a RegEx expression.
For example: where{$_.Name -match "XYZ$"
will return all VMs whose displayname ends (the $ character in the mask) with XYZ.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Lucd,
While that looked promising, I am thinking that there may not be a solution to this. I tried your response, and while it works if there are no mistakes up to the wildcard, it does not work with a simple mistake. EX: Get-VM | where{$_.Name -like "server1*"} )
(Where server1 is server1.domain.local but if I put in server2* I do not get shown server1.domain.local )
We have several thousand VM's and the endusers that have applications running on them, occasionally they send the wrong server name, EX: srever1.domain.local instead of server1.domain.local. I was hoping that -like would show names that are "like" the name with the mistake, and save time looking through them all to get the correct name.
Thx again.
I think you are looking for something that is called a "fuzzy" search.
There is a module, named Communary.PASM, that has a number of these available.
See also the post Fuzzy Search with PowerShell
Some tests I just ran
Import-Module Communary.PASM
$test = 'server1','sever1','srever1','server'
Write-Host 'Fuzzy' -ForegroundColor Green
$test | Select-FuzzyString -Search 'server1' | Out-Default
Write-Host 'SoundEx' -ForegroundColor Green
$test | Select-SoundexString -Search 'server1' | Out-Default
Write-Host 'Approximate' -ForegroundColor Green
$test | Select-ApproximateString -Search 'server1' | Out-Default
The results
In the case you mentioned, you could try
Import-Module Communary.PASM
Get-VM | where{Select-ApproximateString -Data $_.Name -Search 'server1'} |
Select Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference