Solved

Need a powercli script to search for VM by subnet

Posted on 2013-02-04
3
570 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 120

Assisted Solution

by:Andrew Hancock (VMware vExpert / EE MVE^2)
Andrew Hancock (VMware vExpert / EE MVE^2) 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This Micro Tutorial steps you through the configuration steps to configure your ESXi host Management Network settings and test the management network, ensure the host is recognized by the DNS Server, configure a new password, and the troubleshooting…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

740 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