Iterating multiple variables in ForEach

Notumlord

Using the below code I'm trying to find servers that have mismatching power status in our SQL database (manually entered, so prone to mistakes) and vCenter. I doesn't quite work though since at -And $VM.PowerState -like "PoweredOn" it gets the status of all servers (about 500) rather than just one server at a time, causing the statement to always fail.

# $VM = List of Virtual Machines, properties Name and PowerState
# $ServerList = List of Servers, properties Name and Status.

foreach ($serverEntry in $ServerList) {
    if ($VM.Name -contains $serverEntry.Name -And $VM.PowerState -like "PoweredOn" -And $serverEntry.Status -like "In Use") {
        Stuff will happen
    } else {
        Some other stuff
    }
 }

I tried a nested loop but can't get that to work, and since the servers aren't listed in the same order I can't use a simple counter ($i).

How do I solve this?

Martin
foreach ($serverEntry in $ServerList)
{
    $currentVM = $VM | ? {$_.Name -eq $serverEntry.Name}
    if ($currentVM.PowerState -like "PoweredOn" -And $serverEntry.Status -like "In Use") {Stuff will happen} else {Some other stuff}

 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related