-
Notifications
You must be signed in to change notification settings - Fork 0
/
4104 script
1646 lines (1450 loc) · 42.5 KB
/
4104 script
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
# Copyright © 2008, Microsoft Corporation. All rights reserved.
function GetRuntimePath([string]$fileName = $(throw "No file name is specified"))
{
if([string]::IsNullorEmpty($fileName))
{
throw "Invalid file name"
}
return $null;
}
trap [Exception]
{
return $null;
}
}
#function assumes passed in UNC is in \\host\share form (share can be missing)
function ContainsInvalidUNCChars($UNC)
{
&{
#will return an exception if the string has invalid characters
$ignoreResult = [System.IO.Path]::IsPathRooted($UNC)
#check the path for invalid characters
#remove the starting slashes
$tmp = $UNC.Substring(2)
$nextSlash = $tmp.IndexOf("\")
if(($nextSlash -lt 0) -or ($nextSlash -eq ($nextSlash.Length - 1)))
{
#string only contains hostname
#hostname is already validated in IsUNCFormat function
return $false
}
#remove host and backslash after host
$UNCPath = $tmp.Substring($nextSlash+1)
#under certain circumstances some of these make it through the above check
#so we do a direct sanity check here
if(!($UNCPath.IndexOfAny(@('/',':','*','?','"','<','>','|')) -eq -1))
{
return $true;
}
return $false;
}
trap [Exception]
{
return $true;
}
}
function GetValidUNC($CandidateUNC)
{
$toReturn = $null
#is it valid
$unc = IsUNCFormat $CandidateUNC
if($unc)
{
$invalidChars = ContainsInvalidUNCChars $unc
if($invalidChars)
{
$toReturn = -1;
}
else
{
$toReturn = $unc
}
}
return $toReturn;
}
function GetUNCNDFIncidentData($UNC)
{
#build entry point parameters
$haXML = "<HelperAttributes><HelperAttribute><Name>UNCPath</Name><Type>AT_STRING</Type><Value><![CDATA[" + $UNC + "]]></Value></HelperAttribute></HelperAttributes>"
return @{"HelperClassName" = "SMBHelperClass"; "HelperAttributes" =$haXML}
}
function FileSharingEntry()
{
$IT_UNC = Get-DiagInput -ID "IT_UNC"
if($IT_UNC -eq $null) {
#Failed retriving UNC path
return $null
}
#assign input to non-array variable to facilitate usage and transform
$validUNC = GetValidUNC $IT_UNC[0]
while((!$validUNC) -or ($validUNC -eq -1))
{
#build the RTF text
#use original entry for re-prompt even though "file://" UNC may have been transformed
$replacedError = "";
if(!$validUNC)
{
$replacedError = [System.String]::Format([System.Globalization.CultureInfo]::InvariantCulture, $localizationString.interaction_InvalidUNC_FormatError, $IT_UNC[0]);
}
else
{
$replacedError = [System.String]::Format([System.Globalization.CultureInfo]::InvariantCulture, $localizationString.interaction_InvalidUNC_CharError, $IT_UNC[0]);
}
$RTFText = GetErrorRTF ($localizationString.interaction_InvalidUNC_Desc) ($replacedError);
#reprompt for input
$IT_UNC = Get-DiagInput -ID "IT_Invalid_UNC" -p @{"UNC" = $IT_UNC; "RTFText" = $RTFText}
if($IT_UNC -eq $null) {
#Failed retriving UNC path
return $null
}
$validUNC = GetValidUNC $IT_UNC[0]
}
return GetUNCNDFIncidentData $validUNC
}
function NetworkAdapterEntry()
{
#enumerate interfaces to build options list
$interfaces = get-wmiobject -class Win32_NetworkAdapter
#hash table with options
$optionList = @()
foreach($curInterface in $interfaces)
{
if($curInterface.GUID -ne $null)
{
$curHash = @{"Name"=$curInterface.NetConnectionID}
$curHash += @{"Description"=$curInterface.NetConnectionID}
$curHash += @{"Value"=$curInterface.GUID}
$optionList += @($curHash)
}
}
if($optionList.Count -gt 1)
{
#add zero guid entry to check all interfaces
$optionList += @(@{"Name"=$localizationString.interaction_AllAdapters; "Description"=$localizationString.interaction_AllAdapters; "Value"="{00000000-0000-0000-0000-000000000000}"; "ExtensionPoint"="<Default />"})
#get interface selection from user
$IT_NetworkAdapter = Get-DiagInput -ID "IT_NetworkAdapter" -c $optionList
if($IT_NetworkAdapter -eq $null) {
throw "Failed retriving Network Connetion ID from user"
}
}
elseif($optionList.Count -eq 1)
{
$IT_NetworkAdapter = $optionList[0]["Value"]
}
else
{
#No NICs, do zero GUID diag
$IT_NetworkAdapter = "{00000000-0000-0000-0000-000000000000}"
}
#build entry point parameters
$haXML = "<HelperAttributes><HelperAttribute><Name>guid</Name><Type>AT_GUID</Type><Value>" + $IT_NetworkAdapter + "</Value></HelperAttribute></HelperAttributes>"
return @{"HelperClassName" = "NetConnection"; "HelperAttributes" =$haXML}
}
function WinsockEntry()
{
$IT_RemoteAddress = Get-DiagInput -ID "IT_RemoteAddress"
if($IT_RemoteAddress -eq $null -or $IT_RemoteAddress[0].Length -eq 0) {
#Failed retriving Remote Address
return $null
}
$IT_Protocol = Get-DiagInput -ID "IT_Protocol"
if($IT_Protocol -eq $null -or $IT_Protocol[0].Length -eq 0) {
#Failed retriving Remote Port
return $null
}
$IT_ApplicationID = Get-DiagInput -ID "IT_ApplicationID"
if($IT_ApplicationID -eq $null -or $IT_ApplicationID[0].Length -eq 0) {
#Failed retriving Application ID
return $null
}
#build entry point parameters
$haXML = "<HelperAttributes><HelperAttribute><Name>remoteaddr</Name><Type>AT_SOCKADDR</Type><Value>" + $IT_RemoteAddress + "</Value></HelperAttribute>";
$haXML += "<HelperAttribute><Name>protocol</Name><Type>AT_UINT32</Type><Value>" + $IT_Protocol + "</Value></HelperAttribute>";
$haXML += "<HelperAttribute><Name>localaddr</Name><Type>AT_SOCKADDR</Type><Value>0.0.0.0</Value></HelperAttribute>";
$haXML += "<HelperAttribute><Name>appid</Name><Type>AT_STRING</Type><Value>" + $IT_ApplicationID + "</Value></HelperAttribute>";
$haXML += "</HelperAttributes>";
return @{"HelperClassName" = "Winsock"; "HelperAttributes" =$haXML}
}
function GroupingEntry()
{
$IT_GroupName = Get-DiagInput -ID "IT_GroupName"
if($IT_GroupName -eq $null -or $IT_GroupName[0].Length -eq 0) {
#Failed retriving Remote Address
return $null
}
#build entry point parameters
$haXML = "<HelperAttributes><HelperAttribute><Name>groupname</Name><Type>AT_STRING</Type><Value>" + $IT_GroupName + "</Value></HelperAttribute></HelperAttributes>"
return @{"HelperClassName" = "GroupingHelperClass"; "HelperAttributes" =$haXML}
}
function GetValidExePath($File)
{
&{
$uri = [System.URI]($File);
$scheme = $uri.scheme;
if(($scheme -eq "file" ))
{
#make sure it send in .exe
if($File.ToLower().IndexOf(".exe") -eq ($File.Length - 4))
{
return $File;
}
}
return $null;
}
trap [Exception]
{
return $null;
}
}
function InboundEntry()
{
$staticOptionRes = @($INBOUND_FILESHARE_RESOURCE, $INBOUND_REMOTEDESKTOP_RESOURCE, $INBOUND_DISCOVERY_RESOURCE)
$staticOptions = @($INBOUND_FILESHARE_PARAM, $INBOUND_REMOTEDESKTOP_PARAM, $INBOUND_DISCOVERY_PARAM)
# If defined for the corresponding option, the item will be filtered out if the current sku matches anything in the list
# Sku values as defined in the OperatingSystemSKU property of Win32_OperatingSystem
$SKUFilters = @($null, @(2,3,5,11), $null)
#get the SKU, to filter out inappropriate static options
$SKUObject = get-wmiobject -class Win32_OperatingSystem -property "OperatingSystemSKU"
$SKU = $SKUObject.OperatingSystemSKU
$optionList = @()
$curOptionIndex = 0
for($curStaticOption = 0; $curStaticOption -lt $staticOptions.Length; $curStaticOption++)
{
$SKUFilter = $SKUFilters[$curStaticOption]
if($SKUFilter)
{
if($SKUFilter -contains $SKU)
{
#should filter out this option from the list because it is not present in the SKU
continue;
}
}
$curApp = LoadResourceString($staticOptionRes[$curStaticOption])
$curHash = @{}
$curHash.Add("Name",$curApp)
$curHash.Add("Value",$curOptionIndex)
$curHash.Add("Description",$curApp)
$curHash.Add("HelperAttributeName","serviceid")
$curHash.Add("HelperAttributeValue",$staticOptions[$curStaticOption])
$optionList += $curHash
$curOptionIndex++
}
#add dynamic options (do not fail if call fails)
$script:ExpectingException = $true
$dll = "NetworkDiagnosticSnapIn.dll"
try
{
RegSnapin $dll
$droppedApps = [Microsoft.Windows.Diagnosis.Network.FirewallApi.ManagedMethods]::GetDiagnosticAppInfo()
$script:ExpectingException = $false
if($droppedApps)
{
foreach($droppedApp in $droppedApps)
{
#omit svchosts since we cannot display a friendly name for them
if($droppedApp.Path.IndexOf("svchost") -eq -1)
{
$appEntryDisplayStr = [System.String]::Format([System.Globalization.CultureInfo]::InvariantCulture, $localizationString.interaction_Inbound_Exe, $droppedApp.FriendlyName);
$curHash = @{}
$curHash.Add("Name",$appEntryDisplayStr)
$curHash.Add("Value",$curOptionIndex)
$curHash.Add("Description",$droppedApp.FriendlyName)
$curHash.Add("HelperAttributeName","appid")
$curHash.Add("HelperAttributeValue",$droppedApp.Path)
$optionList += $curHash
$curOptionIndex++
}
}
}
}
finally
{
UnregSnapin $dll
}
#add the last option to browse for files
$curApp = LoadResourceString($INBOUND_OTHER_RESOURCE)
$curHash = @{}
$curHash.Add("Name",$curApp)
$curHash.Add("Value",$curOptionIndex)
$curHash.Add("Description",$curApp)
$curHash.Add("HelperAttributeName","serviceid")
$curHash.Add("HelperAttributeValue",$INBOUND_OTHER_RESOURCE)
$optionList += $curHash
#trap exception if it happens, and if expected don't fail
trap [Exception]
{
if($script:ExpectingException)
{
$script:ExpectingException = $false
"Exception: " + $_.Exception.GetType().FullName + " Message: " + $_.Exception.Message | convertto-xml | Update-DiagReport -id GetDiagAppInfoFailure -name "GetDiagAppInfo" -verbosity Debug
continue;
}
else
{
#rethrow exception
throw $_.Exception;
}
}
$IT_ServiceName = Get-DiagInput -ID "IT_ServiceName" -c $optionList
if($IT_ServiceName -eq $null -or $IT_ServiceName[0].Length -eq 0) {
#Failed retriving service name
return $null
}
$optionSelected = $optionList[$IT_ServiceName]
$optionSelected = $optionSelected[0] #need to to this to get access to the dictionary entry object
$HelperAttributeName = $null
$HelperAttributeValue = $null
if($optionSelected.HelperAttributeValue -eq $INBOUND_OTHER_RESOURCE)
{
#show the file browsing interaction so that the user can pick their own executable
$IT_BrowseFile = Get-DiagInput -ID "IT_BrowseFile"
if($IT_BrowseFile -eq $null) {
#Failed retriving file
return $null
}
$validExe = GetValidExePath $IT_BrowseFile[0]
while(!$validExe)
{
#build the RTF text
#build the error
$replacedError = [System.String]::Format([System.Globalization.CultureInfo]::InvariantCulture, $localizationString.interaction_InvalidExe_FormatError , $IT_BrowseFile[0]);
#only a single line
$RTFText = GetErrorRTF ($localizationString.interaction_InvalidExe_Desc) ($replacedError);
#reprompt for input
$IT_BrowseFile = Get-DiagInput -ID "IT_Invalid_BrowseFile" -p @{"File" = $IT_BrowseFile[0]; "RTFText" = $RTFText}
if($IT_BrowseFile -eq $null) {
#Failed retriving file
return $null
}
$validExe = GetValidExePath $IT_BrowseFile[0]
}
$HelperAttributeName = "appid"
$HelperAttributeValue = $IT_BrowseFile;
}
else
{
$HelperAttributeName = $optionSelected.HelperAttributeName
$HelperAttributeValue = $optionSelected.HelperAttributeValue
}
#build entry point parameters
$haXML = "<HelperAttributes>"
$haXML += "<HelperAttribute><Name>" + $HelperAttributeName + "</Name><Type>AT_STRING</Type><Value>" + $HelperAttributeValue + "</Value></HelperAttribute>"
$haXML += "<HelperAttribute><Name>localaddr</Name><Type>AT_SOCKADDR</Type><Value>0.0.0.0</Value></HelperAttribute>"
$haXML += "</HelperAttributes>"
return @{"HelperClassName" = "Winsock"; "HelperAttributes" =$haXML}
}
function DirectAccessEntry()
{
$toReturn = $null;
$path = "hklm:SOFTWARE\Policies\Microsoft\Windows\NetworkConnectivityStatusIndicator\CorporateConnectivity";
if(test-path $path)
{
$corpReg = get-itemproperty -path $path
if($corpReg.WebProbeURL -and $corpReg.WebProbeURL.Length -gt 0)
{
#build entry point parameters
$haXML = "<HelperAttributes><HelperAttribute><Name>URL</Name><Type>AT_STRING</Type><Value>" + $corpReg.WebProbeURL + "</Value></HelperAttribute></HelperAttributes>"
$toReturn = @{"HelperClassName" = "WinInetHelperClass"; "HelperAttributes" =$haXML}
}
elseif($corpReg.DnsProbeHost -and $corpReg.DnsProbeHost -gt 0)
{
#build entry point parameters
$haXML = "<HelperAttributes><HelperAttribute><Name>QueryName</Name><Type>AT_STRING</Type><Value>" + $corpReg.DnsProbeHost + "</Value></HelperAttribute></HelperAttributes>"
$toReturn = @{"HelperClassName" = "DnsHelperClass"; "HelperAttributes" =$haXML}
}
}
return $toReturn;
}
function DefaultConnectivityFollowUpEntry()
{
$toReturn = $null
$IT_DefaultConnectivityInitialChoice = Get-DiagInput -ID "IT_DefaultConnectivityInitialChoice"
if($IT_DefaultConnectivityInitialChoice -eq $null -or $IT_DefaultConnectivityInitialChoice[0].Length -eq 0)
{
#Failed retriving service name
return $null
}
#clear the progress so that the last step doesn't show before things get started again
Write-DiagProgress -activity " "
if($IT_DefaultConnectivityInitialChoice -eq "Other")
{
#query which other entry point they wish to use
$IT_DefaultConnectivityOtherChoice = Get-DiagInput -ID "IT_DefaultConnectivityOtherChoice"
if($IT_DefaultConnectivityOtherChoice[0].Length -eq 0)
{
#Failed retriving service name
return $null
}
if($IT_DefaultConnectivityOtherChoice -eq "Inbound")
{
$toReturn = InboundEntry
}
elseif($IT_DefaultConnectivityOtherChoice -eq "DirectAccess")
{
$toReturn = DirectAccessEntry
if(!$toReturn)
{
#not provisioned, root cause outside of NDF
Update-DiagRootCause -ID "{E42E5B5A-16E0-43f1-AB32-C94C608D269D}" -Detected $true
return;
}
}
elseif($IT_DefaultConnectivityOtherChoice -eq "NetworkAdapter")
{
$toReturn = NetworkAdapterEntry
}
}
else
{
#query for the URL/UNC path
$IT_URLOrUNC = Get-DiagInput -ID "IT_URLOrUNC"
$validUNC = $null
#Is it a valid URL?
$validURL = GetValidURL $IT_URLOrUNC[0]
if(!$validURL)
{
$validUNC = GetValidUNC $IT_URLOrUNC[0]
}
while((!$validURL) -and
((!$validUNC) -or ($validUNC -eq -1)))
{
#build the RTF text
$replacedError = [System.String]::Format([System.Globalization.CultureInfo]::InvariantCulture, $localizationString.interaction_InvalidURLOrUNC_FormatError, $IT_URLOrUNC[0]);
$RTFText = GetErrorRTF ($localizationString.interaction_InvalidURLOrUNC_Desc) ($replacedError);
#reprompt for input
$IT_URLOrUNC = Get-DiagInput -ID "IT_Invalid_URLOrUNC" -p @{"URLOrUNC" = $IT_URLOrUNC; "RTFText" = $RTFText}
if($IT_URLOrUNC -eq $null) {
#Failed retriving URL/UNC path
return $null
}
$validURL = GetValidURL $IT_URLOrUNC[0]
if(!$validURL)
{
$validUNC = GetValidUNC $IT_URLOrUNC[0]
}
}
if($validURL)
{
$toReturn = GetWebNDFIncidentData $validURL $false
}
else
{
$toReturn = GetUNCNDFIncidentData $validUNC
}
}
return $toReturn
}
function GetRepair($RepairRank, $RCEnum)
{
$RCEnum.Reset()
$rootCauseCount = $RCEnum.Count
for($i=0; $i -lt $rootCauseCount; $i++)
{
$rootCause = $RCEnum.Next;
$repairEnum = $rootCause.Repairs
$repairCount = $repairEnum.Count;
for($r=0; $r -lt $repairCount; $r++)
{
$curRep = $repairEnum.Next
if($curRep.Rank -eq $RepairRank)
{
return $curRep
}
}
}
return $null
}
function SplitString($FullString, [ref]$Title, [ref]$Rest)
{
$newLineIndex = $FullString.IndexOf("`n");
if(!($newLineIndex -eq -1))
{
$Title.value = $FullString.Substring(0, $newLineIndex)
$Rest.value = $FullString.Substring($newLineIndex+1)
}
else
{
$Title.value = $FullString
$Rest.value = ""
}
}
function GetTitleAndDesc($RCorRep, [ref]$Title, [ref]$Desc)
{
$descEx = $RCorRep.DescriptionEx
if(!($descEx -eq $null))
{
$Title.value = $descEx.Title
$Desc.value = $descEx.Description
if($Desc.value -eq $null)
{
$Desc.value = ""
}
}
else
{
#split the string into title and description using \n
SplitString $RCorRep.Description ($Title) ($Desc)
}
}
function GetButtonParams($Repair, [ref]$Params, $DefaultName, $DefaultDesc, $ExtensionName, $ButtonITName, $ButtonITDesc)
{
#defaults
$buttonName = $DefaultName;
$buttonDesc = $DefaultDesc;
$descriptionEx = $Repair.DescriptionEx;
if($descriptionEx)
{
#this is a XML based root cause, get replacement parameters
$paramEnum = $DescriptionEx.Extensions;
[string]$runtimePath = [System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()
return Join-Path $runtimePath $fileName
}
function RegSnapin([string]$dllName = $(throw "No dll is specified"))
{
$dllPath = ".\" + $dllName
Import-Module $dllPath
}
function UnregSnapin([string]$dllName = $(throw "No dll is specified"))
{
$moduleName = $dllName.TrimEnd(".dll")
Remove-Module $moduleName
}
function GetExistingNDFInstance($IncidentID)
{
&{
#if fails we start a new session
$script:ExpectingException = $true
$ndf = new-object -comObject ndfapi.NetworkDiagnostics.1 -strict
$ndf.OpenExistingIncident($IncidentID); #throws exception if fails
$script:ExpectingException = $false
return $ndf
}
trap [Exception]
{
if($script:ExpectingException)
{
$script:ExpectingException = $false
"Exception: " + $_.Exception.GetType().FullName + " Message: " + $_.Exception.Message | convertto-xml | Update-DiagReport -id OpenExistingSessionFailure -name "Open Existing Session" -description "Failed while opening existing NDF session." -verbosity Debug
return $null;
}
else
{
#rethrow exception
throw $_.Exception;
}
}
}
function WaitWithProgress($ActivityNoDetails, $WaitHandle, $Ndf)
{
$lastProgress = $null
do {
$progress = $Ndf.Progress
if(($progress -ne $null) -and ($progress.Length -gt 0) -and !($progress -eq $lastProgress))
{
Write-DiagProgress -activity $progress
$lastProgress = $progress
}
elseif(($progress.Length -eq 0) -and !($ActivityNoDetails -eq $lastProgress))
{
#clear the last progress string, use alternate description of operation
Write-DiagProgress -activity $ActivityNoDetails
$lastProgress = $ActivityNoDetails
}
&{
$WaitHandle.Wait($ProgressUpdateDelay)
break
}
trap [Exception]
{
#timed out, continue waiting
continue
}
} while($true)
}
# if filtered mode is enabled and the root cause is not on the filter list then return TRUE
function GetRootCauseFilterValue($RootCause, [ref]$FilterMode)
{
#assume filtering is enabled
$filteringEnabled = $true;
if($FilterMode)
{
$FilterMode.value = 0;
}
$regValue = get-itemproperty -path hklm:\SYSTEM\CurrentControlSet\Control\NetDiagFx\Config -name FilterMode -ErrorAction SilentlyContinue -ErrorVariable regError
if($regValue)
{
if($FilterMode)
{
$FilterMode.value = $regValue.FilterMode;
}
$filteringEnabled = !($regValue.FilterMode -eq $FM_DISABLED);
}
elseif($regError)
{
if(!($regError[0].CategoryInfo.Category -eq "InvalidArgument") -and !($regError[0].CategoryInfo.Category -eq "ObjectNotFound"))
{
" Warning: Unexpected error when reading FilterMode key: " + $regError[0].CategoryInfo.Category | convertto-xml | Update-DiagReport -id UnexpectedRegError -name "Unexpected Registry Error" -verbosity Debug
}
}
if(!$filteringEnabled)
{
#allow all root causes
return $FV_NOTFILTERED;
}
else
{
#get the registry value and make the filtering decision
$rootCauseID = $RootCause.RootCauseID;
$regValue = get-itemproperty -path hklm:\SYSTEM\CurrentControlSet\Control\NetDiagFx\Config\RC -name $rootCauseID -ErrorAction SilentlyContinue -ErrorVariable regError
if($regValue)
{
#root cause present, check the entry type
$filterValue = $regValue.$rootCauseID;
if($filterValue -eq 1)
{
#exclusion list, force enabled filter mode so that the RC is dropped
if($FilterMode)
{
$FilterMode.value = $FM_ENABLED;
}
return $FV_FILTERED;
}
else
{
#inclusion list, not filtered
return $FV_NOTFILTERED;
}
}
else
{
if(!($regError[0].CategoryInfo.Category -eq "InvalidArgument") -and !($regError[0].CategoryInfo.Category -eq "ObjectNotFound"))
{
" Warning: Unexpected error when reading Root Cause key : " + $regError[0].CategoryInfo.Category | convertto-xml | Update-DiagReport -id UnexpectedRegError -name "Unexpected Registry Error" -verbosity Debug
}
#could not find root cause
return $FV_MISSING;
}
}
}
function GetCatchAllInformation($RootCauseEnum, [ref]$CatchAlls, [ref]$CatchAllsIndex, [ref]$CatchAllsAltRC)
{
$localCatchAlls = @{}; #will contain the RC's with catch all repairs, indexed by HC it applies to
$localCatchAllsIndex = @{}; #will hold the index of the catch-all in the root cause list
$localCatchAllsAltRC = @{}; #will contain the alternate RC ID for the catch all, if available, indexed by root cause
$rootCauseCount = $RootCauseEnum.Count;
$RootCauseEnum.Reset()
for($i=0; $i -lt $rootCauseCount; $i++)
{
$rootCause=$RootCauseEnum.Next;
$repairEnum = $rootCause.Repairs;
$repairCount = $repairEnum.Count;
$repairEnum.Reset();
for($curRep=0; $curRep -lt $repairCount; $curRep++)
{
$repair = $repairEnum.Next;
if($repair.Flags -band $RF_RESERVED_CA)
{
$altRC = GetExtensionValue ($repair.DescriptionEx) ("CatchAllRC");
#HC's this applies to
$catchAllHCString = GetExtensionValue ($repair.DescriptionEx) ("CatchAllHCs");
if($catchAllHCString)
{
#semi-colon separated
$catchAllHCList = $catchAllHCString.Split(";");
for($curHC=0; $catchAllHCList[$curHC]; $curHC++)
{
#if a RC is filtered for this HC, this catch all will be used
$localCatchAlls[$catchAllHCList[$curHC]]= $rootCause;
if($altRC)
{
#store the alternate RC
$localCatchAllsAltRC[$rootCause] = $altRC;
}
}
#store the index
$localCatchAllsIndex[$rootCause]= $i;
}
break;
}
}
}
#return back the values collected
if($CatchAlls)
{
$CatchAlls.value = $localCatchAlls;
}
if($CatchAllsIndex)
{
$CatchAllsIndex.value = $localCatchAllsIndex;
}
if($CatchAllsAltRC)
{
$CatchAllsAltRC.value = $localCatchAllsAltRC;
}
}
function GetParameters($DescriptionEx, $Params)
{
$toReturn = @{};
#this is a XML based root cause, get replacement parameters
$paramEnum = $DescriptionEx.Parameters;
$paramCount = $paramEnum.Count;
$paramEnum.Reset()
for($curP=0;$curP -lt $paramCount; $curP++)
{
$param = $paramEnum.Next;
if($Params[$param.Name] -eq $null)
{
$toReturn += @{$param.Name = $param.Value}
}
}
return $toReturn;
}
function GetKeywords($DescriptionEx)
{
if($DescriptionEx -eq $nul)
{
return "";
}
$toReturn = "";
#this is a XML based root cause, get replacement parameters
$paramEnum = $DescriptionEx.Extensions;
$paramCount = $paramEnum.Count;
$paramEnum.Reset()
for($curP=0;$curP -lt $paramCount; $curP++)
{
$param = $paramEnum.Next;
if($param.Name -eq "Keyword")
{
if($toReturn.Length -gt 0)
{
$toReturn += "+";
}
$toReturn += """" + $param.Value + """";
}
}
return $toReturn.Replace(" ", "+");
}
function GetExtensionValue($DescriptionEx, $ExtensionName)
{
if($DescriptionEx)
{
$paramEnum = $DescriptionEx.Extensions;
$paramCount = $paramEnum.Count;
$paramEnum.Reset()
for($curP=0;$curP -lt $paramCount; $curP++)
{
$param = $paramEnum.Next;
if($param.Name -eq $ExtensionName)
{
return $param.Value;
}
}
}
return $null;
}
#are we an administrator or LUA?
function IsAdmin()
{
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [System.Security.Principal.WindowsPrincipal]($identity)
return $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
#the security boundary safe data now contains two items of information
#(1) the incident ID GUID (without the { } brackets)
#(2) flags: whether the process is LUA (0) or Admin (1)
#the two items are separated by a semicolon
function GetSBSData($IncidentID)
{
$flags = 0
$admin = IsAdmin
if($admin)
{
$flags = 1
}
return $IncidentID.Substring(1,$IncidentID.Length-2) +":"+$flags
}
#the function takes a security boundary safe data and splits it into
#the incident ID GUID (putting back the {} brackets), and the
#flags
function SplitSBSData($SBSData, [ref]$IncidentID, [ref]$Flags)
{
$IncidentID.value = "{" + $SBSData.Substring(0,$SBSData.IndexOf(":")) + "}"
$Flags.value = $SBSData.Substring($SBSData.IndexOf(":")+1)
}
function GetInstanceHashCode($RootCause, $CatchAllInUse)
{
$hashCode = "";
$repairEnum = $RootCause.Repairs;
$repairCount = $repairEnum.Count;
$repairEnum.Reset();
if($repairCount -gt 0)
{
$repair = $repairEnum.Next
if($CatchAllInUse)
{
#make sure we use the hash for the first catch-all repair
for($i=0; ($i -lt $repairEnum.Count) -and (!($repair.Flags -band $RF_RESERVED_CA)); $i++)
{
$repair = $repairEnum.Next;
}
}
$hashCode = $repair.Description.GetHashCode();
}
else
{
$hashCode = $RootCause.Description.GetHashCode();
}
return $hashCode;
}
function GetInstanceIDRC($RootCause, $CatchAlls, $CatchAllsAltRC)
{
$rootCause = $RootCause;
$rootCauseID = $RootCause.RootCauseID;
#if catch all is in use, we need to use the catch all root cause information in the
#instance ID so that all catch-all matches go to the same root cause in the session
$catchAllInUse = $null;
$filterValue = GetRootCauseFilterValue ($RootCause) ($null);
if($filterValue -eq $FV_MISSING)
{
#rc is not in the filter list, catch all instance ID information should be used if present
$className = $rootCause.ClassName;
if($className)
{
#is there a chatch all fo the helper class?
$catchAllRC = $CatchAlls[$className];
if($catchAllRC)
{
#user the catch-all root cause
$rootCause = $catchAllRC;
#does the catch-all have an alternate root cause?
$altRCID = $CatchAllsAltRC[$catchAllRC];
if($altRCID)
{
#use alternate root cause ID
$rootCauseID = $catchAllsAltRC[$catchAllRC];
}
$catchAllInUse = $true;
}
}
}
$hashCode = GetInstanceHashCode($rootCause) ($catchAllInUse);
return $rootCauseID.Substring(1,$rootCauseID.Length-2) + ":" + $hashCode;
}
function GetInstanceID($RootCauseIndex, $RootCauses, $CatchAlls, $CatchAllsAltRC)
{
#approach for generating unique instance ID:
# (1) the root cause GUID (stripped of the { })
# (2) the hash code of the first repair's text
# the repair text is guaranteed to be unique between root causes, and constant across reruns
# If no repairs, then the root cause text hash code is used
$hashCode = 0
$rootCauseID = 0
#find the first repair for this root cause
$rootCauseCount = $RootCauses.Count
$RootCauses.Reset()
for($i=0; $i -lt $rootCauseCount; $i++)
{
$rootCause=$RootCauses.Next;
#root cause index is the index of the root cause in the root cause list
#find the specific root cause
if($i -eq $RootCauseIndex)
{
break;
}
}
if($i -eq $rootCauseCount)
{
throw "Could not find a root cause with index " + $RootCauseIndex;
}
return GetInstanceIDRC($rootCause) ($CatchAlls) ($CatchAllsAltRC);
}
function GetSkipReasonText($Reason)
{
#this is for debug, OK to not localize
if($Reason -eq $NDF_SKIPREASON_ADAPTER)
{
return "An interactive fix for another network interface has been attempted."
}
elseif($Reason -eq $NDF_SKIPREASON_DUPLICATE)
{
return "Another repair with the same ID applying to the same network interface has been attempted."
}
}
function InContextEntry()
{
$IT_HelperClassName = Get-DiagInput -ID "IT_HelperClassName"
if($IT_HelperClassName -eq $null -or $IT_HelperClassName[0].Length -eq 0) {
#Failed retriving HelperClassName from In-Context answer file
return $null
}
#get input attributes
$IT_HelperAttributes = Get-DiagInput -ID "IT_HelperAttributes"
if($IT_HelperAttributes -eq $null) {
#Failed retriving HelperAttributes from In-Context answer file
return $null
}
return @{"HelperClassName" = $IT_HelperClassName; "HelperAttributes" = $IT_HelperAttributes}
}