Solved

Need a powercli script to search for VM by subnet

Posted on 2013-02-04
3
560 Views
Last Modified: 2013-04-09
I have the following script to search for VM by ip address:

$tgtIP = "172.25.xxx.xxx"
$vms = Get-VM
foreach($vm in $vms){
  
  $vmIP = $vm.Guest.IPAddress
  foreach($ip in $vmIP){
    if($ip -eq $tgtIP) {
      Write-Host "Found the VM!" 
      $vm.Name 
    }
  }
}

Open in new window


But I need one to search for all VMs on a particular subnet... could this script be modified to do so?
0
Comment
Question by:Thaidog
3 Comments
 
LVL 35

Accepted Solution

by:
Joseph Daly earned 167 total points
ID: 38852001
I would think this would be very simple with a few changes. This should work using the -like parameter.

$tgtIP = "172.25.xxx."
$vms = Get-VM
foreach($vm in $vms){
  
  $vmIP = $vm.Guest.IPAddress
  foreach($ip in $vmIP){
    if($ip -like '$tgtIP*') {
      Write-Host "Found the VM!" 
      $vm.Name 
    }
  }
}

Open in new window

0
 
LVL 117

Assisted Solution

by:Andrew Hancock (VMware vExpert / EE MVE)
Andrew Hancock (VMware vExpert / EE MVE) earned 167 total points
ID: 38852022
scripts are included in

VMware Community Pack
http://www.virtu-al.net/featured-scripts/vmware-powerpack/

to find VMs by IP Address and MAC Address
0
 
LVL 1

Assisted Solution

by:Sitaram Pamarthi
Sitaram Pamarthi earned 166 total points
ID: 38854704
Hi, You can do like this.

Function Test-SameSubnet {
param (
[parameter(Mandatory=$true)]
[Net.IPAddress]
$ip1,

[parameter(Mandatory=$true)]
[Net.IPAddress]
$ip2,

[parameter()]
[alias("SubnetMask")]
[Net.IPAddress]
$mask ="255.255.255.0"
)

if (($ip1.address -band $mask.address) -eq ($ip2.address -band $mask.address)) {$true}
else {$false}

}

$tgtIP = "172.25.xxx.xx"
$vms = Get-VM
foreach($vm in $vms){
 
  $vmIP = $vm.Guest.IPAddress
  foreach($ip in $vmIP){
if(test-Samesubnet -Ip1 $tgtIP -Ip2 $ip -SubnetMask "255.255.255.0") {
      Write-Host "Found the VM in same subnet"
      $vm.Name

    }
  }
}

In the above code Test-SameSubnet is a function that takes two IPs and a subnet mask as arguments and determines if both IPs belong to same subnet. below is the source of Test-Samesubnet function.

http://get-powershell.com/post/2010/01/30/Determining-if-IP-addresses-are-on-the-same-subnet.aspx

Hope this helps.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
Teach the user how to join ESXi hosts to Active Directory domains Open vSphere Client: Join ESXi host to AD domain: Verify ESXi computer account in AD: Configure permissions for domain user in ESXi: Test domain user login to ESXi host:
This video shows you how to use a vSphere client to connect to your ESX host as the root user. Demonstrates the basic connection of bypassing certification set up. Demonstrates how to access the traditional view to begin managing your virtual mac…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now