-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathGet-Network.ps1
265 lines (230 loc) · 6.76 KB
/
Get-Network.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<#
.SYNOPSIS
Determines the most likely candidate for the active Internet-specific network adapter on this
machine. All other adpaters such as tunneling and loopbacks are ignored. Only connected IP
adapters are considered.
#>
param(
[switch] $preferred, # just return the preferred address
[switch] $addresses, # return a list of host addresses
[switch] $verbose) # verbose report
Begin
{
function Get-Addresses ()
{
$addresses = @()
if ([Net.NetworkInformation.NetworkInterface]::GetIsNetworkAvailable())
{
[Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | % `
{
$props = $_.GetIPProperties()
$address = $props.UnicastAddresses `
| ? { $_.Address.AddressFamily -eq 'InterNetwork' } `
| select -first 1 -ExpandProperty Address
if ($address)
{
$addresses += $address.IPAddressToString
}
}
}
$addresses
}
function Get-Preferred ()
{
$prefs = @()
if ([Net.NetworkInformation.NetworkInterface]::GetIsNetworkAvailable())
{
[Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | % `
{
if (($_.NetworkInterfaceType -ne 'Loopback') -and ($_.OperationalStatus -eq 'Up'))
{
$props = $_.GetIPProperties()
$address = $props.UnicastAddresses `
| ? { $_.Address.AddressFamily -eq 'InterNetwork' } `
| select -first 1 -ExpandProperty Address
$DNSServer = $props.DnsAddresses `
| ? { $_.AddressFamily -eq 'InterNetwork' } `
| select -first 1 -ExpandProperty IPAddressToString
if ($address -and $DNSServer)
{
$prefs += $address.IPAddressToString
}
}
}
}
if ($prefs.Length -gt 0)
{
return $prefs[0]
}
return $null
}
function Get-Information ()
{
$preferred = $null
$items = @()
if ([Net.NetworkInformation.NetworkInterface]::GetIsNetworkAvailable())
{
[Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | % `
{
if ($_.NetworkInterfaceType -ne 'Loopback')
{
$item = New-Object PSObject -Property @{
Address = $null
PhysicalAddress = $_.GetPhysicalAddress().ToString()
DNSServer = $null
Gateway = $null
Description = $null
DnsSuffix = $null
BytesReceived = 0
BytesSent = 0
Status = $_.OperationalStatus
Type = $_.NetworkInterfaceType
}
$props = $_.GetIPProperties()
$item.Address = $props.UnicastAddresses `
| ? { $_.Address.AddressFamily -eq 'InterNetwork' } `
| select -first 1 -ExpandProperty Address
$item.DNSServer = $props.DnsAddresses `
| ? { $_.AddressFamily -eq 'InterNetwork' } `
| select -first 1 -ExpandProperty IPAddressToString
$item.Gateway = $props.GatewayAddresses `
| ? { $_.Address.AddressFamily -eq 'InterNetwork' } `
| select -first 1 -ExpandProperty Address
if ($verbose)
{
$stats = $_.GetIPv4Statistics() | Select -first 1
$item.BytesReceived = $stats.BytesReceived
$item.BytesSent = $stats.BytesSent
}
$item.Description = $_.Name + ', ' + $_.Description
$item.DnsSuffix = $props.DnsSuffix
if (($props.DnsSuffix -ne $null) -and ($props.DnsSuffix.Length -gt 0))
{
if ($item.Type.ToString().StartsWith('Wireless'))
{
$ssid = netsh wlan show interfaces | Select-String '\sSSID'
if ($ssid)
{
$profile = $ssid.ToString().Split(':')[1].Trim()
if ($profile) { $item.Description += (', ' + $profile) }
}
}
else
{
$item.Description += (', ' + $props.DnsSuffix)
}
}
if ((!$preferred) -and ($item.Status -eq 'Up') -and $item.Address -and $item.DNSServer)
{
$preferred = $item.Address
}
$items += $item
}
}
}
@{
Preferred = $preferred
Items = $items
}
}
function Show-Preferred ($preferred)
{
Write-Host
if ($preferred -eq $null)
{
Write-Host ('{0} Preferred address is unknown' -f $env:COMPUTERNAME) -ForegroundColor DarkGreen
}
else
{
Write-Host ("{0} Preferred address is {1}" -f $env:COMPUTERNAME, $preferred) -ForegroundColor Green
}
}
function Get-ForeColor ($item, $preferred)
{
if ($item.Status -ne 'Up') { @{ foregroundcolor='DarkGray' } }
elseif ($item.Address -eq $preferred) { @{ foregroundcolor='Green' } }
elseif ($item.Type -match 'Wireless') { @{ foregroundcolor='Cyan' } }
elseif ($item.Description -match 'Bluetooth') { @{ foregroundcolor='DarkCyan' } }
else { @{ } }
}
function Show-Information ($info)
{
Write-Host
Write-Host 'Address DNS Server Gateway Interface'
Write-Host '------- ---------- ------- ---------'
$info.Items | % `
{
$line = ("{0,-15} {1,-15} {2,-15} {3}" -f $_.Address, $_.DNSServer, $_.Gateway, $_.Description)
$hash = Get-ForeColor $_ $info.Preferred
Write-Host $line @hash
}
}
function Show-Verbose ($info)
{
Write-Host
Write-Host 'IP/DNS/Gateway Interface Details'
Write-Host '-------------- -----------------'
$info.Items | % `
{
for ($i = 10; $i -gt 0; $i -= 2) { $_.PhysicalAddress = $_.PhysicalAddress.insert($i, '-') }
$hash = Get-ForeColor $_ $info.Preferred
Write-Host ("{0,-15} {1}" -f $_.Address, $_.Description) @hash
Write-Host ("{0,-15} Physical Address.. {1}" -f $_.DNSServer, $_.PhysicalAddress) -ForegroundColor DarkGray
Write-Host ("{0,-15} Type.............. {1}" -f $_.Gateway, $_.Type) -ForegroundColor DarkGray
if ($_.Status -eq 'Up')
{
Write-Host ("{0,16} Bytes Sent........ {1:N0}" -f '',$_.BytesSent) -ForegroundColor DarkGray
Write-Host ("{0,16} Bytes Received.... {1:N0}" -f '',$_.BytesReceived) -ForegroundColor DarkGray
if ($_.DnsSuffix)
{
Write-Host ("{0,16} DnsSuffix......... {1}" -f '',$_.DnsSuffix) -ForegroundColor DarkGray
}
}
Write-Host
}
}
}
Process
{
if ($preferred)
{
return Get-Preferred
}
if ($addresses)
{
return Get-Addresses
}
$info = Get-Information
if ($info -and $info.Items -and $info.Items.Count -gt 0)
{
Show-Preferred $info.Preferred
if ($verbose)
{
Show-Verbose $info
}
else
{
Show-Information $info
}
}
else
{
Write-Host 'Network unavailable' -ForegroundColor Red
}
}
<#
... This is a whole lot less code but is much slower then the code above
$candidates = @()
Get-NetIPConfiguration | % `
{
$dns = $_.DNSServer | ? { $_.AddressFamily -eq 2 } | select -property ServerAddresses | select -first 1
$ifx = $_.InterfaceAlias + ', ' + $_.InterfaceDescription
if ($_.NetProfile.Name -notmatch 'Unidentified') { $ifx += (', ' + $_.NetProfile.Name) }
$candidates += New-Object psobject -Property @{
Address = $_.IPv4Address.IPAddress
DNSServer = [String]::Join(',', $dns.ServerAddresses)
Gateway = $_.IPv4DefaultGateway.NextHop
Interface = $ifx
}
}
#>