-
Notifications
You must be signed in to change notification settings - Fork 56
/
find_by_mac.ps1
61 lines (56 loc) · 1.63 KB
/
find_by_mac.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
# Define Script Parameters
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty]
[string]$Server,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty]
[string]$MacAddress
)
# Check if VMware Snappin is loaded for when ran outside of PowerCli
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Write-Host "[*] VMware Automation Snapin is not loaded, attempting to load..." -ForegroundColor Green
Try {
Add-PsSnapin VMware.VimAutomation.Core | Out-Null
}
Catch {
$Host.UI.WriteErrorLine("[-] Could not load snapping check that PowerCli is installed.")
exit
}
}
#verify we're connected to a VM host
Try {
if (get-vmhost -Server $Server -State connected -erroraction "Stop") {
$connected=$True
}
}
Catch {
#Try to connect
Try {
Write-Host -ForegroundColor Green "[*] Connecting to $Server"
$viserver=Connect-VIserver -Server $Server -errorAction "Stop"
$connected=$True
}
Catch {
$msg="[-] Failed to connect to server $Server"
Write-Warning $msg
Write-Warning $error[0].Exception.Message
}
}
$ints = Get-VM | Get-NetworkAdapter
$found = $null
Write-Host "[*] Searching for "$MacAddress -ForegroundColor Green
foreach ($i in $ints){
if ($i.MacAddress -eq $MacAddress){
$found = $i
}
}
if ($found){
Write-Host "Mac Address: "$found.macaddress -ForegroundColor Green
write-host "VM: "$found.Parent -ForegroundColor Green
}
else{
Write-Host "[-] Mac Address was not found on any VM on this server." -ForegroundColor Cyan
}