-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUse-MenuNet.ps1
513 lines (494 loc) · 20.7 KB
/
Use-MenuNet.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<#
.NAME
Use-MenuNet.ps1
.SYNOPSIS
Provide menu system for running Powershell scripts
.DESCRIPTION
Menu design initially adopted from : https://quickclix.wordpress.com/2012/08/14/making-powershell-menus/
2021-12-15
Written by Jason Crockett - Sho-Me Electric Power Cooperative
use Ctrl+J to bring up "snippets" that can be inserted into code
Tips from MVA
- Scripting ToolMaking
- www.powershell.org
- #PowerShell on Twitter & @JSnover
.PARAMETERS
This parameter doesn't exist (Comps = list of computernames)
.EXAMPLE
"a command with a parameter would look like: (.\Get-DiskInfo.ps1 -comps dc)"
.\Use-MenuNet.ps1 []
.SYNTAX
.\Use-MenuNet.ps1 []
.REMARKS
To see the examples, type: help Use-MenuNet.ps1 -examples
To see more information, type: help Use-MenuNet.ps1 -detailed
.TODO
Fix the network PING connection persistence function so that it exits gracefully
Configure the function Get-hostname to gather the results into an object
#>
# set script variables
# [BOOLEAN]$Global:ExitSession=$false
# Get Credentials for later wmi requests
# $cred = Get-Credential "$env:USERDOMAIN\Administrator"
# $CDate = Get-Date -UFormat "%Y%m%d"
$Script:Nt4 = "`t`t`t`t"
$Script:Nd4 = "------ Use-Net Menu ($Global:PCCnt Systems currently selected)-----"
$Script:NNC = $null
$Script:Np1 = $null
$Script:Np2 = $null
$Script:Np3 = $null
[int]$NetMenuSelect = 0
Function chNcolor($Script:Np1,$Script:Np2,$Script:Np3,$Script:NNC){
Write-host $Script:Nt4 -NoNewline
Write-host "$Script:Np1 " -NoNewline
Write-host $Script:Np2 -ForegroundColor $Script:NNC -NoNewline
Write-host "$Script:Np3" -ForegroundColor White
$Script:NNC = $null
$Script:Np1 = $null
$Script:Np2 = $null
$Script:Np3 = $null
}
Function NetMenu()
{
Clear-Host
while ($NetMenuSelect -lt 1 -or $NetMenuSelect -gt 12)
{
Trap {"Error: $_"; Break;}
$MNNum = 0;Clear-host |out-null
Start-Sleep -m 50
if ($Global:PCCnt -eq $null) {$Global:PCCnt = 0}Else {$Global:PCCnt = ($Global:PCList).Count}
$Script:Nd4 = "------ Use-Net Menu ($Global:PCCnt Systems currently selected)-----"
Write-host $Script:Nt4 $Script:Nd4 -ForegroundColor Green
# Exit
$MNNum ++;$NExit=$MNNum;
$Script:Np1 = " $MNNum. `t";
$Script:Np2 = "Exit to Main Menu";
$Script:Np3 = " ";
$Script:NNC = "Red";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# $Get_PCList
$MNNum ++;$Get_PCList=$MNNum;
$Script:Np1 =" $MNNum. `tSelect";
$Script:Np2 = "systems to use ";
$Script:Np3 = "with commands on menu ($Global:PCCnt Systems currently selected)";
$Script:NNC = "Cyan";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# Test PC(s) for network connectivity
$MNNum ++;$test_Connections=$MNNum;
$Script:Np1 =" $MNNum. `tRun";
$Script:Np2 = "Test-Connection ";
$Script:Np3 = "on select PCs or all PCs in Domain";
$Script:NNC = "yellow";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# Test Ports
$MNNum ++;$test_ports=$MNNum;
$Script:Np1 =" $MNNum. `tTest";
$Script:Np2 = "ports ";
$Script:Np3 = "on a system or list of systems";
$Script:NNC = "yellow";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# Status of NIC on PC
$MNNum ++;$Call_Get_NICStatus=$MNNum;
$Script:Np1 =" $MNNum. `tGet ";
$Script:Np2 = "NIC ";
$Script:Np3 = "Status and information";
$Script:NNC = "yellow";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# View IPConfig-like information on select systems
$MNNum ++;$Get_IPConfig=$MNNum;
$Script:Np1 = " $MNNum. `tView ";
$Script:Np2 = "IPConfig";
$Script:Np3 = " information for select systems"
$Script:NNC = "Yellow";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# HostName from IP
$MNNum ++;$Get_hostname=$MNNum;
$Script:Np1 =" $MNNum. `tFind";
$Script:Np2 = "hostname from IP ";
$Script:Np3 = "address(es)`n$Script:Nt4$Script:Nt4---";
$Script:NNC = "yellow";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# NetStat
$MNNum ++;$View_NetStat=$MNNum;
$Script:Np1 =" $MNNum. `tView";
$Script:Np2 = "NetStat results ";
$Script:Np3 = "for a (single) system on the network (Requires elevation)";
$Script:NNC = "yellow";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# Ping
$MNNum ++;$Test_PCUp=$MNNum;
$Script:Np1 =" $MNNum. `tTest";
$Script:Np2 = "(Ping) Network ";
$Script:Np3 = "Connection Persistence (unable to exit gracefully)";
$Script:NNC = "yellow";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# IP from MAC
$MNNum ++;$Get_IP=$MNNum;
$Script:Np1 =" $MNNum.`tIdentify ";
$Script:Np2 = "MAC 2 IP ";
$Script:Np3 = "- Use a known MAC address to find an IP address";
$Script:NNC = "yellow";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# Validate IP
$MNNum ++;$Test_ValidIPPrompt=$MNNum;
$Script:Np1 =" $MNNum. `tIdentify";
$Script:Np2 = "valid IP ";
$Script:Np3 = "address from input";
$Script:NNC = "yellow";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# Wait for live port
$MNNum ++;$Wait_Live=$MNNum;
$Script:Np1 = " $MNNum. `tWait for ";
$Script:Np2 = "System port number";
$Script:Np3 = " to come alive";
$Script:NNC = "Yellow";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
# Setting up the menu in this way allows you to move menu items around and have the numbering change automatically
# It also allows you to put a word in the middle of the line and have it change color to be easier to see
# $MNNum ++;$Script:Np1 =" $MNNum. FIRST";$Script:Np2 = "HIGHLIGHTED ";$Script:Np3 = "END"; $Script:NNC = "yellow";chNcolor $Script:Np1 $Script:Np2 $Script:Np3 $Script:NNC
Write-Host "$Script:Nt4$Script:Nd4" -ForegroundColor Green
$Script:MNNum = 0;[int]$NetMenuSelect = Read-Host "`n`tPlease select the menu item you would like to do now"
}
switch($NetMenuSelect)
{
# Variables at the beginning of these lines in the switch are numbers assigned by their position in the above
# configured menu layout. This allows them to be moved around and let the numbers automatically adjust
$NExit{$NetMenuSelect=$null}
$Get_PCList{Clear-Host;Get-PCList;Reload-NetMenu}
$test_Connections{Clear-Host;test-connections;Reload-PromptNetMenu}
$test_ports{Clear-Host;test-ports;Reload-PromptNetMenu}
$Call_Get_NICStatus{Clear-Host;Call-Get-NICStatus;Reload-PromptNetMenu}
$Get_IPConfig{Clear-Host;Get-IPConfig;Reload-PromptNetMenu}
$Get_hostname{Clear-Host;Get-hostname;Reload-PromptNetMenu}
$View_NetStat{Invoke-Expression .\Get-NetStat.ps1;Reload-PromptNetMenu} # View NetStat results for a system including the process name and active ports
$Test_PCUp{Clear-Host;Test-PCUp;Reload-PromptNetMenu}
$Get_IP{Clear-Host;Get-IP;Reload-PromptNetMenu}
$Test_ValidIPPrompt{clear-host|Test-ValidIPPrompt;Reload-PromptNetMenu}
$Wait_Live{Clear-Host;Wait-Live;Reload-PromptNetMenu}
default
{
$NetMenuSelect = $null
Reload-PromptNetMenu
}
}
}
Function Reload-PromptNetMenu()
{
Read-Host "Hit Enter to continue"
$NetMenuSelect = $null
NetMenu
}
Function Reload-NetMenu()
{
$NetMenuSelect = $null
NetMenu
}
# Run Test-Connection on select PCs or all PCs in Domain
Function test-connections()
{
$PP = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
# Use the following if we want to retain a single PCList across many menu functions
if ($Global:PCList -eq $null)
{
Get-PCList
}
<# # Commented out because this is not desirable when called from another script
Else
{
Write-Warning "The Current list is: $Global:PCList"
}
#>
$Global:PCCnt = ($Global:PCList).Count
Write-Warning "Running a PING test on $Global:PCCnt Systems"
$ObjectOut =""
$ObjectOut = foreach ($Global:pcline in $Global:pclist)
{
$TCAdd=""
$TCv4Add=""
# This if statement could potentially be moved to inside the Get-PCList function, but it might need to be modified to do so
Identify-PCName
# Similar code using test-netconnection except in a script a pop-up UI stayed open making it useless
# {$TC = Test-netconnection $Global:PC -InformationLevel Quiet;$TC |Select ComputerName,RemoteAddress,PingSucceeded}
$TC = Test-connection $Global:PC -Count 1 -ErrorAction SilentlyContinue -ErrorVariable TCError
if ($TC.StatusCode -eq 0)
{
Write-host "." -NoNewline -ForegroundColor Green
$OBProperties = @{'pscomputername'=$Global:PC;
# 'Address'=$TC.Address;
'IPV4Address'=$TC.IPV4Address;
'StatusCode'=$TC.StatusCode;
'ResponseTime'=$TC.ResponseTime;
'Result'="$Global:PC : Success"}
New-Object -TypeName PSObject -Prop $OBProperties
}
Else
{
Write-host "." -NoNewline -ForegroundColor Red
$TCAdd = $TC.Address
$TCv4Add = $TC.IPV4Address
$TCSC = $TC.StatusCode
foreach ($TCErrorLine in $TCError)
{
$OBProperties = @{'pscomputername'=$Global:PC;
# 'Address'=$TCAdd;
'IPV4Address'=$TC.IPV4Address;
'StatusCode'=$TC.StatusCode;
'ResponseTime'=$TC.ResponseTime;
'Result'=$TCErrorLine}
New-Object -TypeName PSObject -Prop $OBProperties
}
}
}
$ObjectOut|Sort -Descending ResponseTime,Result |FT PSComputerName,IPV4Address,ResponseTime,StatusCode,Result
# Set Powershell's default $ProgressPreference back to what it started out as using $PP
$ProgressPreference = $PP
}
# Test ports on a system or list of systems
Function test-ports()
{
# Use the following if we want to retain a single PCList across many menu functions
if ($Global:PCList -eq $null)
{
Get-PCList
}
Else
{
Write-Warning "The Current list is: $Global:PCList"
}
$IETPRDetails = @()
$port = Read-Host "`nEnter a port, CSV port list, or list..range "
if (($protocol = Read-Host "`nEnter UDP or hit [Enter] to use current default: TCP ") -eq "") {$protocol = "TCP"}
foreach ($Global:pcLine in $Global:PCList)
{
Identify-PCName
If (Test-Connection $Global:pc -Count 1 -Quiet)
{
Write-Host ". " -nonewline
$IETPResult = Invoke-Expression ".\Test-Ports.ps1 -computer $Global:pc -port @($port) -$protocol" # |FT
$IETPRDetails += foreach ($IETPDetail in ($IETPResult |where {$_.port -ne $null}))
{
$IETPOBProperties = @{'pscomputername'=$Global:PC;
'Port'=($IETPDetail.Port);
'TypePort'=($IETPDetail.TypePort);
'Open'=($IETPDetail.Open);
'Notes'=($IETPDetail.Notes)}
New-Object -TypeName PSObject -Prop $IETPOBProperties
}
}
}
$IETPRDetails|sort pscomputername,Open,Port |ft pscomputername,Port,Open,TypePort,Notes -Wrap -AutoSize
}
# This function basically pings a system to see if it is alive. It logs the data and time and whether it is accessible or not
Function Test-PCUp()
{
$pcUp= Read-host "`nEnter a system name to test the connection"
[bool]$hideKeyStrokes = $false #$true
try
{
Write-host "`nPress x to quit" -foregroundcolor yellow
while(($true) -or ($ky -ne "X"))
{
"$($ky = $key.key;Write-Output "$ky key As of ")$(Get-Date)$(Write-Output ", $pcUp ")$(if($(Test-connection -ComputerName $pcUp <#-Quiet#>)){Write-output "is accessible"}else{Write-Output "is inaccessible"})" |ft
$key = [System.Console]::Readkey($true)
Console.Read
"ky is $ky"
}
}
finally
{
Write-Host "ky is $ky"
}
}
# Use a known MAC address to find an IP address
Function Get-IP()
{
Write-Host "`nYou may need to ping or communicate with a host prior to running this.`nTry pinging the broadcast address first."
$macaddr = Read-Host "`nEnter some or all of a MAC address in the format AC-86-...."
arp -a | select-string $macaddr #|% {$_.ToString().Trim().Split(" ")[0]}
}
# Find hostname from IP address
# Prompt for IP Address from User. Combine calls Test-ValidIP to test input for valid IP Address
Function Get-hostname()
{
# Use the following if we want to retain a single PCList across many menu functions
if ($Global:PCList -eq $null)
{
Get-PCList
}
Else
{
Write-Warning "The Current list is: $Global:PCList"
}
$VIPObjectOut = foreach($Global:pcline in $Global:pclist)
{
Identify-PCName
Try # if (Test-ValidIP $Global:pc)
{
Test-ValidIP $Global:pc
# } # commented from if
Try {
$GHAddress = [System.Net.DNS]::GetHostByAddress($Global:PC)
$VIPOBProperties = @{'HostName'=$GHAddress.HostName;
'AddressList'=$GHAddress.AddressList.IPaddressToString;
# 'Aliases'=$GHAddress|select -ExpandProperty Aliases;
'Query'="Get host $Global:PC by address"}
}
Catch {
$VIPOBProperties = @{'HostName'=""; # Revised due to overflow from previous response 'HostName'=$Global:PC;
'AddressList'=""; # Revised due to overflow from previous response $GHAddress.AddressList;
# 'Aliases'=$GHAddress.Aliases; # Revised due to overflow from previous response $GHAddress.Aliases;
'Query'="Get host $Global:PC by address"}
}
New-Object -TypeName PSObject -Prop $VIPOBProperties
}
# Else
Catch
{
$VIPOBProperties = @{'HostName'=$Global:PC;
'AddressList'=$GHAddress.AddressList.IPaddressToString;
# 'Aliases'=$GHAddress.Aliases; # Revised due to overflow from previous response $GHAddress.Aliases;
'Query'="$Global:PC is not a Valid IP Address"}
New-Object -TypeName PSObject -Prop $VIPOBProperties
}
}
$VIPObjectOut | select * |ft Hostname, AddressList, Query, Aliases
}
# Identify valid IP address from input
Function Test-ValidIPPrompt # Depends on Test-ValidIP
{
<#
# Use the following if we want to retain a single PCList across many menu functions
if ($Global:PCList -eq $null)
{
Get-PCList
}
#>
try {
$Global:PC = Read-host "`nType in a valid IP address to find information about"
Test-ValidIP $Global:PC -erroraction "stop"
}
catch
{
$Another = Read-Host "`nInvalid IP Address. Type Y to enter another."
$Global:PC = "Test-ValidIPPrompt_Catch"
while($Another -eq "Y")
{
Test-ValidIPPrompt
}
}
}
Function Test-ValidIP # Called from Test-ValidIPPrompt, Tests input for valid IP Address
{
# Manually this is called from Test-ValidIPPrompt
# This function illustrates parameter validation and using RegEx to validate IPv4 IP addresses including broadcast address
param ([ValidatePattern('^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')]
[string]$Global:PC # should this be $Global:PC?
)
Write-Output "$Global:PC is a valid IP address. One moment please." # Should this be $Global:PC ?
}
# Wait for System port number to come alive
Function Wait-Live()
{
$WaitPC = Read-host "Enter the name of a pc to wait for"
$port = Read-host "Enter the port number (like 3389)"
"`n"
(Invoke-Expression ".\Test-Ports.ps1 -computer $WaitPC -port @($port)")
while ((Invoke-Expression ".\Test-Ports.ps1 -computer $WaitPC -port @($port)").Open -eq "False")
{
Write-host "." -NoNewline
start-sleep 5
}
"`n"
(Invoke-Expression ".\Test-Ports.ps1 -computer $WaitPC -port @($port)")
}
#Get Network information from pc
Function Get-NICStatus() # Called from Call-Get-NICStatus
{
Get-WmiObject -Class win32_networkadapterconfiguration -ComputerName $Global:pc -Credential $cred|where {$_.IPAddress -ne $null}|select PSComputerName,MACAddress,IPSubnet,DNSServerSearchOrder, @{n="IP Address";e={($_ | select -ExpandProperty IPAddress)-join ','}},DHCPEnabled # |ft #out-grid-view
}
# Get NIC Status and information
Function Call-Get-NICStatus() # Depends on Get-NICStatus
{
# Use the following if we want to retain a single PCList across many menu functions
if ($Global:PCList -eq $null)
{
Get-PCList
}
Else
{
Write-Warning "The Current list is: $Global:PCList"
}
Write-host "Checking for NIC status"
$PCNICStatus = foreach($Global:pcLine in $Global:pclist)
{
Identify-PCName
If (Test-Connection $Global:pc -Count 1 -Quiet)
{
Get-NICStatus
}
}
$PCNICStatus |ft
}
# View IPConfig information for select systems
Function Get-IPConfig()
{
<#
.Synopsis
Runs 'Get-WmiObject Win32_NetworkAdapterConfiguration' against a list of Windows systems with enabled NICs
.Description
Runs 'Get-WmiObject Win32_NetworkAdapterConfiguration' against a list of Windows systems with enabled NICs
Based on code by Techibee: https://techibee.com/powershell/powershell-get-ip-address-subnet-gateway-dns-serves-and-mac-address-details-of-remote-computer/1367
Adapted by Jason Crockett - Laclede Electric Cooperative
2019-05-02
.Parameter
$Global:pclist (one or more computernames)
.ToDo
Fix RegEx for matching IP addresses
#>
$ObCSVFile = $PSScriptRoot+"\"+$Global:CDate+"_IPConfig.csv"
# Use the following if we want to retain a single PCList across many menu functions
if ($Global:PCList -eq $null)
{
Get-PCList
}
Else
{
Write-Warning "The Current list is: $Global:PCList"
}
$OutputObj = foreach($Global:pcline in $Global:pclist) {
# This if statement could potentially be moved to inside the Get-PCList function, but it might need to be modified to do so
if ($Global:PCline.name -eq $null)
{
$Global:PC = $Global:PCline
}
else
{
$Global:PC = $Global:PCline.name
}
if(Test-Connection -ComputerName $Global:PC -Count 1 -ea 0) {
try {
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Global:PC -EA Stop | ? {$_.IPEnabled}
} catch {
Write-Warning "Error occurred while querying $Global:PC."
Continue
}
foreach ($Network in $Networks) {
$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway
$DNSServers = $Network.DNSServerSearchOrder
$WINS1 = $Network.WINSPrimaryServer
$WINS2 = $Network.WINSSecondaryServer
$WINS = @($WINS1,$WINS2)
$IsDHCPEnabled = $false
$NError = $Stop
If($network.DHCPEnabled) {
$IsDHCPEnabled = $true
}
$MACAddress = $Network.MACAddress
$OBProperties = @{'computername'=$Global:PC.ToUpper();
'IPAddress'=$IPAddress;
'SubnetMask'=$SubnetMask;
'Gateway'=($DefaultGateway -join ",");
'IsDHCPEnabled'=$IsDHCPEnabled;
'DNSServers'=($DNSServers -join ",");
'WINSServers'=($WINS -join ",");
'MACAddress'=$MACAddress;
'Error'=$NError}
New-Object -TypeName PSObject -Prop $OBProperties
}
}
}
Write-Warning "Sending results to a grid and: $ObCSVFile"
$OutputObj|Select computername, SubnetMask, DNSServers, Gateway, IPAddress, IsDHCPEnabled, MACAddress, WINSServers, Error |Out-GridView
$OutputObj|Select computername, SubnetMask, DNSServers, Gateway, IPAddress, IsDHCPEnabled, MACAddress, WINSServers, Error |Export-Csv $ObCSVFile -NoClobber -NoTypeInformation -Append
}