-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathGet-NetworkStatus.ps1
256 lines (224 loc) · 8.54 KB
/
Get-NetworkStatus.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
#Requires -version 2.0
# -----------------------------------------------------------------------------
# Script: Get-NetworkStatus.ps1
# Version: 1.0
# Author: Jeffery Hicks
# http://jdhitsolutions.com/blog
# http://twitter.com/JeffHicks
# Date: 1/18/2011
# Keywords: DNS, WMI, WinRM, Network
# Comments:
# This script assumes you are querying desktop computers with a single network card
# and IP address. It also assumes you are in a domain, running this administrator
# credentials and that your DNS server is running Microsoft Windows.
#
# "Those who neglect to script are doomed to repeat their work."
#
# ****************************************************************
# * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
# * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
# * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
# * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
# ****************************************************************
# -----------------------------------------------------------------------------
<#
.Synopsis
Get network health for a given computer.
.Description
This script runs a number of network health tests and checks for a computer
and writes a custom object to the pipeline.
.Parameter Computername
The name of the computer to check. The default is the local computer. This
parameter has an alias of -Name.
.Example
PS C:\> S:\Get-NetworkStatus.ps1 xplab
CShare : True
Computername : xplab
LeaseObtained : 1/19/2011 9:22:58 AM
WMI : True
AdapterHostname : xplab
Ping : True
AdminShare : True
DHCPServer : 192.168.56.102
WinRM : True
LeaseExpires : 1/27/2011 9:22:58 AM
DNSDomain : jdhlab.local
MACAddress : 08-00-27-AD-81-3E
IPAddress : 192.168.56.203
ReverseLookup : True
DNSHostName : xplab.jdhlab.local
.Example
PS C:\> Import-Module ActiveDirectory
PS C:\> Get-ADComputer -Filter * -SearchBase "OU=Desktops,DC=jdhlab,DC=local" | Foreach {
>> S:\Get-Networkstatus $_.Name} | Where {$_.Ping} |
>> Export-CSV -Path C:\Work\Desktop-Net-Report.csv -NoTypeInformation
This command uses the Get-ADComputer cmdlets from the Active Directory module. It queries
all the computers int he Desktops OU and creates a CSV file only for computers that reply
to a ping.
.Notes
NAME: Get-NetworkStatus.ps1
AUTHOR: Jeffery Hicks
VERSION: 1.0
LASTEDIT: 01/19/2011
Learn more with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010)
.Link
Http://jdhitsolutions.com/blog
.Link
Get-WMIObject
Get-Service
Test-Connection
Test-Path
.Inputs
Strings
.Outputs
Custom object
#>
Param(
[Parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[ValidateNotNullorEmpty()]
[Alias("name")]
[string[]]$Computername=$env:computername
)
Begin
{
Set-StrictMode -Version 2.0
#not necessarily good practice, but I'm turning off the error pipeline
#to suppress all error messages. I've set default values that should cover
#everything.
$ErrorActionPreference="SilentlyContinue"
#define some helper functions
Function Get-DefaultDNSServer {
#this is fast and easy, but perhaps not the best PowerShell
$lookup=nslookup $env:computername | select-string "Server"
#get the matching line, split it at the colon, get the last
#item from the array and trim any spaces.
$lookup.line.split(":")[1].Trim()
}
Function New-ReverseIP {
Param([string]$IPAddress)
$arr=$IPAddress.Split(".")
$Reverse="{0}.{1}.{2}.{3}" -f $arr[3],$arr[2],$arr[1],$arr[0]
Write-Output $Reverse
}
} #close Begin
Process {
Foreach ($name in $computername)
{
#set some default values
$dns = $False
$dnsHostName = $Null
$IP = $Null
$ReverseVerify = $False
$CShare = $False
$AdminShare = $False
$WMIVerify = $False
$WinRMVerify = $False
$AdapterHostname = $Null
$DNSDomain = $Null
$DHCPServer = $Null
$LeaseObtained = $Null
$LeaseExpires = $Null
#test connection
$Ping = Test-Connection $name -Quiet
#resolve DNS Name
$dns=[system.net.dns]::GetHostEntry("$name")
if ($dns)
{
Write-Verbose ($dns | out-String)
$dnshostname = $dns.hostname
#filter out IPv6 addresses
$IPv4 = $dns.addresslist | where {$_.AddressFamily -eq "Internetwork"}
if (($IPv4 | Measure-Object).Count -gt 1)
{
#only take the first address
$IP=$IPv4[0].IPAddressToString
}
else
{
$IP=$IPv4.IPAddressToString
}
} #close if $DNS
else
{
Write-Verbose "No DNS record found for $name"
}
if ($IP)
{
#verify reverse lookup
Write-Verbose "Reverse lookup check"
$RevIP = "{0}.in-addr.arpa" -f (New-ReverseIP $IP)
Write-Verbose $revIP
$DNSServer = Get-DefaultDNSServer
$filter = "OwnerName = ""$RevIP"" AND RecordData=""$DNSHostName."""
Write-Verbose "Querying $DNSServer for $filter"
$record = Get-WmiObject -Class "MicrosoftDNS_PTRType" -Namespace "Root\MicrosoftDNS" -ComputerName $DNSServer -filter $filter
if ($record)
{
Write-Verbose ($record | Out-String)
if ($record.RecordData -match $dnsHostName)
{
$ReverseVerify=$True
}
}
#Get Network adapter configuration for primary IP
Write-Verbose "Getting WMI NetworkAdapterConfiguration for address $IP"
$configs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -filter "IPEnabled=True" -computername $name
#get the adapter with the matching primary IP
$adapter = $configs | where {$_.IPAddress -contains $IP}
Write-Verbose ($adapter | Out-String)
if ($adapter)
{
$AdapterHostname = $adapter.DNSHostname
$DNSDomain = $adapter.DNSDomain
$DHCPServer = $adapter.DHCPServer
$LeaseObtained = $adapter.ConvertToDateTime($adapter.dhcpleaseobtained)
$LeaseExpires = $adapter.ConvertToDateTime($adapter.dhcpleaseExpires)
}
} #close if $IP
#Verify admin shares
if (Test-Path -Path \\$name\c$) {$CShare=$True}
if (Test-Path -Path \\$name\admin$) {$AdminShare=$True}
#Verify WMI service
Write-Verbose "Validating WinMgmt Service on $name"
$wmisvc = Get-Service -Name Winmgmt -ComputerName $name
if ($wmisvc.status -eq "Running") {$WMIVerify=$True}
#validate WinRM
Write-Verbose "Validating WinRM Service on $name"
$WinRMSvc = Get-Service -Name WinRM -computername $name
if ($WinRMSvc.status -eq "Running") {$WinRMVerify=$True}
#Get MAC Address for the computer. There are several ways to get this.
#I'm taking the easy way.
Write-Verbose "Retrieving MAC address from $name"
#MAC Address regular expression pattern
[regex]$MACPattern = "([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])"
#run NBTSTAT.EXE and save results
$nbt = nbtstat -a $name
#parse out the MAC Address
$MACAddress = ($MACPattern.match($nbt)).Value
Write-Verbose "Creating object"
#Write a custom object to the pipeline
New-Object -TypeName PSObject -Property @{
Computername = $name
AdapterHostname = $adapterHostName
DNSHostName = $dnsHostname
DNSDomain = $DNSDomain
IPAddress = $IP
ReverseLookup = $ReverseVerify
DHCPServer = $DHCPServer
LeaseObtained = $LeaseObtained
LeaseExpires = $LeaseExpires
MACAddress = $MACAddress
AdminShare = $AdminShare
CShare = $CShare
WMI = $WMIVerify
Ping = $Ping
WinRM = $WinRMVerify
} #close Property hash table
} #close Foreach
} #close Process
End
{
$ErrorActionPreference = "Continue"
Write-Verbose "Finished."
} #close End
#end of script