Could you please try this?
function Search-VMAssociatedToIP{
param(
$ArrayOfIPToCheck
)
process{
$VMsAndAssociatedIP = get-view -viewtype "virtualmachine" | foreach-object{
$VM = $_
$_.guest.net | foreach-object{
$Net = $_
$_.IpAddress | foreach-object{
$IpAddress = $_
$Output = New-Object -Type PSObject -Prop ([ordered]@{
'VM' = $VM.Name
'NetworkName' = $Net.Network
'IpAddress' = $IpAddress
})
Return $Output
}
}
}
$ArrayOfIPToCheck | foreach-object{
$IPtoCheck = $_
$VMsAndAssociatedIP | where {$_.IpAddress -eq $IPtoCheck} | foreach-object{
$Output = New-Object -Type PSObject -Prop ([ordered]@{
'IpToCheck' = $IPtoCheck
'VM' = $_.VM
'NetworkName' = $_.NetworkName
'IpAddress' = $_.IpAddress
})
Return $Output
}
}
}
}
$MyArray = "10.0.33.1","10.0.30.11","10.0.20.11","10.0.32.1"
Search-VMAssociatedToIP -ArrayOfIPToCheck $MyArray | ogv
Use an array as input.
First get IPs of all VMs (Only for VMs with VMware tools properly installed) based on get-view so the collection will be fast.
Then compare with the IPs provided as input.