-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathADReplStatusForm.cs
962 lines (770 loc) · 34.6 KB
/
ADReplStatusForm.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using Microsoft.Win32;
using BrightIdeasSoftware;
using System.Net.NetworkInformation;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace ADReplStatus
{
public partial class ADReplStatusForm : Form
{
public static bool gLoggingEnabled = false;
public static bool gDarkMode = false;
public static bool gErrorsOnly = false;
public static string gLogfileName = string.Empty;
public static string gForestName = string.Empty;
public static string gUsername = string.Empty;
public static string gPassword = string.Empty;
public static string gTarget = string.Empty;
//Added to allow user controlled DC selection
public static bool gUseUserDomainController = false;
public static string gUserDomainController = string.Empty;
public static List<ADREPLDC> gDCs = new List<ADREPLDC>();
public ADReplStatusForm()
{
InitializeComponent();
}
private void RefreshButton_Click(object sender, EventArgs e)
{
ProgressPercentLabel.Visible = true;
ProgressPercentLabel.Text = "0%";
ActiveForm.Text = $"AD Replication Status Tool - {gForestName}";
gDCs.Clear();
foreach (var control in this.Controls)
{
if (control is Button)
{
((Button)control).Enabled = false;
}
}
backgroundWorker1.RunWorkerAsync();
}
private void ADReplStatusForm_Load(object sender, EventArgs e)
{
toolTip1.SetToolTip(RefreshButton, "Refresh Replication Status");
toolTip1.SetToolTip(EnableLoggingButton, "Enable Logging");
toolTip1.SetToolTip(SetForestButton, "Manually Set Forest");
toolTip1.SetToolTip(AlternateCredsButton, "Provide Alternate Credentials");
toolTip1.SetToolTip(ErrorsOnlyButton, "Show Errors Only");
try
{
using (var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\ADREPLSTATUS", false))
{
if (key != null)
{
gForestName = key.GetValue("ForestName", string.Empty).ToString();
gDarkMode = Convert.ToBoolean(key.GetValue("DarkMode", false));
}
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occured while trying to read app settings from the registry!\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (gDarkMode == true)
{
SetDarkMode();
}
else
{
SetLightMode();
}
if (string.IsNullOrEmpty(gForestName))
{
try
{
using (Forest forest = Forest.GetCurrentForest())
{
gForestName = forest.Name;
}
}
catch
{
MessageBox.Show("Unable to detect AD forest. You will need to manually enter the AD forest you wish to scan using the 'Manually Set Forest' button.\nThis happens on non-domain joined computers as well as hybrid or Azure AD domain-joined machines.", "Forest Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
private void ADReplStatusForm_Resize(object sender, EventArgs e)
{
treeListView1.Top = 68;
treeListView1.Left = 12;
if (ActiveForm != null)
{
treeListView1.Width = ActiveForm.Width - 40;
treeListView1.Height = ActiveForm.Height - 119;
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Forest forest = null;
try
{
DirectoryContext ForestContext = null;
if(gUseUserDomainController)
{
if (gLoggingEnabled)
{
System.IO.File.AppendAllText(gLogfileName, $"[{DateTime.Now}] Attempting forest discovery against user specified domain controller {gUserDomainController}\n");
}
DirectoryEntry entry = null;
if (gUsername.Length > 0)
{
backgroundWorker1.ReportProgress(0, $"Attempting to connect to forest {gForestName} with alternate user {gUsername}.");
entry = new DirectoryEntry($"LDAP://{gUserDomainController}/RootDSE", gUsername, gPassword);
//Issues a check and throws an exception if the user specified DC does not exist.
var configNamingContext = entry.Properties["configurationNamingContext"].Value;
ForestContext = new DirectoryContext(DirectoryContextType.Forest, gForestName, gUsername, gPassword);
}
else
{
backgroundWorker1.ReportProgress(0, $"Attempting to connect to forest {gForestName} as currently logged-on user.");
entry = new DirectoryEntry($"LDAP://{gUserDomainController}/RootDSE");
//Issues a check and throws an exception if the user specified DC does not exist.
var configNamingContext = entry.Properties["configurationNamingContext"].Value;
ForestContext = new DirectoryContext(DirectoryContextType.Forest, gForestName);
}
forest = Forest.GetForest(ForestContext);
}
else
{
if (gUsername.Length > 0)
{
backgroundWorker1.ReportProgress(0, $"Attempting to connect to forest {gForestName} with alternate user {gUsername}.");
ForestContext = new DirectoryContext(DirectoryContextType.Forest, gForestName, gUsername, gPassword);
}
else
{
backgroundWorker1.ReportProgress(0, $"Attempting to connect to forest {gForestName} as currently logged-on user.");
ForestContext = new DirectoryContext(DirectoryContextType.Forest, gForestName);
}
forest = Forest.GetForest(ForestContext);
}
}
catch (Exception ex)
{
if(gUseUserDomainController)
{
backgroundWorker1.ReportProgress(0, $"ERROR:Unable to find AD forest:{gForestName}\nUsing user specified target domain controller:{gUserDomainController}\n{ex.Message}\n");
}
else
{
backgroundWorker1.ReportProgress(0, $"ERROR:Unable to find AD forest:{gForestName}\n{ex.Message}\n\nYou probably need to manually enter the forest using the button.");
}
return;
}
DomainCollection domainCollection = forest.Domains;
backgroundWorker1.ReportProgress(0, $"Found {domainCollection.Count} domains in forest {forest.Name}.");
int CurrentDC = 0;
int NumDCs = 0;
foreach (Domain domain in domainCollection)
{
NumDCs += domain.DomainControllers.Count;
}
foreach (Domain domain in domainCollection)
{
DomainControllerCollection DCs = domain.DomainControllers;
foreach (DomainController dc in DCs)
{
ADREPLDC adrepldc = new ADREPLDC();
adrepldc.Name = dc.Name;
adrepldc.DomainName = domain.Name;
try
{
adrepldc.Site = dc.SiteName;
}
catch (Exception ex)
{
backgroundWorker1.ReportProgress((int)(((float)CurrentDC / (float)NumDCs) * 100), $"Failed to contact DC {adrepldc.Name} and fetch site name:{ex.Message}");
adrepldc.Site = "Unknown";
adrepldc.DiscoveryIssues = true;
}
try
{
adrepldc.IsGC = dc.IsGlobalCatalog().ToString();
}
catch (Exception ex)
{
backgroundWorker1.ReportProgress((int)(((float)CurrentDC / (float)NumDCs) * 100), $"Failed to contact DC {adrepldc.Name} and determine global catalog status:{ex.Message}");
adrepldc.IsGC = "Unknown";
adrepldc.DiscoveryIssues = true;
}
try
{
using (DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://" + dc.Name))
{
using (DirectorySearcher search = new DirectorySearcher(directoryEntry))
{
search.ClientTimeout = new TimeSpan(0, 0, 20);
search.Filter = $"(samaccountname={dc.Name.Split('.')[0]}$)";
search.PropertiesToLoad.Add("msDS-isRODC");
SearchResult result = search.FindOne();
if (result == null || result.Properties["msDS-isRODC"].Count == 0)
{
throw new Exception("msDS-isRODC attribute not found!");
}
if ((bool)result.Properties["msDS-isRODC"][0] == true)
{
adrepldc.IsRODC = "True";
}
else
{
adrepldc.IsRODC = "False";
}
}
}
}
catch (Exception ex)
{
backgroundWorker1.ReportProgress((int)(((float)CurrentDC / (float)NumDCs) * 100), $"Failed to determine RODC status for {dc.Name}:{ex.Message}");
adrepldc.IsRODC = "Unknown";
adrepldc.DiscoveryIssues = true;
}
if (adrepldc.DiscoveryIssues == false)
{
try
{
foreach (ReplicationNeighbor partner in dc.GetAllReplicationNeighbors())
{
adrepldc.ReplicationPartners.Add(partner);
}
}
catch (Exception ex)
{
backgroundWorker1.ReportProgress((int)(((float)CurrentDC / (float)NumDCs) * 100), $"Failed to determine replication neighbors and repl status for {dc.Name}:{ex.Message}");
adrepldc.DiscoveryIssues = true;
}
}
gDCs.Add(adrepldc);
CurrentDC++;
backgroundWorker1.ReportProgress((int)(((float)CurrentDC / (float)NumDCs) * 100), "UPDATEPERCENT");
}
}
treeListView1.SetObjects(gDCs);
treeListView1.CanExpandGetter = delegate (object x)
{
return (x is ADREPLDC);
};
treeListView1.ChildrenGetter = delegate (object x)
{
return ((ADREPLDC)x).ReplicationPartners;
};
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
ProgressPercentLabel.Visible = false;
foreach (var control in this.Controls)
{
if (control is Button)
{
((Button)control).Enabled = true;
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (gLoggingEnabled)
{
System.IO.File.AppendAllText(gLogfileName, $"[{DateTime.Now}] {e.UserState}\n");
}
if (e.UserState.ToString().StartsWith("ERROR:"))
{
MessageBox.Show(e.UserState.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (e.UserState.ToString().Equals("UPDATEPERCENT"))
{
ProgressPercentLabel.Text = $"{e.ProgressPercentage}%";
}
}
private void EnableLoggingButton_Click(object sender, EventArgs e)
{
gLoggingEnabled = !gLoggingEnabled;
if (gLoggingEnabled)
{
toolTip1.SetToolTip(EnableLoggingButton, "Disable Logging");
EnableLoggingButton.BackColor = SystemColors.ControlDark;
DateTime Now = DateTime.Now;
gLogfileName = $"adreplstatus_{Now.Month}.{Now.Day}.{Now.Year}.{Now.Hour}.{Now.Minute}.{Now.Second}.log";
System.IO.File.AppendAllText(gLogfileName, $"[{DateTime.Now}] Logging enabled.\n");
}
else
{
toolTip1.SetToolTip(EnableLoggingButton, "Enable Logging");
if (gDarkMode == true)
{
EnableLoggingButton.BackColor = Color.FromArgb(32, 32, 32);
}
else
{
EnableLoggingButton.BackColor = SystemColors.Control;
}
System.IO.File.AppendAllText(gLogfileName, $"[{DateTime.Now}] Logging disabled.\n");
}
}
private void SetForestButton_Click(object sender, EventArgs e)
{
SetForestNameForm setForestNameForm = new SetForestNameForm();
if (gLoggingEnabled)
{
System.IO.File.AppendAllText(gLogfileName, $"[{DateTime.Now}] SetForestName button was clicked.\n");
}
setForestNameForm.ShowDialog();
setForestNameForm.Dispose();
}
private void AlternateCredsButton_Click(object sender, EventArgs e)
{
AlternateCredsForm alternateCredsForm = new AlternateCredsForm();
if (gLoggingEnabled)
{
System.IO.File.AppendAllText(gLogfileName, $"[{DateTime.Now}] AlternateCreds button was clicked.\n");
}
alternateCredsForm.ShowDialog();
alternateCredsForm.Dispose();
}
private void treeListView1_FormatRow(object sender, BrightIdeasSoftware.FormatRowEventArgs e)
{
if (e.Model is ADREPLDC)
{
ADREPLDC dc = (ADREPLDC)e.Model;
if (dc.DiscoveryIssues == true)
{
e.Item.BackColor = Color.Red;
e.Item.ForeColor = Color.White;
}
else
{
if (gDarkMode == true)
{
e.Item.ForeColor = Color.White;
}
}
}
else if (e.Model is ReplicationNeighbor)
{
ReplicationNeighbor neighbor = (ReplicationNeighbor)e.Model;
if (neighbor.ConsecutiveFailureCount > 0)
{
e.Item.BackColor = Color.Red;
e.Item.ForeColor = Color.White;
}
else
{
if (gDarkMode == true)
{
e.Item.ForeColor = Color.White;
}
}
}
}
private void ErrorsOnlyButton_Click(object sender, EventArgs e)
{
gErrorsOnly = !gErrorsOnly;
if (gErrorsOnly == true)
{
toolTip1.SetToolTip(ErrorsOnlyButton, "Show Everything");
ErrorsOnlyButton.BackColor = SystemColors.ControlDark;
treeListView1.ExpandAll();
treeListView1.ModelFilter = new ModelFilter(delegate (object x)
{
if (x is ADREPLDC)
{
return ((ADREPLDC)x).DiscoveryIssues;
}
else if (x is ReplicationNeighbor)
{
return (((ReplicationNeighbor)x).ConsecutiveFailureCount > 0);
}
return false;
});
}
else
{
toolTip1.SetToolTip(ErrorsOnlyButton, "Show Errors Only");
if (gDarkMode == true)
{
ErrorsOnlyButton.BackColor = Color.FromArgb(32, 32, 32);
}
else
{
ErrorsOnlyButton.BackColor = SystemColors.Control;
}
treeListView1.ModelFilter = null;
}
}
private void DCNameColumn_RightClick(object sender, CellRightClickEventArgs e)
{
try
{
//Only display the menu in the context of the "DC Name" column
if (e.Column.Text == "DC Name")
{
//Only display the menu if the cell is populated
if (this.treeListView1.SelectedItem.Text != "")
{
//Create the menustrip
ContextMenuStrip diagnosticMenu = new ContextMenuStrip();
//Add menuItem click handler
diagnosticMenu.ItemClicked += new ToolStripItemClickedEventHandler(diagnosticMenuSelector);
//Create a List view of all the diagnostics we want to add
ObjectListView olv = e.ListView;
//Add the "Ping" option
ToolStripMenuItem pingMenuItem = new ToolStripMenuItem(String.Format($"Ping"));
diagnosticMenu.Items.Add(pingMenuItem);
//Add the "RDP" option
ToolStripMenuItem rdpMenuItem = new ToolStripMenuItem(String.Format($"Initiate RDP connection"));
diagnosticMenu.Items.Add(rdpMenuItem);
//Add the "Enter-PSSession" option
ToolStripMenuItem enterPSSessionMenuItem = new ToolStripMenuItem(String.Format($"Enter PowerShell session"));
diagnosticMenu.Items.Add(enterPSSessionMenuItem);
//Add the "Port Tester" option
ToolStripMenuItem portTesterMenuItem = new ToolStripMenuItem(String.Format($"Port Tester"));
diagnosticMenu.Items.Add(portTesterMenuItem);
//Actually attach the menu to the cell
e.MenuStrip = diagnosticMenu;
}
}
}
catch
{
//Do nothing, the user simply right-clicked somewhere else, this is the handler ONLY when the selected column is "DC name"
}
}
private void diagnosticMenuSelector(object sender, ToolStripItemClickedEventArgs e)
{
switch(e.ClickedItem.ToString())
{
case "Ping":
if (ADReplStatusForm.gLoggingEnabled)
{
System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] Diagnostic ping menu opened.\n");
}
diagnosticPing(sender, e);
break;
case "Initiate RDP connection":
diagnosticRdp(sender,e);
break;
case "Enter PowerShell session":
diagnosticPSSession(sender, e);
break;
case "Port Tester":
diagnosticNetworkTester(sender, e);
break;
}
}
private void diagnosticPing(object sender, ToolStripItemClickedEventArgs e)
{
string destination = this.treeListView1.SelectedItem.Text;
if (destination != "")
{
using (var dialog = new Form())
{
//Set up the ping test window
dialog.Text = "Ping Test";
dialog.StartPosition = FormStartPosition.CenterParent;
dialog.MaximizeBox = false;
dialog.MinimizeBox = false;
dialog.FormBorderStyle = FormBorderStyle.FixedDialog;
dialog.ShowInTaskbar = false;
dialog.Width = 290;
dialog.Height = 150;
var ipv4Button = new Button();
ipv4Button.Text = "IPv4";
ipv4Button.Location = new Point(10, 20);
ipv4Button.Click += (s, ev) => RunPing(destination, AddressFamily.InterNetwork, dialog);
var ipv6Button = new Button();
ipv6Button.Text = "IPv6";
ipv6Button.Location = new Point(180, 20);
ipv6Button.Click += (s, ev) => RunPing(destination, AddressFamily.InterNetworkV6, dialog);
var statusTextBox = new TextBox();
statusTextBox.Multiline = true;
statusTextBox.ReadOnly = true;
statusTextBox.Location = new Point(10, 60);
statusTextBox.Width = dialog.Width - 45;
statusTextBox.Height = dialog.Height - 110;
statusTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
dialog.Controls.Add(ipv4Button);
dialog.Controls.Add(ipv6Button);
dialog.Controls.Add(statusTextBox);
//Add support for dark mode
if (ADReplStatusForm.gDarkMode == true)
{
dialog.BackColor = Color.FromArgb(32, 32, 32);
foreach (var control in dialog.Controls)
{
if (control is Label)
{
((Label)control).BackColor = Color.FromArgb(32, 32, 32);
((Label)control).ForeColor = Color.White;
}
else if (control is TextBox)
{
((TextBox)control).BackColor = Color.FromArgb(32, 32, 32);
((TextBox)control).ForeColor = Color.White;
}
else if (control is Button)
{
((Button)control).BackColor = Color.FromArgb(32, 32, 32);
((Button)control).ForeColor = Color.White;
}
else if (control is CheckBox)
{
((CheckBox)control).BackColor = Color.FromArgb(32, 32, 32);
((CheckBox)control).ForeColor = Color.White;
}
else if (control is RadioButton)
{
((RadioButton)control).BackColor = Color.FromArgb(32, 32, 32);
((RadioButton)control).ForeColor = Color.White;
}
else if (control is ListBox)
{
((ListBox)control).BackColor = Color.FromArgb(32, 32, 32);
((ListBox)control).ForeColor = Color.White;
}
}
}
dialog.ShowDialog(this);
}
}
}
private async void RunPing(string destination, AddressFamily addressFamily, Form dialog)
{
try
{
IPAddress address;
if (!IPAddress.TryParse(destination, out address))
{
var entry = await Dns.GetHostEntryAsync(destination);
address = entry.AddressList.FirstOrDefault(a => a.AddressFamily == addressFamily);
if (address == null)
{
throw new Exception($"No {addressFamily} address found for {destination}");
}
}
using (var p = new Ping())
{
var reply = await p.SendPingAsync(address, 5000, new byte[1], new PingOptions(64, true));
if (reply.Status == IPStatus.Success)
{
string protocol = addressFamily == AddressFamily.InterNetwork ? "IPv4" : "IPv6";
string successMessage = $"Success:\nDCName: {destination} ({reply.Address.ToString()})\nProtocol: {protocol}";
var statusTextBox = (TextBox)dialog.Controls[2];
statusTextBox.Clear();
statusTextBox.AppendText($"Ping to {destination} using {protocol} ({reply.Address.ToString()}) successful.\n");
if (gLoggingEnabled)
{
System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] {statusTextBox.Text}");
}
}
else
{
throw new Exception(reply.Status.ToString());
}
}
}
catch (Exception ex)
{
dialog.Invoke(new Action(() =>
{
string errorMessage = $"Ping failed!\n{ex.Message}\n";
var statusTextBox = (TextBox)dialog.Controls[2];
statusTextBox.Clear();
statusTextBox.AppendText($"{errorMessage}\n");
if (gLoggingEnabled)
{
System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] {errorMessage}");
}
}));
}
}
private void diagnosticRdp(object sender, ToolStripItemClickedEventArgs e)
{
try
{
if (ADReplStatusForm.gLoggingEnabled)
{
System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] Initiating RDP connection to {this.treeListView1.SelectedItem.Text}.\n");
}
string args = $"/v {this.treeListView1.SelectedItem.Text}";
Process.Start($"mstsc.exe", args);
}
catch (Exception ex)
{
string errorMessage = $"ERROR: RDP to {this.treeListView1.SelectedItem.Text} failed!\n{ex.Message}\n";
new Thread(() => MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error));
if (gLoggingEnabled)
{
System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] {errorMessage}\n");
}
}
}
private void diagnosticPSSession(object sender, ToolStripItemClickedEventArgs e)
{
try
{
if (ADReplStatusForm.gLoggingEnabled)
{
System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] Initiating remote powershell session to {this.treeListView1.SelectedItem.Text}.\n");
}
string powershellArgs = $"-NoExit $Cred = Get-Credential;Enter-PSSession -ComputerName {this.treeListView1.SelectedItem.Text} -Credential $Cred";
Process.Start($"powershell.exe", powershellArgs);
}
catch (Exception ex)
{
string errorMessage = $"ERROR: Enter-PsSession -ComputerName {this.treeListView1.SelectedItem.Text} failed!\n{ex.Message}\n";
new Thread(() => MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error));
if (gLoggingEnabled)
{
System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] {errorMessage}\n");
}
}
}
private void diagnosticNetworkTester(object sender, ToolStripItemClickedEventArgs e)
{
gTarget = this.treeListView1.SelectedItem.Text;
PortTester protocolTesterForm = new PortTester();
if (gLoggingEnabled)
{
System.IO.File.AppendAllText(gLogfileName, $"[{DateTime.Now}] Port Tester button was clicked.\n");
}
protocolTesterForm.ShowDialog();
protocolTesterForm.Dispose();
}
private void DarkModeButton_Click(object sender, EventArgs e)
{
gDarkMode = !gDarkMode;
if (gDarkMode == true)
{
SetDarkMode();
}
else
{
SetLightMode();
}
try
{
var key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\ADREPLSTATUS", true);
if (key != null)
{
if (gDarkMode == true)
{
key.SetValue("DarkMode", 1);
}
else
{
key.SetValue("DarkMode", 0);
}
key.Dispose();
}
}
catch (Exception ex)
{
string errorMessage = $"ERROR: Failed to write to the HKCU\\ADREPLSTATUS registry key!\n{ex.Message}\n";
new Thread(() => MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error));
if (gLoggingEnabled)
{
System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] {errorMessage}\n");
}
}
}
void SetDarkMode()
{
toolTip1.SetToolTip(DarkModeButton, "Light Mode");
this.BackColor = Color.FromArgb(32, 32, 32);
foreach (var control in this.Controls)
{
if (control is Button)
{
((Button)control).BackColor = Color.FromArgb(32, 32, 32);
}
if (control is Label)
{
((Label)control).BackColor = Color.FromArgb(32, 32, 32);
((Label)control).ForeColor = Color.White;
}
}
if (gLoggingEnabled == true)
{
EnableLoggingButton.BackColor = SystemColors.ControlDark;
}
if (gErrorsOnly == true)
{
ErrorsOnlyButton.BackColor = SystemColors.ControlDark;
}
treeListView1.BackColor = Color.FromArgb(32, 32, 32);
foreach (OLVColumn item in treeListView1.Columns)
{
var headerstyle = new HeaderFormatStyle();
headerstyle.SetBackColor(Color.FromArgb(32, 32, 32));
headerstyle.SetForeColor(Color.White);
item.HeaderFormatStyle = headerstyle;
}
}
void SetLightMode()
{
toolTip1.SetToolTip(DarkModeButton, "Dark Mode");
this.BackColor = SystemColors.Control;
foreach (var control in this.Controls)
{
if (control is Button)
{
((Button)control).BackColor = SystemColors.Control;
}
if (control is Label)
{
((Label)(control)).BackColor = SystemColors.Control;
((Label)control).ForeColor = SystemColors.ControlText;
}
}
if (gLoggingEnabled == true)
{
EnableLoggingButton.BackColor = SystemColors.ControlDark;
}
if (gErrorsOnly == true)
{
ErrorsOnlyButton.BackColor = SystemColors.ControlDark;
}
treeListView1.BackColor = SystemColors.Window;
foreach (OLVColumn item in treeListView1.Columns)
{
var headerstyle = new HeaderFormatStyle();
headerstyle.SetBackColor(SystemColors.Window);
headerstyle.SetForeColor(SystemColors.ControlText);
item.HeaderFormatStyle = headerstyle;
}
}
private void SetDcButton_Click(object sender, EventArgs e)
{
SetUserDomainControllerForm setUserDCForm = new SetUserDomainControllerForm();
if (gLoggingEnabled)
{
System.IO.File.AppendAllText(gLogfileName, $"[{DateTime.Now}] SetUserDomainController button was clicked.\n");
}
setUserDCForm.ShowDialog();
setUserDCForm.Dispose();
}
}
public class ADREPLDC
{
public string Name;
public string DomainName;
public bool DiscoveryIssues = false;
public string Site;
public string IsGC;
public string IsRODC;
public List<ReplicationNeighbor> ReplicationPartners = new List<ReplicationNeighbor>();
}
}