Solved

Need a powercli script to search for VM by subnet

Posted on 2013-02-04
3
562 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 118

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Last article we focus in how to VMware: How to create and use VMs TAGs – Part 1 so before follow this article and perform the next tasks, you should read the first article how to create the TAG before using them in Veeam Backup Jobs.
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
Teach the user how to use vSphere Update Manager to update the VMware Tools and virtual machine hardware version Open vSphere Client: Review manual processes for updating VMware Tools and virtual hardware versions: Create a new baseline group in vSp…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 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

21 Experts available now in Live!

Get 1:1 Help Now