forked from proxb/PowerShell_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-NetworkInfo.ps1
220 lines (193 loc) · 18.9 KB
/
Get-NetworkInfo.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
Function Get-NetworkInfo {
<#
.SYNOPSIS
Retrieves the network configuration from a local or remote client.
.DESCRIPTION
Retrieves the network configuration from a local or remote client.
.PARAMETER Computername
A single or collection of systems to perform the query against
.PARAMETER Credential
Alternate credentials to use for query of network information
.PARAMETER Throttle
Number of asynchonous jobs that will run at a time
.NOTES
Name: Get-NetworkInfo.ps1
Author: Boe Prox
Version: 1.0
.EXAMPLE
Get-NetworkInfo -Computername 'System1'
NICDescription : Ethernet Network Adapter
MACAddress : 00:11:22:33:aa:bb
NICName : enthad
Computername : System1.domain.com
DHCPEnabled : True
WINSPrimary : 192.0.0.25
SubnetMask : {255.255.255.255}
WINSSecondary : 192.0.0.26
DNSServer : {192.0.0.31, 192.0.0.30}
IPAddress : {192.0.0.5}
DefaultGateway : {192.0.0.1}
Description
-----------
Retrieves the network information from 'System1'
.EXAMPLE
$Servers = Get-Content Servers.txt
$Servers | Get-NetworkInfo -Throttle 10
Description
-----------
Retrieves all of network information from the remote servers while running 10 runspace jobs at a time.
.EXAMPLE
(Get-Content Servers.txt) | Get-NetworkInfo -Credential domain\adminuser -Throttle 10
Description
-----------
Gathers all of the network information from the systems in the text file. Also uses alternate administrator credentials provided.
#>
#Requires -Version 2.0
[cmdletbinding()]
Param (
[parameter(ValueFromPipeline = $True,ValueFromPipeLineByPropertyName = $True)]
[Alias('CN','__Server','IPAddress','Server')]
[string[]]$Computername = $Env:Computername,
[parameter()]
[Alias('RunAs')]
[System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty,
[parameter()]
[int]$Throttle = 15
)
Begin {
#Function that will be used to process runspace jobs
Function Get-RunspaceData {
[cmdletbinding()]
param(
[switch]$Wait
)
Do {
$more = $false
Foreach($runspace in $runspaces) {
If ($runspace.Runspace.isCompleted) {
$runspace.powershell.EndInvoke($runspace.Runspace)
$runspace.powershell.dispose()
$runspace.Runspace = $null
$runspace.powershell = $null
$Script:i++
} ElseIf ($runspace.Runspace -ne $null) {
$more = $true
}
}
If ($more -AND $PSBoundParameters['Wait']) {
Start-Sleep -Milliseconds 100
}
#Clean out unused runspace jobs
$temphash = $runspaces.clone()
$temphash | Where {
$_.runspace -eq $Null
} | ForEach {
Write-Verbose ("Removing {0}" -f $_.computer)
$Runspaces.remove($_)
}
} while ($more -AND $PSBoundParameters['Wait'])
}
Write-Verbose ("Performing inital Administrator check")
$usercontext = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
$IsAdmin = $usercontext.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
#Main collection to hold all data returned from runspace jobs
$Script:report = @()
Write-Verbose ("Building hash table for WMI parameters")
$WMIhash = @{
Class = "Win32_NetworkAdapterConfiguration"
Filter = "IPEnabled='$True'"
ErrorAction = "Stop"
}
#Supplied Alternate Credentials?
If ($PSBoundParameters['Credential']) {
$wmihash.credential = $Credential
}
#Define hash table for Get-RunspaceData function
$runspacehash = @{}
#Define Scriptblock for runspaces
$scriptblock = {
Param (
$Computer,
$wmihash
)
Write-Verbose ("{0}: Checking network connection" -f $Computer)
If (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
#Check if running against local system and perform necessary actions
Write-Verbose ("Checking for local system")
If ($Computer -eq $Env:Computername) {
$wmihash.remove('Credential')
} Else {
$wmihash.Computername = $Computer
}
Try {
Get-WmiObject @WMIhash | ForEach {
$IpHash = @{
Computername = $_.DNSHostName
DNSDomain = $_.DNSDomain
IPAddress = $_.IpAddress
SubnetMask = $_.IPSubnet
DefaultGateway = $_.DefaultIPGateway
DNSServer = $_.DNSServerSearchOrder
DHCPEnabled = $_.DHCPEnabled
MACAddress = $_.MACAddress
WINSPrimary = $_.WINSPrimaryServer
WINSSecondary = $_.WINSSecondaryServer
NICName = $_.ServiceName
NICDescription = $_.Description
}
$IpStack = New-Object PSObject -Property $IpHash
#Add a unique object typename
$IpStack.PSTypeNames.Insert(0,"IPStack.Information")
$IpStack
}
} Catch {
Write-Warning ("{0}: {1}" -f $Computer,$_.Exception.Message)
Break
}
} Else {
Write-Warning ("{0}: Unavailable!" -f $Computer)
Break
}
}
Write-Verbose ("Creating runspace pool and session states")
$sessionstate = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$runspacepool = [runspacefactory]::CreateRunspacePool(1, $Throttle, $sessionstate, $Host)
$runspacepool.Open()
Write-Verbose ("Creating empty collection to hold runspace jobs")
$Script:runspaces = New-Object System.Collections.ArrayList
}
Process {
$totalcount = $computername.count
Write-Verbose ("Validating that current user is Administrator or supplied alternate credentials")
If (-Not ($Computername.count -eq 1 -AND $Computername[0] -eq $Env:Computername)) {
#Now check that user is either an Administrator or supplied Alternate Credentials
If (-Not ($IsAdmin -OR $PSBoundParameters['Credential'])) {
Write-Warning ("You must be an Administrator to perform this action against remote systems!")
Break
}
}
ForEach ($Computer in $Computername) {
#Create the powershell instance and supply the scriptblock with the other parameters
$powershell = [powershell]::Create().AddScript($ScriptBlock).AddArgument($computer).AddArgument($wmihash)
#Add the runspace into the powershell instance
$powershell.RunspacePool = $runspacepool
#Create a temporary collection for each runspace
$temp = "" | Select-Object PowerShell,Runspace,Computer
$Temp.Computer = $Computer
$temp.PowerShell = $powershell
#Save the handle output when calling BeginInvoke() that will be used later to end the runspace
$temp.Runspace = $powershell.BeginInvoke()
Write-Verbose ("Adding {0} collection" -f $temp.Computer)
$runspaces.Add($temp) | Out-Null
Write-Verbose ("Checking status of runspace jobs")
Get-RunspaceData @runspacehash
}
}
End {
Write-Verbose ("Finish processing the remaining runspace jobs: {0}" -f (@(($runspaces | Where {$_.Runspace -ne $Null}).Count)))
$runspacehash.Wait = $true
Get-RunspaceData @runspacehash
Write-Verbose ("Closing the runspace pool")
$runspacepool.close()
}
}