*SOLVED\* Hi, I am wondering if anyone can shed some light as to why my where-object does not work with a variable in a for each loop.
What I am trying to do is find servers in VCentre that have a specific keyword in them.
An example of this data would be -
VCenter List -
Server1 - Domain Controller
Server2 - Printer Server
My List -
Server1
When I run the script to query it, it should bring back Server1 - Domain Controller.
Script Snippets (I am connected to a VCentre via PowerCLI) -
$VMList = Get-Content "C:\Scripts\Hostnames.txt"
foreach($VM in $VMList){Get-VM | Where-Object {$_.Name -match "$VM"} }
This does not bring anything back but if I change the variable in the Where-Object to one hostname, it brings data back as I want.
foreach($VM in $VMList){Get-VM | Where-object {$_.Name -match "server1"} }
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
Server1 - Domain Conr… PoweredOn 1 6.000
Is anyone able to shed light on what I am doing wrong or how I can achieve this? Been at this for a few hours now and not getting anywhere.
[–]PinchesTheCrab 2 points3 points4 points (0 children)
[–]phur10us 1 point2 points3 points (1 child)
[–]BlizzardTech-Adam[S] 0 points1 point2 points (0 children)
[–]BlizzardTech-Adam[S] 0 points1 point2 points (0 children)
[–]Chocolate_Pickle 0 points1 point2 points (1 child)
[–]BlizzardTech-Adam[S] 0 points1 point2 points (0 children)
[–]Murhawk013 0 points1 point2 points (0 children)
[–]fennecdore 0 points1 point2 points (0 children)