Looking for a script that will match vm's to IP's but need to be able to search several ip's at once.
I have put this together from scripts on the net, which works well to search one IP at a time:
$match_address = Read-Host "Enter IP address to find"
Get-VM | %{
$vmips = $_.Guest.IPAddress
foreach($ip in $vmips) {
if ($ip -like $match_address) {
"Found VM with matching address: {0}" -f $_.Name
}
}
}
But would need to be able to enter several IP's from different subnets into the $match_address and have the script search for a match for each ip.
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.
Try this function I wrote for this purpose:
function Search-VMIP {
<#.SYNOPSIS
Searches for VMs by IP-Addresses specified as a comma-separated list.
.PARAMETER IPs
Comma-separated list of IP-Addresses to be used for the search.
.EXAMPLE
Search-VMIP -IPs '10.1.1.1,10.2.2.2,192.168.1.1'
#Requires -Version 2.0
#>[CmdletBinding()]
Param(
[string][parameter(Mandatory=$true)] $IPs
)
$VMs = Get-VM
$IPList = $IPs -split ','
ForEach ($IP in $IPList) {
if (!($IP -As [IPAddress]) -As [Bool]) {
Throw "Error: $IP does not look like a valid IP address."
}
else {
$ret = $VMs | Where-Object {$_.Guest.IPAddress -eq $IP} | Select Name, @{N='IP'; E={$_.Guest.IPAddress}}
if ($ret) {
$ret
}
else {
Write "Could not find a VM with IP $IP."
}
}
}
}
Use it like Search-VMIP -IPs '10.1.1.1,10.2.2.2,192.168.1.1'
Thanks for the quick response but unfortunately the script does not work for me. When I enter Search-VMIP -IPs "ipaddress,ipaddress" the script does not even search it goes imediatlly back to a prompt.
I've also tried this which work with individual IP's but as soon as more then one ip is entered the results are blank?
$match_address = Read-Host "Type IPs Coma Seperated"
$IPList = $match_address -split ','
ForEach ($IP in $IPList)
{
Get-VM | %{
$vmIPs = $_.Guest.IPAddress
foreach($ip in $vmIPs) {
if ($ip -eq $IPList) {
"Found VM with matching address: {0}" -f $_.Name
}
}
}
}
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.