Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need a powercli script to search for VM by subnet

Posted on 2013-02-04
3
Medium Priority
?
624 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 668 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 124

Assisted Solution

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ransomware is a malware that is again in the list of security  concerns. Not only for companies, but also for Government security and  even at personal use. IT departments should be aware and have the right  knowledge to how to fight it.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

886 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