Solved

Need a powercli script to search for VM by subnet

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

732 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