-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-VM-from-IP.ps1
70 lines (60 loc) · 2.07 KB
/
Get-VM-from-IP.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<#
Author: Stan Crider
Date: 10Apr2017
What this crap does:
Take user-input IP address and search specified vCenter for virtual machines with that IP.
!!! Requirements for this script to work: !!!
1. VMware PowerCLI must be installed on the computer running this script
2. VMware Tools must be installed on the VM for this script to recognize the IP address
3. Permissions on specified vCenter to read VM properties
#>
# Specify vCenter and DataCenter
$vCenter = "VC-Server"
# Initialize counter
$FindCounter = 0
# Get user-input IP address
$VIP = Read-Host "Enter IP address to search for or 'exit' to cancel"
If($VIP -eq "exit"){
# Exit script if cancelled
Write-Output "Script cancelled."
Break
}
Else{
# Validate IP Address format
$ip_address = $null
[System.Net.IPAddress]::TryParse($VIP, [ref]$ip_address) | Out-Null
If($null -ne $ip_address){
# Import PowerCLI Module and connect to vCenter server
Write-Output ("Connecting to vCenter " + $vCenter + ". . .")
Import-Module VMware.vimautomation.core
Connect-VIserver $vCenter
# Get virtual machines
$VSrvrs = Get-VM
# $VSrvrs = Get-DataCenter $VMDataCenter | Get-VM
Write-Output ("Searching for IP address " + $VIP + ". . .")
# Find each IP address of each VM; report name and increment counter upon match
ForEach($VSrv in $VSrvrs){
$VMIPList = $VSrv.Guest.IPAddress
ForEach($VMIP in $VMIPList){
If($VMIP -eq $VIP){
Write-Output ($VSrv.Name + ": " + $VMIP)
$FindCounter++
}
}
}
# Report number of matches
If ($FindCounter -eq 0){
Write-Warning ("No VM's found with IP address " + $VIP)
}
Else{
Write-Output ("VM's found with IP address " + $VIP + ": " + $FindCounter)
}
# Close vCenter connection
Disconnect-VIserver -Confirm:$false
}
# If IP format not valid, exit script
Else{
Write-Error ("The entry " + $VIP + " is not a valid IP Address. Script terminated.")
Break
}
}