-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.ps1
1346 lines (1077 loc) · 50.6 KB
/
Main.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
param(
[ValidateSet("Interactive", "Monitor-RDS", "PingTool")]
[string]$scriptaction = "Interactive"
)
#region functions
#region Monitor-RDS
function Get-RDPSessions {
param(
[int]$MeasureTimeSeconds = 1
)
#region Get RDPSessions
$DisconnectedReasonCodes = @{
0 = "No additional information is available."
1 = "An application initiated the disconnection."
2 = "An application logged off the client."
3 = "The server has disconnected the client because the client has been idle for a period of time longer than the designated time-out period."
4 = "The server has disconnected the client because the client has exceeded the period designated for connection."
5 = "The client's connection was replaced by another connection."
6 = "No memory is available."
7 = "The server denied the connection."
8 = "The server denied the connection for security reasons."
9 = "The server denied the connection for security reasons."
10 = "Fresh credentials are required."
11 = "User activity has initiated the disconnect."
12 = "The user logged off, disconnecting the session."
256 = "Internal licensing error."
257 = "No license server was available."
258 = "No valid software license was available."
259 = "The remote computer received a licensing message that was not valid."
260 = "The hardware ID does not match the one designated on the software license."
261 = "Client license error."
262 = "Network problems occurred during the licensing protocol."
263 = "The client ended the licensing protocol prematurely."
264 = "A licensing message was encrypted incorrectly."
265 = "The local computer's client access license could not be upgraded or renewed."
266 = "The remote computer is not licensed to accept remote connections."
267 = "An access denied error was received while creating a registry key for the license store."
768 = "Invalid credentials were encountered."
}
$events = Get-WinEvent -FilterHashtable @{LogName = 'Microsoft-Windows-TerminalServices-LocalSessionManager/Operational' }
$NetTCPSessions = Get-NetTCPConnection -State Established -LocalPort 3389
$sessions = qwinsta | ? { $_ -notmatch '^ SESSIONNAME' } | % {
[PSCustomObject]@{
Active = $_.Substring(0, 1) -match '>'
SessionName = $_.Substring(1, 18).Trim()
SessionNameRDP = $null
Username = $_.Substring(19, 20).Trim()
DateTime = get-date
Id = $_.Substring(39, 9).Trim()
State = $_.Substring(48, 8).Trim()
Type = $_.Substring(56, 12).Trim()
Device = $_.Substring(68).Trim()
Idle = $null
IdleTime = $null
LogonTime = $null
LastReconnectTime = $null
LastDisconnectedTime = $null
LastDisconnectedReason = $null
LastDisconnectedReasonID = $null
SessionEvents = $null
RemoteIP = $null
RemotePort = $null
AvgAppResponseTime = $null
WorstAPPName = $null
WorstAPPResponseTime = $null
DroppedFramesServer = $null
DroppedFramesClient = $null
DroppedFramesNetwork = $null
CurrentTCPRTT = $null
BottleNeck = $null
}
}
#Merge data from qwinsta and quser
foreach ($session in ($sessions | ? username)) {
$user = quser | ? { $_ -match $session.Username }
if ($session.SessionName -match "rdp-tcp#") {
$session.SessionNameRDP = $session.SessionName -replace "rdp-tcp#", ""
}
#TODO: nogengange fejler den her. den kører if statement selvom user er tom. wrap i try.
if ($user) {
try {
$idletime = $user.Substring(54, 9).Trim()
$session.IdleTime = $idletime
$session.Idle = $idletime -eq "\."
$session.LogonTime = get-date ($user.Substring(65, 16).Trim())
}
catch {
}
}
$SessionEvents = $events | ? TimeCreated -gt $session.LogonTime | ? { $_.message -match "Session ID: $($session.Id)" -or $_.message -match "Session $($session.Id) " } | sort timecreated -Descending
$session.SessionEvents = $SessionEvents
#Get Client IP
$LastClientIPEvent = $SessionEvents | ? message -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" | select -first 1
$null = $LastClientIPEvent.message -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
$session.RemoteIP = $matches[0]
#Get Client Port
$session.RemotePort = $NetTCPSessions | ? RemoteAddress -eq $session.RemoteIP | select -first 1 | select -ExpandProperty RemotePort
# Fill in the values from eventlog
$lastReconnectEvent = $SessionEvents | ? id -eq 25 | select -first 1
if ($lastReconnectEvent) {
$session.LastReconnectTime = $lastReconnectEvent.TimeCreated
}
#Session 5 has been disconnected, reason code 12
$lastDisconnectEvent = $SessionEvents | ? id -eq 40 | select -first 1
if ($lastDisconnectEvent) {
$session.LastDisconnectedTime = $lastDisconnectEvent.TimeCreated
$null = $lastDisconnectEvent.Message -match "reason code (\d+)"
try {
$session.LastDisconnectedReasonID = [int]$matches[1]
$session.LastDisconnectedReason = $DisconnectedReasonCodes[$session.LastDisconnectedReasonID]
}
catch {
$session.LastDisconnectedReason = "Unknown"
}
}
}
#endregion
#region Performance Measurement
# Detect system language
$Language = (Get-Culture).Name
# Define counters for English
$Counters_EN = @(
"\RemoteFX Graphics(*)\Frames Skipped/Second - Insufficient Server Resources"
"\RemoteFX Graphics(*)\Frames Skipped/Second - Insufficient Network Resources"
"\RemoteFX Graphics(*)\Frames Skipped/Second - Insufficient Client Resources"
"\RemoteFX Graphics(*)\Average Encoding Time"
"\RemoteFX Graphics(*)\Output Frames/Second"
"\RemoteFX Network(*)\Current TCP RTT"
"\RemoteFX Network(*)\Current UDP RTT"
"\User Input Delay per Process(*)\Max Input Delay"
"\Processor(_Total)\% Processor Time"
)
# Define counters for Danish (you need to find the correct Danish translations)
$Counters_DA = @(
"\RemoteFX-grafik(*)\Rammer sprunget over/sekund - utilstrækkelige serverressourcer"
"\RemoteFX-grafik(*)\Rammer sprunget over/sekund - utilstrækkelige netværksressourcer"
"\RemoteFX-grafik(*)\Rammer sprunget over/sekund - utilstrækkelige klientressourcer"
"\RemoteFX-grafik(*)\Gennemsnitlig kodningstid"
"\RemoteFX-grafik(*)\Outputrammer/sekund"
"\RemoteFX-netværk(*)\Aktuel RTT for TCP"
"\RemoteFX-netværk(*)\Aktuel RTT for UDP"
"\Forsinkelse af brugerinput pr. proces(*)\Maks. inputforsinkelse"
"\Processoroplysninger(*)\% processortid"
)
# Define regex patterns for language specific paths
$Patterns_EN = @{
CPUUtil = "% Processor Time"
UserInputDelay = "user input delay per process"
RemoteFX = "RemoteFX"
TCPRTT = "current tcp rtt"
InsufficientServerResources = "insufficient server resources"
InsufficientNetworkResources = "insufficient network resources"
InsufficientClientResources = "insufficient client resources"
}
$Patterns_DA = @{
CPUUtil = "% processortid"
UserInputDelay = "Forsinkelse af brugerinput pr. proces"
RemoteFX = "RemoteFX"
TCPRTT = "Aktuel RTT for TCP"
InsufficientServerResources = "utilstrækkelige serverressourcer"
InsufficientNetworkResources = "utilstrækkelige netværksressourcer"
InsufficientClientResources = "utilstrækkelige klientressourcer"
}
# Select the correct patterns based on the language
switch ($Language) {
"en-US" { $Patterns = $Patterns_EN }
"da-DK" { $Patterns = $Patterns_DA }
default { throw "Language not supported: $Language" } # Default to English if language is not supported
}
# Select the correct counters based on the language
switch ($Language) {
"en-US" { $Counters = $Counters_EN }
"da-DK" { $Counters = $Counters_DA }
default { throw "Language not supported: $Language" } # Default to English if language is not supported
}
try {
$PerformanceData = Get-Counter -ErrorAction Stop -Counter $Counters -MaxSamples $MeasureTimeSeconds -SampleInterval 1
}
catch {
continue
}
#threshold in ms for application response time to be considered slow
$SlowApplicationResponsTime = 500
#threshold in ms for general response time to be considered slow. This is used to determine if the server is overloaded
$GeneralSlowResponsTime = 150
[int]$ServerCPUUtil = $PerformanceData.CounterSamples | ? path -match $Patterns.CPUUtil | sort cookedvalue -Descending | select -first 1 | select -ExpandProperty CookedValue
#$session = $sessions | ? username -eq "laajadmin"
foreach ($session in $sessions | ? state -eq "active" ) {
#region App Responsetime
$SlowApplicationCount = $PerformanceData.CounterSamples | ? path -match $Patterns.UserInputDelay | ? cookedvalue -gt $GeneralSlowResponsTime | measure | select -ExpandProperty count
$AvgAppResponseTime = $PerformanceData.CounterSamples | ? path -match $Patterns.UserInputDelay | ? instanceName -match "^$($session.Id)" | ? cookedvalue -gt 0 | measure -Average -Property cookedvalue | select -ExpandProperty Average
$session.AvgAppResponseTime = $AvgAppResponseTime
$WorstAPP = $PerformanceData.CounterSamples | ? path -match $Patterns.UserInputDelay | ? instanceName -match "^$($session.Id)" | sort cookedvalue -Descending | select -first 1
$null = $WorstAPP.instancename -match "<(.+?)>"
$session.WorstAPPName = $matches[1]
$session.WorstAPPResponseTime = $WorstAPP.CookedValue
#endregion
#region Resources
$SessionRemoteFXCounters = $PerformanceData.CounterSamples | ? path -match $Patterns.RemoteFX | ? path -match "\(rdp-tcp $($session.SessionNameRDP)\)"
[int]$DroppedFramesServer = $SessionRemoteFXCounters | ? path -match $Patterns.InsufficientServerResources | sort CookedValue -desc | select -first 1 | select -ExpandProperty CookedValue
[int]$DroppedFramesClient = $SessionRemoteFXCounters | ? path -match $Patterns.InsufficientClientResources | sort CookedValue -desc | select -first 1 | select -ExpandProperty CookedValue
[int]$DroppedFramesNetwork = $SessionRemoteFXCounters | ? path -match $Patterns.InsufficientNetworkResources | sort CookedValue -desc | select -first 1 | select -ExpandProperty CookedValue
$CurrentTCPRTT = $SessionRemoteFXCounters | ? path -match $Patterns.TCPRTT | sort CookedValue -desc | select -first 1 | select -ExpandProperty CookedValue
$session.DroppedFramesServer = $DroppedFramesServer
$session.DroppedFramesClient = $DroppedFramesClient
$session.DroppedFramesNetwork = $DroppedFramesNetwork
$session.CurrentTCPRTT = $CurrentTCPRTT
if ($DroppedFramesServer -gt 5 ) {
$BottleNeck = "Server Resources: $($session.DroppedFramesServer) frames skipped"
}
elseif ($DroppedFramesNetwork -gt 5) {
$BottleNeck = "Network Packetdrops: $($session.DroppedFramesNetwork) frames skipped"
}
elseif ($DroppedFramesClient -gt 5) {
$BottleNeck = "Client: $($session.DroppedFramesClient) frames skipped"
}
#shows 400 ms when it cant calculate the RTT. so we exclude that
elseif ($session.CurrentTCPRTT -gt 150 -and $session.CurrentTCPRTT -ne 400) {
$BottleNeck = "Network Latency: $($session.CurrentTCPRTT) ms"
}
elseif ($session.WorstAPPResponseTime -gt $SlowApplicationResponsTime) {
$BottleNeck = "Application $($session.WorstAPPName) ( $($session.WorstAPPResponseTime) ms)"
}
elseif ($session.AvgAppResponseTime -gt $generalSlowResponsTime -and $SlowApplicationCount -gt 5) {
$BottleNeck = "Server Avg. Application response time: $($session.AvgAppResponseTime) ms. $SlowApplicationCount applications are slow. Server CPU utilization: $ServerCPUUtil% "
}
else {
$BottleNeck = "None"
}
$session.BottleNeck = $BottleNeck
#endregion
}
# User Input Delay per Session(*)\* Input Delay
#endregion
return $sessions
}
function Monitor-RDS {
$Logfilepath = Join-Path -Path $global:config.Logfolder -ChildPath "Monitor-RDS.csv"
while ($true) {
#Get-RDPSessions -MeasureTimeSeconds 5 |select * -ExcludeProperty SessionEvents| ft *
write-progress -id 0 -activity "Analyzing Performance. Let the script run in background." -status (get-date)
write-progress -id 1 -activity "Outputting to $Logfilepath"
$Bottlenecks = Get-RDPSessions -MeasureTimeSeconds 1 | ? state -eq "active"
if (ISInteractive) {
$Bottlenecks | ft -AutoSize Datetime, username, RemoteIP, BottleNeck, DroppedFramesServer, DroppedFramesClient, DroppedFramesNetwork, CurrentTCPRTT, AvgAppResponseTime, WorstAPPName, WorstAPPResponseTime
}
$Bottlenecks | ? bottleneck -ne "none" | select Datetime, username, RemoteIP, BottleNeck, DroppedFramesServer, DroppedFramesClient, DroppedFramesNetwork, CurrentTCPRTT, AvgAppResponseTime, WorstAPPName, WorstAPPResponseTime | export-csv $Logfilepath -NoTypeInformation -Append
}
}
#endregion Monitor-RDS
#region PingTool
Function New-IntervalPing {
[Alias("iping")]
Param(
[string]$ComputerName = "1.1.1.1",
[int]$Count = 10,
[int]$TimeOut = 100,
[int]$Interval = 100
)
$successCount = 0
$Ping = [System.Net.NetworkInformation.Ping]::New()
$results = 1..$Count | ForEach-Object {
$PingResult = $Ping.Send($ComputerName, $TimeOut)
if ($PingResult.Status -eq "Success") {
$successCount += 1
$PingResult.RoundtripTime
}
else {
$TimeOut + 1
}
start-sleep -Milliseconds $Interval
}
$packetLossPercent = ($Count - $successCount) / $Count * 100
$maxRoundtrip = ($results | Measure-Object -Maximum).Maximum
$percentile99 = $results | Sort-Object -Descending | Select-Object -Skip (($Count - ($Count * 0.99))) | Select-Object -First 1
$percentile95 = $results | Sort-Object -Descending | Select-Object -Skip (($Count - ($Count * 0.95))) | Select-Object -First 1
$averageRoundtrip = ($results | Measure-Object -Average).Average
$minRoundtrip = ($results | Measure-Object -Minimum).Minimum
$stddevresult = Get-StandardDeviation $results
return [pscustomobject]@{
"stddev" = $stddevresult.stddev
"stddevpercent" = $stddevresult.stddevpercent
"MaxRoundTrip" = $maxRoundtrip
"99PercentileLatency" = $percentile99
"95PercentileLatency" = $percentile95
"AverageRoundTrip" = $averageRoundtrip
"MinRoundTrip" = $minRoundtrip
"PacketLossPercent" = $packetLossPercent
"Count" = $successCount
}
}
Function New-IntervalPingJob {
[Alias("iping")]
Param(
[string[]]$ComputerNames = @("1.1.1.1", "8.8.8.8"),
[int]$Count = 60,
[int]$runtimeMinutes = 10,
[int]$TimeOut = 1000,
[int]$Interval = 1000,
[string]$LogFolder = "C:\CloudFactoryToolbox\Logs",
[switch]$RestartJobs = $False
)
if ($RestartJobs) {
Get-Job "Ping*" -ErrorAction SilentlyContinue | Stop-Job | remove-job
}
$jobs = @()
$computerNames | ForEach-Object {
$computerName = $_
$Jobname = "Ping$computerName"
Get-Job -name $Jobname -ErrorAction SilentlyContinue | ? state -NE "running" | remove-job
if (!((Get-Job -name $Jobname -ErrorAction SilentlyContinue).State -eq "running")) {
#job already running. Skip
$job = Start-Job -name $Jobname -ScriptBlock {
param($ComputerName, $Count, $TimeOut, $Interval, $LogFolder, $runtimeMinutes)
$stopwatch = New-Object System.Diagnostics.Stopwatch
$Ping = [System.Net.NetworkInformation.Ping]::New()
do {
$output = 1..$Count | ForEach-Object {
$PingResult = $Ping.Send($ComputerName, $TimeOut)
[PSCustomObject]@{
Status = $PingResult.Status
RoundtripTime = $PingResult.RoundtripTime
}
Start-Sleep -Milliseconds $Interval
}
#output to file
$timestamp = (Get-Date).ToString("HH-mm-ss")
$logPath = Join-Path -Path $LogFolder -ChildPath "Ping-$ComputerName-$timestamp.csv"
$output | Export-Csv -Path $logPath -Force -Delimiter ";"
Start-Sleep -Milliseconds $Interval
}until($stopwatch.Elapsed.TotalMinutes -ge $runtimeMinutes)
} -ArgumentList $computerName, $Count, $TimeOut, $Interval, $LogFolder, $runtimeMinutes
$jobs += $job
}
}
}
function Get-StandardDeviation {
#Begin function Get-StandardDeviation
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)]
[decimal[]]$value
)
#Simple if to see if the value matches digits, and also that there is more than one number.
if ($value -match '\d+' -and $value.Count -gt 1) {
#Variables used later
[decimal]$newNumbers = $Null
[decimal]$stdDev = $null
#Get the average and count via Measure-Object
$avgCount = $value | Measure-Object -Average | Select Average, Count
if ($avgCount.Average -eq 0) {
return [pscustomobject]@{
'stddev' = 0
'stddevpercent' = 0
'avg' = 0
}
}
#Iterate through each of the numbers and get part of the variance via some PowerShell math.
ForEach ($number in $value) {
$newNumbers += [Math]::Pow(($number - $avgCount.Average), 2)
}
#Finish the variance calculation, and get the square root to finally get the standard deviation.
$stdDev = [math]::Sqrt($($newNumbers / ($avgCount.Count - 1)))
#Create an array so we can add the object we create to it. This is incase we want to perhaps add some more math functions later.
[System.Collections.ArrayList]$formattedObjectArray = @()
#Create a hashtable collection for the properties of the object
Return [pscustomobject]@{
'stddev' = [double][Math]::Round($stdDev, 2)
'stddevpercent' = [double][Math]::Round($stdDev / $avgCount.Average * 100, 2)
'avg' = [double][Math]::Round($avgCount.Average, 2)
}
}
else {
#Display an error if there are not enough numbers
Write-Host "You did not enter enough numbers!" -ForegroundColor Red -BackgroundColor DarkBlue
}
}
function Calculate-Jitter {
param (
[float[]]$Latencies
)
# Calculate variations in interarrival times (latencies)
$Variations = @()
for ($i = 0; $i -lt $Latencies.Length - 1; $i++) {
$Variations += [math]::Abs(($Latencies[$i + 1] - $Latencies[$i]))
}
# Calculate average jitter
$Jitter = ($Variations | Measure-Object -Sum).Sum / $Variations.Length
return [math]::Round($Jitter, 2)
}
function Calculate-MOS {
param (
[float]$Latency,
[float]$Jitter,
[float]$PacketLossPercent
)
# Calculate PacketLossImpact
$PacketLossImpact = 2.5 * $PacketLossPercent
# Simplified R-factor calculation
$R = 94.2 - ($Latency + $Jitter) / 2 - $PacketLossImpact
if ($R -lt 0) { $R = 0 }
# Calculate MOS using the simplified formula
$MOS = 1 + 0.035 * $R + 7 * [math]::Pow(10, -6) * $R * ($R - 60) * (100 - $R)
return [math]::Round($MOS, 2)
}
function Analyze-PingData {
[CmdletBinding()]
param (
[string]$LogFolder = "C:\CloudFactoryToolbox\Logs",
$DatabasePath = "C:\CloudFactoryToolbox\Logs\PingDatabase.csv",
[switch]$UploadToGoogleSheet = $false,
[int]$PingInterval = 500,
[int]$PingCount = 100
)
function Update-RunningAverage {
param (
$ExistingAverage,
$NewValue,
[int]$TotalCount = 10
)
[double]$ExistingAverage = $ExistingAverage -replace ",", "."
[double]$NewValue = $NewValue -replace ",", "."
if ($TotalCount -eq 1) {
return $NewValue
}
$newAverage = ($ExistingAverage * ($TotalCount - 1) + $NewValue) / $TotalCount
if ($newAverage -gt 1000) {
Write-Warning "ExistingAverage $ExistingAverage"
Write-Warning "NewValue $NewValue"
Write-Warning "TotalCount $TotalCount"
Write-Warning "newAverage $newAverage"
}
return $newAverage
}
function Round-Number {
param ($number)
return [math]::Round($number, 2)
}
# Read existing database
$existingData = @()
if (Test-Path $DatabasePath) {
$existingData += Import-Csv -Path $DatabasePath -Delimiter ";"
}
# Get a list of all ping log files in the folder
$files = Get-ChildItem -Path $LogFolder -Filter "Ping-*.csv" | Sort-Object -Property LastWriteTime
<#
$file=$files[0]
#>
foreach ($file in $files) {
try {
Write-Verbose "Processing file: $($file.FullName)"
$pingResults = Import-Csv $file.FullName -Delimiter ";"
$SuccessfullPingResults = $pingResults | Where-Object { $_.Status -eq "Success" }
$successCount = $SuccessfullPingResults.Count
$totalCount = $pingResults.Count
$packetLossPercent = ($totalCount - $successCount) / $totalCount * 100
Write-Verbose "Total pings: $totalCount, Successful pings: $successCount, Packet Loss: $packetLossPercent%"
if ($successCount -gt 0) {
$maxRoundtrip = ($SuccessfullPingResults | Measure-Object -Maximum RoundtripTime).Maximum
$minRoundtrip = ($SuccessfullPingResults | Measure-Object -Minimum RoundtripTime).Minimum
$averageRoundtrip = ($SuccessfullPingResults | Measure-Object -Average RoundtripTime).Average
$jitter = Calculate-Jitter -Latencies ($SuccessfullPingResults | Select-Object -ExpandProperty RoundtripTime)
$mos = Calculate-MOS -Latency $averageRoundtrip -Jitter $jitter -PacketLossPercent $packetLossPercent
}
else {
$maxRoundtrip = 0
$minRoundtrip = 0
$averageRoundtrip = 0
$jitter = 0
$mos = 1 # Lowest possible MOS score
}
Write-Verbose "Max RTT: $maxRoundtrip, Min RTT: $minRoundtrip, Avg RTT: $averageRoundtrip, Jitter: $jitter, MOS: $mos"
$mosCategory = Get-MOSCategory -MOS $mos
$computerName = $file.BaseName.Split("-")[1]
$currentDateTime = $file.LastWriteTime
$Sourcecomputer = $env:COMPUTERNAME
$SourceIP = [System.Net.Dns]::GetHostAddresses($env:COMPUTERNAME) | Where-Object { $_.AddressFamily -eq 'InterNetwork' } | Select-Object -First 1 | ForEach-Object { $_.IPAddressToString }
$entry = [pscustomobject]@{
"DateTimeFirst" = $currentDateTime.ToString("yyyy-MM-dd HH:mm:ss")
"DateTime" = $currentDateTime.ToString("yyyy-MM-dd HH:mm:ss")
"SourceComputer" = $Sourcecomputer
"SourceIP" = $SourceIP
"ComputerName" = $computerName
"PacketLossPercent" = Round-Number $packetLossPercent
"Jitter" = Round-Number $jitter
"MOS" = Round-Number $mos
"MOSCategory" = $mosCategory
"MaxRoundTrip" = Round-Number $maxRoundtrip
"MinRoundTrip" = Round-Number $minRoundtrip
"AverageRoundTrip" = Round-Number $averageRoundtrip
"TotalCount" = $totalCount
"SuccessCount" = $successCount
"UniqueString" = "$Sourcecomputer-$SourceIP-$computerName"
"Reason" = ""
}
#select the newest entry for the specific unique entry
$existingEntry = $existingData | ? UniqueString -eq $entry.UniqueString | Sort-Object -Descending DateTimeFirst | select -first 1
$NewEntryIntervalSeconds = ($PingInterval * $PingCount / 1000)
#old entry exists
if ($existingEntry) {
#write-warning "Check if old entry is too old. Then we add a new entry"
$existingMOSCategory = $existingEntry.MOSCategory
$existingDateTimeFirst = [DateTime]::ParseExact($existingEntry.DateTimeFirst, "yyyy-MM-dd HH:mm:ss", $null)
$existingDateTime = [DateTime]::ParseExact($existingEntry.DateTime, "yyyy-MM-dd HH:mm:ss", $null)
Write-Verbose "Existing MOS Category: $existingMOSCategory, New MOS Category: $($entry.MOSCategory)"
$SecondsSinceLastEntry = $currentDateTime - $existingDateTime | select -ExpandProperty TotalSeconds
#Check if last entry is more the 3x interval time. Then create a new entry. This should only happen if script is paused.
if ($SecondsSinceLastEntry -gt (5 * $NewEntryIntervalSeconds)) {
#Old entry is too old. we create new Entry
#Write-Warning "SecondsSinceLastEntry $SecondsSinceLastEntry is to high. creating new entry)"
$entry.reason = "Old entry is too old. we create new Entry"
$existingData += $entry
}
else {
#write-warning "Old entry is strill fresh."
#MOS Category is the same as last measurement. Update the running averages
if ($entry.MOSCategory -eq $existingMOSCategory) {
#UpdateTimeEntry
$existingEntry.DateTime = $entry.datetime
$timeDifference = ($currentDateTime - $existingDateTimeFirst).TotalSeconds
$measurementCount = [Math]::Ceiling($timeDifference / $NewEntryIntervalSeconds)
Write-Verbose "Time difference: $timeDifference seconds, Measurement count: $measurementCount"
$tempexistingEntry = $existingEntry | ConvertTo-Json | ConvertFrom-Json
$tempentry = $entry | ConvertTo-Json | ConvertFrom-Json
if ($measurementCount -gt 1) {
Write-Verbose "Updating running averages"
$existingEntry.PacketLossPercent = Round-Number (Update-RunningAverage -ExistingAverage $existingEntry.PacketLossPercent -NewValue $entry.PacketLossPercent -TotalCount $measurementCount)
$existingEntry.Jitter = Round-Number (Update-RunningAverage -ExistingAverage $existingEntry.Jitter -NewValue $entry.Jitter -TotalCount $measurementCount)
#write-warning "Old Mos $($existingEntry.MOS)"
#Write-Warning "New Mos $($entry.MOS)"
#write-warning "Measurement count $measurementCount"
$existingEntry.MOS = Round-Number (Update-RunningAverage -ExistingAverage $existingEntry.MOS -NewValue $entry.MOS -TotalCount $measurementCount)
#Write-Warning "New Mos running avg. $($entry.MOS)"
$existingEntry.AverageRoundTrip = Round-Number (Update-RunningAverage -ExistingAverage $existingEntry.AverageRoundTrip -NewValue $entry.AverageRoundTrip -TotalCount $measurementCount)
$existingEntry.reason = "Updated old entry: $(get-date)"
Write-Verbose "Updated values:"
Write-Verbose "PacketLossPercent: $($entry.PacketLossPercent), Jitter: $($entry.Jitter), MOS: $($entry.MOS), AverageRoundTrip: $($entry.AverageRoundTrip)"
}
}
else {
Write-Warning "MOS Category changed. Creating a new entry."
# MOS Category changed, create a new entry
$entry.reason = "MOS Category changed. Creating a new entry"
$existingData += $entry
}
}
}
else {
#no old entry exists. Create new entry
$entry.reason = "no old entry exists. Create new entry"
$existingData += $entry
}
}
catch {
Write-Warning "Error processing file $($file.FullName): $($_|out-string)"
}
try {
Remove-Item -Path $file.FullName -Force
}
catch {
Write-Warning "Can't remove file $($file.FullName): $_"
}
}
if ($UploadToGoogleSheet) {
Append-GoogleSheet -Data $existingData
}
# Write updated data back to CSV
$existingData | sort DateTime, SourceComputer, SourceIP, ComputerName | Export-Csv -Path $DatabasePath -Delimiter ";" -NoTypeInformation
Copy-Item $DatabasePath -Destination "$($DatabasePath -replace ".csv","-COPY.csv")" -Force -ErrorAction SilentlyContinue
}
function Ping-IPRange {
<#
.SYNOPSIS
Sends ICMP echo request packets to a range of IPv4 addresses between two given addresses.
.DESCRIPTION
This function lets you sends ICMP echo request packets ("pings") to
a range of IPv4 addresses using an asynchronous method.
Therefore this technique is very fast but comes with a warning.
Ping sweeping a large subnet or network with many swithes may result in
a peak of broadcast traffic.
Use the -Interval parameter to adjust the time between each ping request.
For example, an interval of 60 milliseconds is suitable for wireless networks.
The RawOutput parameter switches the output to an unformated
[System.Net.NetworkInformation.PingReply[]].
.INPUTS
None
You cannot pipe input to this funcion.
.OUTPUTS
The function only returns output from successful pings.
Type: System.Net.NetworkInformation.PingReply
The RawOutput parameter switches the output to an unformated
[System.Net.NetworkInformation.PingReply[]].
.NOTES
Author : G.A.F.F. Jakobs
Created : August 30, 2014
Version : 6
.EXAMPLE
Ping-IPRange -StartAddress 192.168.1.1 -EndAddress 192.168.1.254 -Interval 0 -timeout 500
IPAddress Bytes Ttl ResponseTime
--------- ----- --- ------------
192.168.1.41 32 64 371
192.168.1.57 32 128 0
192.168.1.64 32 128 1
192.168.1.63 32 64 88
192.168.1.254 32 64 0
In this example all the ip addresses between 192.168.1.1 and 192.168.1.254 are pinged using
a 0 millisecond interval between each request.
All the addresses that reply the ping request are listed.
.LINK
http://gallery.technet.microsoft.com/Fast-asynchronous-ping-IP-d0a5cf0e
#>
[CmdletBinding(ConfirmImpact = 'Low')]
Param(
[parameter(Mandatory = $false, Position = 0)]
[System.Net.IPAddress]$StartAddress,
[parameter(Mandatory = $false, Position = 1)]
[System.Net.IPAddress]$EndAddress,
[System.Net.IPAddress[]]$IPrange,
[int]$Interval = 30,
$timeout = 100,
[Switch]$RawOutput = $false
)
#if range not specified, use the start and end address to create a range
if (!($IPrange)) {
function New-Range ($start, $end) {
[byte[]]$BySt = $start.GetAddressBytes()
[Array]::Reverse($BySt)
[byte[]]$ByEn = $end.GetAddressBytes()
[Array]::Reverse($ByEn)
$i1 = [System.BitConverter]::ToUInt32($BySt, 0)
$i2 = [System.BitConverter]::ToUInt32($ByEn, 0)
for ($x = $i1; $x -le $i2; $x++) {
$ip = ([System.Net.IPAddress]$x).GetAddressBytes()
[Array]::Reverse($ip)
[System.Net.IPAddress]::Parse($($ip -join '.'))
}
}
$IPrange = New-Range $StartAddress $EndAddress
}
$IpTotal = $IPrange.Count
Get-Event -SourceIdentifier "ID-Ping*" | Remove-Event
Get-EventSubscriber -SourceIdentifier "ID-Ping*" | Unregister-Event
$IPrange | ForEach-Object {
[string]$VarName = "Ping_" + $_.Address
New-Variable -Name $VarName -Value (New-Object System.Net.NetworkInformation.Ping)
Register-ObjectEvent -InputObject (Get-Variable $VarName -ValueOnly) -EventName PingCompleted -SourceIdentifier "ID-$VarName"
(Get-Variable $VarName -ValueOnly).SendAsync($_, $timeout, $VarName)
Remove-Variable $VarName
try {
$pending = (Get-Event -SourceIdentifier "ID-Ping*").Count
}
catch [System.InvalidOperationException] {}
$index = [array]::indexof($IPrange, $_)
try {
Write-Progress -Activity "Sending ping to" -Id 1 -status $_.IPAddressToString -PercentComplete (($index / $IpTotal) * 100)
Write-Progress -Activity "ICMP requests pending" -Id 2 -ParentId 1 -Status ($index - $pending) -PercentComplete (($index - $pending) / $IpTotal * 100)
}
catch {
}
Start-Sleep -Milliseconds $Interval
}
try {
Write-Progress -Activity "Done sending ping requests" -Id 1 -Status 'Waiting' -PercentComplete 100
}
catch {
# Handle any errors that occur during the execution of the code here
}
While ($pending -lt $IpTotal) {
Wait-Event -SourceIdentifier "ID-Ping*" | Out-Null
Start-Sleep -Milliseconds 10
$pending = (Get-Event -SourceIdentifier "ID-Ping*").Count
try {
Write-Progress -Activity "ICMP requests pending" -Id 2 -ParentId 1 -Status ($IpTotal - $pending) -PercentComplete (($IpTotal - $pending) / $IpTotal * 100)
}
catch {
# Handle any errors that occur during the execution of the code here
}
}
if ($RawOutput) {
$Reply = Get-Event -SourceIdentifier "ID-Ping*" | ForEach {
If ($_.SourceEventArgs.Reply.Status -eq "Success") {
$_.SourceEventArgs.Reply
}
Unregister-Event $_.SourceIdentifier
Remove-Event $_.SourceIdentifier
}
}
else {
$Reply = Get-Event -SourceIdentifier "ID-Ping*" | ForEach {
If ($_.SourceEventArgs.Reply.Status -eq "Success") {
$_.SourceEventArgs.Reply | select @{
Name = "IPAddress" ; Expression = { $_.Address }
},
@{Name = "Bytes" ; Expression = { $_.Buffer.Length } },
@{Name = "Ttl" ; Expression = { $_.Options.Ttl } },
@{Name = "ResponseTime"; Expression = { $_.RoundtripTime } }
}
Unregister-Event $_.SourceIdentifier
Remove-Event $_.SourceIdentifier
}
}
if ($Reply -eq $Null) {
Write-Verbose "Ping-IPrange : No ip address responded" -Verbose
}
return $Reply
}
function Get-MOSCategory {
param (
[float]$MOS
)
switch ($MOS) {
{ $_ -ge 4.3 } { return "Excellent" }
{ $_ -ge 4.0 } { return "Good" }
{ $_ -ge 3.6 } { return "Fair" }
{ $_ -ge 3.1 } { return "Poor" }
default { return "Bad" }
}
}
function Get-TracerouteIPs {
param($destination = "1.1.1.1", $Hopcount = 3)
<#test params
$destination="1.1.1.1"
$Hopcount=3
#>
$Traceroute = Test-NetConnection -ComputerName $destination -TraceRoute -Hops $Hopcount
$TracerouteIPs = $Traceroute.TraceRoute
$RespondingIPs = (Ping-IPRange -iprange $TracerouteIPs).IPAddress.IPAddressToString
return $RespondingIPs
}
function Get-DefaultGateway {
Get-WmiObject -Class Win32_IP4RouteTable |
where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0' } |
Sort-Object metric1 | select -first 1 -ExpandProperty nexthop
}
function PingTool {
param(
$pings,
$interval = 500,
$count = 100 #count needs to be high for the percent calculation to be accurate. 100+ is good.
)
#$pings += (Ping-IPRange -StartAddress 10.245.100.1 -EndAddress 10.245.100.254).IPAddress.IPAddressToString
#$pings += (Ping-IPRange -StartAddress 10.245.110.1 -EndAddress 10.245.110.254).IPAddress.IPAddressToString
#add all pingable trace route ips
$RespondingTraceRouteIPs = Get-TracerouteIPs -destination "1.1.1.1" -Hopcount 1
$pings += $RespondingTraceRouteIPs
#add default gateway in case its not pingable
$DefaultGateway = Get-DefaultGateway
$pings += $DefaultGateway
$pings = $pings | Sort-Object -Unique
$pings = (Ping-IPRange -iprange $pings).IPAddress.IPAddressToString
Write-Output "Pinging following IPs"
$pings
get-job | Stop-Job | Remove-Job
New-IntervalPingJob -ComputerNames $pings -Interval $interval -Count $count -LogFolder "C:\CloudFactoryToolbox\Logs" -RestartJobs $true
while ($true) {
#get-job
Analyze-PingData -PingInterval $interval -PingCount $count | out-null
Start-Sleep -Seconds 1
}
get-job | Stop-Job | Remove-Job
}
#endregion PingTool
#region Helper functions
function Append-GoogleSheet {
param (
$Endpoint = "https://script.google.com/macros/s/AKfycbwuNMkMVNgkTq7eo3a5fcKWI2cAMVfZ_HrHd9Gs1j89lfRHt7gcCPN5Kmzf3MGnZMKbBQ/exec",
$SheetName = "Ping",
$Data = $pingData
)
#region Google Sheet Script
#function doPost(e) {
# var requestData;
# if (e) {
# requestData = JSON.parse(e.postData ? e.postData.contents : "{}");
# } else {
# // Use the provided JSON test data as dummy data
# requestData = {
# sheetName: 'Ping', // Ensure this sheet name exists in your spreadsheet
# data: [
# { Age: 25, Name: 'Jane Doe', Email: '[email protected]', Country: 'USA' },
# { Name: 'John Doe', Age: 30, Email: '[email protected]' },
# { Name: 'Mike Smith', Age: 28 } // Note: Missing 'Email' and 'Country'
# ]
# };
# }
#
#
# var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(requestData.sheetName);
# var data = requestData.data;