-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-if-name.ps1
113 lines (91 loc) · 3.7 KB
/
check-if-name.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<#
.SYNOPSIS
Checks the ip-address and resolves name(s) for active interface(s)
.DESCRIPTION
Does a forward lookup for the hostname and a reverse lookup for the ip-addresses for active interfaces
.EXAMPLE
./
.NOTES
Results differ depending if computer is AD-connected, have several active interfaces and have ipv6-addresses.
Prints the Primary-domain suffix and Primary-NVdomain suffix, just in case....
Domain is the current domain, NV-domain is one the after reboot.
Needs some better errorhandling
Some dead code currently in there.....
.LINK
http://virot.eu/getting-the-computername-in-powershell/
#>
#$hostname = ($env:ComputerName + "." + $env:UserDnsDomain).ToLower()
$hostname = (Get-CIMInstance CIM_ComputerSystem).Name.ToLower()
$primarydomain = (Get-CIMInstance CIM_ComputerSystem).Domain.ToLower()
$interface = (Get-NetAdapter| Select-Object Name,ifIndex,Status| Where Status -eq Up)
# Stupid stuff, Primary DNS-suffix and Non Volatile Domain
$domain= (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\").Domain
$nvdomain = (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\")."NV Domain"
"{0,-20}{1,-30}" -f "Local hostname = ","$hostname"
"{0,-20}{1,-30}" -f "Primary AD-domain = ","$primarydomain"
"{0,-20}{1,-30}" -f "Primary domain suffix = ","$domain"
"{0,-20}{1,-30}" -f "Primary nvdomain suffix = ","$nvdomain`n"
# Get interfaces that are up and print the ip-address
foreach ($if in $interface) {
# Check for en IPv4 address for the interface
try {
$ipv4 = (Get-NetIPAddress -ifIndex ($if).ifIndex -Type Unicast -AddressFamily IPv4 -ErrorAction Stop).IPAddress
}
catch {
$ipv4 = ""
}
# Check for en IPv6 address for the interface
try {
$ipv6 = (Get-NetIPAddress -ifIndex ($if).ifIndex -Type Unicast -AddressFamily IPv6 -ErrorAction Stop).IPAddress
}
catch {
$ipv6 = ""
}
$ifName = ($if).Name
$ifIndex = ($if).ifIndex
try {
$conspecsuffix = (Get-DnsClient -InterfaceIndex $ifIndex -ErrorAction Stop).ConnectionSpecificSuffix
}
catch {}
# If there is a domain suffix for interface, print it.
if ($conspecsuffix) {
"{0,-54}{1,-3}{2,-35}" -f "Conn. specific suffix for $ifName ($ifIndex)"," = ","$conspecsuffix"
}
# Write every ipv4 address for the interface on a separate line
foreach ($addr_4 in $ipv4) {
"{0,-54}{1,-3}{2,-15}" -f "Interface $ifName ($ifIndex) has ipv4-address"," = ","$addr_4"
}
# Write every ipv6 address for the interface on a separate line
foreach ($addr_6 in $ipv6) {
"{0,-54}{1,-3}{2,-30}" -f "Interface $ifName ($ifIndex) has ipv6-address"," = ","$addr_6"
}
# Check for the reverse record in DNS
try {
$dnshost = (Resolve-DnsName -Name $ipv4 -DnsOnly -ErrorAction Ignore).NameHost #Reverselookup
}
catch {}
if ($dnshost) {
$dnshost= $dnshost.ToLower()
"{0,-54}{1,-3}{2,-40}" -f "Reverse DNS lookup for $ipv4"," = ", "$dnshost"
# In case there is no name for dnshost
try {
$faddr = (Resolve-DnsName -Name $dnshost -DnsOnly -ErrorAction Stop).IpAddress #Forward Lookup
}
# This part is probably dead
catch {
Write-Output "No name for $ipv4"
#Break
}
# For every ip-address in the forward lookup, resolve to a name (reverse lookup)
finally {
foreach ($name in $faddr) {
"{0,-54}{1,-3}{2,-40}" -f "Forward DNS lookup for $dnshost", " = ", "$name"
}
Write-Output ""
}
}
else {
# The ip-address has no record in DNS
Write-Output "No reverse lookup for $ipv4 in DNS.`n"
}
}