-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCCMCollectionDesigner.cs
More file actions
2393 lines (2123 loc) · 106 KB
/
SCCMCollectionDesigner.cs
File metadata and controls
2393 lines (2123 loc) · 106 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Management;
using System.Collections;
using System.DirectoryServices.ActiveDirectory;
using System.DirectoryServices;
using System.Threading;
using Microsoft.ConfigurationManagement.ManagementProvider;
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;
using Microsoft.VisualBasic;
namespace AuditSec
{
public partial class SCCMCollectionDesigner : Form
{
void saveSettings(bool disposing)
{
AuditSec.saveSettings_SMS();
if (disposing && (components != null)) components.Dispose();
//base.Dispose(disposing);
AuditSec.Exit("SCCM Collection Designer module ended.",
() => { base.Dispose(disposing); return true; });
}
public class Node
{
public TreeNode node;
public int depth;
public bool uptodate;
public string GetDepthString()
{
StringBuilder ret = new StringBuilder();
for (int i = 0; i < depth; i++) ret.Append("+");
return ret.ToString();
}
override public string ToString()
{
return GetDepthString() + node.Text;
}
}
public class Tree<XNode>
{
public SCCMCollectionDesigner GUI; public TreeView tree; public CheckedListBox list;
public Label label; public Label count; public TextBox basebox, idbox; public string name;
public Func<Tree<XNode>, bool> Refresh_; public Func<Tree<XNode>, XNode, bool> Open_;
public int depth, depth2; public List<string> exclude;
public Hashtable node2item = new Hashtable(), item2node = new Hashtable();
override public string ToString()
{
return name;
}
public Tree(SCCMCollectionDesigner GUI, TreeView tree, CheckedListBox list,
Label label, Label count, TextBox basebox, TextBox idbox, string name,
Func<Tree<XNode>, bool> Refresh_, Func<Tree<XNode>, XNode, bool> Open_,
int depth, int depth2, List<string> exclude,
Func<Label, TextBox, TextBox, bool> Initialize_)
{
this.GUI = GUI; this.tree = tree; this.list = list;
this.label = label; this.count = count; this.basebox = basebox; this.idbox = idbox; this.name = name;
this.Refresh_ = Refresh_; this.Open_ = Open_; this.depth = depth; this.depth2 = depth2; this.exclude = exclude;
if (Initialize_ != null) Initialize_(label, basebox, idbox);
Refresh();
}
public bool shown()
{
return GUI != null && GUI.Visible;
}
public void Initialize()
{
Console.WriteLine("Initializing tree " + (label != null ? label.Text.Trim() : "") + "...");
if (shown()) GUI.clear(tree);
if (list != null) if (shown()) GUI.clear(list);
node2item.Clear();
item2node.Clear();
}
public bool Refresh()
{
Initialize();
bool ret = Refresh_(this);
Console.WriteLine("Refresh tree " + (label != null ? label.Text.Trim() : "")
+ ": " + (ret ? "successful" : "failed"));
return ret;
}
public bool Open(XNode node)
{
bool ret = Open_(this, node);
if (!ret) Console.WriteLine("Open node " + (label != null ? label.Text.Trim() : "")
+ "/" + node.ToString()
+ ": " + (ret ? "successful" : "failed"));
return ret;
}
public List<XNode> selectNodes(Func<XNode, bool> take)
{
List<XNode> output = new List<XNode>();
selectNodes(tree.Nodes, output, take);
return output;
}
void selectNodes(TreeNodeCollection input, List<XNode> output, Func<XNode, bool> take)
{
foreach (TreeNode node in input)
{
XNode xNode = (XNode)node2item[node];
if (take(xNode)) output.Add(xNode);
selectNodes(node.Nodes, output, take);
}
}
}
public class ColNode : Node
{
public string CollectionID;
public string Name;
public string Comment;
public string Members = "";
public string Schedules = "";
public string Query = "";
public Tree<ColNode> tree;
public ColNode(string CollectionID, string Name, string Comment)
{
this.CollectionID = CollectionID;
this.Name = Name;
this.Comment = Comment;
this.uptodate = false;
this.depth = depth;
this.node = null;
}
public string getMembers(bool force)
{
if (force) return Members = getCollectionSchedules(CollectionID);
else return Members.Length > 0 ? Members : Members = getCollectionMembers(CollectionID);
}
public string getSchedules(bool force)
{
if (force) return Schedules = getCollectionSchedules(CollectionID);
return Schedules.Length > 0 ? Schedules : Schedules = getCollectionSchedules(CollectionID);
}
public string getQuery(bool force)
{
if (force) return Query = getCollectionQuery(CollectionID);
return Query.Length > 0 ? Query : Query = getCollectionQuery(CollectionID);
}
}
bool ColRefresh(Tree<ColNode> tree)
{
bool ret = false;
if (tree.depth > 0 && tree.depth2 >= tree.depth
&& tree.idbox.Text.Length > 0)
{
if (tree.shown()) tree.GUI.update(tree.tree, true);
List<ColNode> sub = getSubCollections(tree.idbox.Text, false);
foreach (ColNode item in sub) item.depth = 1;
ret = ColRefresh(tree, tree.tree.Nodes, sub, false);
if (tree.shown()) tree.GUI.sort(tree.tree);
if (tree.shown()) foreach (TreeNode node in tree.tree.Nodes) tree.GUI.expand(node, false);
if (tree.shown()) tree.GUI.update(tree.tree, false);
}
return ret;
}
bool ColRefresh(Tree<ColNode> tree, TreeNodeCollection siblings, List<ColNode> subCols, bool expand)
{
bool ret = false;
{
ret = true; subCols.AsParallel().WithDegreeOfParallelism(expand ? 1 : 8).ForAll(item => {
foreach(ColNode item2 in tree.item2node.Keys)
if (item2.Name.Equals(item.Name)) { item = item2; break; }
if (item.depth <= (expand ? tree.depth2 : tree.depth)
&& take(item.Name, tree.exclude)) try
{
//Console.WriteLine(tree.name + " walk TreeNode name=" + item.Name + " depth=" + item.depth + "/" + tree.depth2 + " expand="
// + expand + " uptodate=" + item.uptodate + " node=" + (item.node == null ? "null" : "defined"));
if (item.node == null)
{
item.node = new TreeNode(item.Name, 0, 1);
tree.item2node[tree.node2item[item.node] = item] = item.node;
if (tree.shown()) tree.GUI.set(tree.count, "" + tree.node2item.Count);
if (tree.shown()) tree.GUI.add(siblings, item.node);
if (item.Name.ToUpper().StartsWith("WW-")) if (tree.shown()) tree.GUI.add(tree.list, item.Name.Substring(3));
}
if (!expand)
{
List<ColNode> sub2 = getSubCollections(item.CollectionID, false);
foreach (ColNode item2 in sub2) item2.depth = item.depth + 1;
if (!ColRefresh(tree, item.node.Nodes, sub2, expand)) ret = false;
item.uptodate = true;
}
}
catch (Exception e) { Console.WriteLine("WQL: " + item.Name + ": " + e.ToString()); }});
}
return ret;
}
bool ColOpen(Tree<ColNode> tree, ColNode item)
{
//if (tree.shown()) tree.GUI.update(tree.tree, true);
bool ret = ColRefresh(tree, item.node.Nodes, getSubCollections(item.CollectionID, false), true);
//if (tree.shown()) tree.GUI.sort(tree.tree);
//if (tree.shown()) tree.GUI.update(tree.tree, false);
return ret;
}
static public string getFirstCollectionID(string CollectionLike)
{
return getFirstCollectionID(CollectionLike, false, true, true);
}
static public string getFirstCollectionID(string CollectionLike, bool nameonly, bool like, bool show)
{
List<ColNode> Collection = getCollection(CollectionLike, nameonly, like, show);
return Collection.Count > 0 ? Collection.First().CollectionID : "";
}
static public ColNode getFirstCollection(string CollectionLike, bool nameonly, bool like, bool show)
{
List<ColNode> Collection = getCollection(CollectionLike, nameonly, like, show);
return Collection.Count > 0 ? Collection.First() : null;
}
static public List<ColNode> getCollection(string CollectionLike)
{
return getCollection(CollectionLike, false, true, true);
}
static public List<ColNode> getCollection(string CollectionLike, bool nameonly, bool like, bool show)
{
List<ColNode> result = new List<ColNode>();
string QUERY =
"SELECT CollectionID, Name, Comment FROM SMS_Collection "
+ " WHERE ( Name " + (like ? "LIKE" : "=") + " \"" + CollectionLike + "\""
+ (nameonly ? "" : " OR Comment " + (like ? "LIKE" : "=") + " \"" + CollectionLike + "\" ")
+ " )";
ManagementScope scope = null; try
{
scope = new ManagementScope(@"\\" + AuditSec.settings.smssrv + @"\Root\sms\" + AuditSec.settings.smssite);
if (show) Console.WriteLine("Running : Query Collection like \"" + CollectionLike + "\""
+ "\n" + QUERY);
ManagementObjectCollection moc = new ManagementObjectSearcher(scope, new ObjectQuery(QUERY)).Get();
if (moc == null) throw new Exception("Query returned null result.");
else foreach (ManagementObject mo in moc)
{
string CollectionID = getStringValue(mo, null, "CollectionID");
string Name = getStringValue(mo, null, "Name");
string Comment = getStringValue(mo, null, "Comment");
string Members = getCollectionMembers(CollectionID);
string Schedules = getCollectionSchedules(CollectionID);
if (CollectionID != null && CollectionID.Length > 0
&& Name != null && Name.Length > 0)
{
ColNode colItem = new ColNode(CollectionID, Name, Comment);
result.Add(colItem);
if (show)
Console.WriteLine(" CollectionID=" + CollectionID
+ "\n Name=" + Name
+ "\n Comment=" + Comment
);
}
}
}
catch (ManagementException ee)
{
string extendedError = ee.Message + ". " + ee.ErrorCode.ToString();
if (extendedError.StartsWith("Quota violation"))
extendedError = "Quota violation. Too long a request (" + QUERY.Length + ")";
try
{
string desc = ee.ErrorInformation["Description"].ToString();
desc = desc.Replace(QUERY, "");
extendedError += "\n" + desc;
}
catch (Exception eee)
{
//Console.WriteLine(eee.ToString());
}
Console.WriteLine("Query interrupted. Management Exception. " + extendedError
+ "\n" + QUERY + "\n" + ee.ToString()
);
if (extendedError.StartsWith("Quota violation"))
Console.WriteLine("Query: " + QUERY);
}
catch (Exception ee)
{
Console.WriteLine("Query interrupted.\n" + ee.ToString());
}
if (scope != null) try
{
scope.Path = new ManagementPath();
}
catch (Exception ee)
{
Console.WriteLine("Could not clean ManagementScope! " + ee.Message);
}
if (show) Console.WriteLine("Returned: Query Collection Like \"" + CollectionLike + "\": " + result.Count);
return result;
}
static List<ColNode> getSubCollections(string parentCollectionID)
{
return getSubCollections(parentCollectionID, true);
}
static string getStringValue(ManagementObject mo, string table, string prop)
{
try
{
if (table == null)
return mo.Properties[prop] != null ? mo.Properties[prop].Value.ToString() : "";
else
{
PropertyData pd = mo.Properties[table] == null
|| mo.Properties[table].Value == null
|| !(mo.Properties[table].Value is ManagementBaseObject)
? null : ((ManagementBaseObject)mo.Properties[table].Value).Properties[prop];
return
pd == null
|| pd.Value == null
? "" : pd.Value.ToString();
}
}
catch (Exception e)
{
Console.WriteLine("getStringValue " + table + "." + prop + " = " + e.Message + "\n" + e.ToString());
return "";
}
}
static List<ColNode> getSubCollections(string parentCollectionID, bool show)
{
List<ColNode> result = new List<ColNode>();
string QUERY =
"SELECT CollectionID, Name, Comment"
+ " FROM SMS_Collection"
+ " RIGHT JOIN SMS_CollectToSubCollect AS CTSC"
+ " ON CTSC.subCollectionID = CollectionID"
+ " WHERE CTSC.parentCollectionID = \"" + parentCollectionID + "\"";
ManagementScope scope = null; try
{
scope = new ManagementScope(@"\\" + AuditSec.settings.smssrv + @"\Root\sms\" + AuditSec.settings.smssite);
if (show) Console.WriteLine("Running : Query SubCollections in parentCollectionID \"" + parentCollectionID + "\""
+ "\n" + QUERY);
ManagementObjectCollection moc = new ManagementObjectSearcher(scope, new ObjectQuery(QUERY)).Get();
if (moc == null) throw new Exception("Query returned null result.");
else foreach (ManagementObject mo in moc)
{
string CollectionID = getStringValue(mo, null, "CollectionID");
string Name = getStringValue(mo, null, "Name");
string Comment = getStringValue(mo, null, "Comment");
if (CollectionID != null && CollectionID.Length > 0
&& Name != null && Name.Length > 0)
{
ColNode colItem = new ColNode(CollectionID, Name, Comment);
result.Add(colItem);
if (show)
Console.WriteLine(" CollectionID=" + CollectionID
+ "\n Name=" + Name
+ "\n Comment=" + Comment
);
}
}
}
catch (ManagementException ee)
{
string extendedError = ee.Message + ". " + ee.ErrorCode.ToString();
if (extendedError.StartsWith("Quota violation"))
extendedError = "Quota violation. Too long a request (" + QUERY.Length + ")";
try
{
string desc = ee.ErrorInformation["Description"].ToString();
desc = desc.Replace(QUERY, "");
extendedError += "\n" + desc;
}
catch (Exception eee)
{
//Console.WriteLine(eee.ToString());
}
Console.WriteLine("Query interrupted. Management Exception. " + extendedError
+ "\n" + QUERY + "\n" + ee.ToString()
);
if (extendedError.StartsWith("Quota violation"))
Console.WriteLine("Query: " + QUERY);
}
catch (Exception ee)
{
Console.WriteLine("Query interrupted.\n" + ee.ToString());
}
if (scope != null) try
{
scope.Path = new ManagementPath();
}
catch (Exception ee)
{
Console.WriteLine("Could not clean ManagementScope! " + ee.Message);
}
if (show) Console.WriteLine("Returned: Query SubCollections in parentCollectionID \"" + parentCollectionID + "\": " + result.Count);
return result;
}
public class ADNode : Node
{
public DirectoryEntry de; public string Name;
public string domain, site, siteonly, dpt;
public bool isDomain, isSite, isDpt, invalid;
public Tree<ADNode> tree;
public ADNode(TreeNode node, Tree<ADNode> tree, DirectoryEntry de, int depth)
{
this.node = node;
this.tree = tree;
this.depth = depth;
this.uptodate = false;
Parse(de, this);
//Console.WriteLine(this.ToString());
}
public string ToString()
{
return depth + "/" + tree.depth2 + " " + (invalid ? de.Name + " ***INVALID*** " : Name);
}
}
static void Parse(DirectoryEntry de, ADNode node)
{
node.invalid = true;
if (de.Name.ToUpper().StartsWith("OU=")
|| de.Name.ToUpper().StartsWith("DC="))
{
string path = de.Path == null ? "" : de.Path;
//test environment
path = path.Replace("OU=SCM-000001,OU=DEVINFRA,OU=SDL,", "");
path = Regex.Replace(path, "^LDAP://[A-Z.]+/", "", RegexOptions.IgnoreCase);
path = Regex.Replace(path, "(DC=[A-Z]+),.*$", "$1", RegexOptions.IgnoreCase);
node.isDomain = path.ToUpper().StartsWith("DC=");
node.isSite = path.Count(c => c == ',') == 1;
node.isDpt = path.Count(c => c == ',') == 2;
string[] path_ = Regex.Replace(path, "[A-Z]+=", "", RegexOptions.IgnoreCase).Split(new char[] { ',' });
if (node.isDomain)
{
node.domain = path_[0].ToUpper();
}
else if (node.isSite)
{
node.site = path_[0];
node.domain = path_[1].ToUpper();
node.siteonly = Regex.Replace(node.site, "^" + node.domain + " +", "", RegexOptions.IgnoreCase);
}
else if (node.isDpt)
{
node.dpt = path_[0];
node.site = path_[1];
node.domain = path_[2].ToUpper();
node.siteonly = Regex.Replace(node.site, "^" + node.domain + " +", "", RegexOptions.IgnoreCase);
if (node.siteonly.Length != 3)
{
//Console.WriteLine("AD: Invalid site name: " + node.siteonly + " (name must be exactly 3 letters long)");
return;
}
}
else
{
Console.WriteLine("AD: Unsupported directory entry: " + path + " (only domain, site, dpt are supported)");
return;
}
node.de = de;
node.Name = de.Name.Substring(3);
if (node.isDomain) node.Name = node.Name.ToUpper();
node.invalid = false;
}
}
public bool ADRefresh(Tree<ADNode> tree)
{
bool ret = true;
if (tree.shown()) tree.GUI.update(tree.tree, true);
if (tree.depth > 0 && tree.depth2 >= tree.depth) try
{
DomainCollection dc = Forest.GetCurrentForest().Domains;
Domain[] domains = new Domain[dc.Count]; dc.CopyTo(domains, 0);
domains.ToArray().AsParallel().ForAll(domain =>
{
try
{
ADNode item = new ADNode(null, tree, domain.GetDirectoryEntry(), 1);
if (!item.invalid && !item.de.Name.Equals("DC=DEVMYCOMPANY"))
{
TreeNode node = new TreeNode(item.Name, 2, 3);
item.node = node;
tree.item2node[tree.node2item[node] = item] = node;
//Console.WriteLine(domain.GetDirectoryEntry().Name + " " + item.level + "/" + tree.depth2);
if (tree.shown()) tree.GUI.set(tree.count, "" + tree.node2item.Count);
if (tree.shown()) tree.GUI.add(tree.tree, node);
if (tree.shown()) tree.GUI.add(tree.list, item.Name);
if (!ADRefresh(tree, node, item, false)) ret = false;
}
}
catch (Exception e)
{
ret = false;
Console.WriteLine("AD Error: Domain " + domain + ": " + e.ToString());
}
});
}
catch (Exception e)
{
Console.WriteLine("AD Error: " + e.ToString());
ret = false;
}
if (tree.shown()) tree.GUI.sort(tree.tree);
if (tree.shown()) foreach (TreeNode node in tree.tree.Nodes) tree.GUI.expand(node, false);
if (tree.shown()) tree.GUI.update(tree.tree, false);
return ret;
}
bool F12_alreadyshown = false;
bool ADRefresh(Tree<ADNode> tree, TreeNode node, ADNode item, bool expand)
{
bool ret = true;
if (item.depth < (expand ? tree.depth2 : tree.depth))
{
List<DirectoryEntry> lde2 = new List<DirectoryEntry>();
DirectoryEntries children = null;
if (!item.de.Name.Equals("DC=SDL")
&& !item.de.Name.Equals("DC=DEVMYCOMPANY"))
{
//prod environment
children = item.de.Children;
}
else if (item.de.Name.Equals("DC=SDL")) try
{
//test environment
DirectoryEntry de = item.de; children = de.Children;
foreach (DirectoryEntry de2_ in children) Console.WriteLine(de.Name + "/" + de2_.Name);
de = children.Find("OU=SDL"); children = de.Children;
//foreach (DirectoryEntry de2_ in children) Console.WriteLine(de.Name + "/" + de2_.Name);
de = children.Find("OU=DEVINFRA"); children = de.Children;
//foreach (DirectoryEntry de2_ in children) Console.WriteLine(de.Name + "/" + de2_.Name);
de = children.Find("OU=SCM-000001"); children = de.Children;
//foreach (DirectoryEntry de2_ in children) Console.WriteLine(de.Name + "/" + de2_.Name);
if (!F12_alreadyshown)
{
F12_alreadyshown = true;
MessageBox.Show("Test Environment mode activated.\n\nPress F12 to show Logs.", "Test Environment");
}
}
catch (Exception e)
{
children = null;
Console.WriteLine("Test Environment mode failure\n" + e.ToString());
MessageBox.Show("Test Environment mode failure\n\nPress F12 to show Logs.", "Test Environment");
}
if (children != null) foreach (DirectoryEntry de2_ in children) lde2.Add(de2_);
DirectoryEntry[] ade2 = new DirectoryEntry[lde2.Count]; lde2.CopyTo(ade2, 0);
ade2.AsParallel().WithDegreeOfParallelism(expand ? 1 : 8).ForAll(de2 =>
{
try
{
ADNode item2 = new ADNode(null, tree, de2, item.depth + 1);
if (!item2.invalid)
{
TreeNode node2 = new TreeNode(item2.Name, 0, 1);
item2.node = node2;
if (item2.isDomain
|| (item2.isSite && item2.siteonly.Length == 3)
|| (item2.isDpt && take(item2.dpt, tree.exclude))
)
{
tree.item2node[tree.node2item[node2] = item2] = node2;
if (tree.shown()) tree.GUI.set(tree.count, "" + tree.node2item.Count);
if (tree.shown()) tree.GUI.add(node, node2);
if (item2.isDpt) if (tree.shown()) tree.GUI.add(ADDptBox, item2.dpt);
//Console.WriteLine(level + "+OU=" + item.Name + " (" + item2.de.Path + ")");
}
if (!expand &&!ADRefresh(tree, node2, item2, expand)) ret = false;
}
}
catch (Exception e)
{
Console.WriteLine("AD Error: " + de2.Name + ": " + e.ToString());
ret = false;
}
});
item.uptodate = true;
}
return ret;
}
bool ADOpen(Tree<ADNode> tree, ADNode item)
{
//if (tree.shown()) tree.GUI.update(tree.tree, true);
bool ret = item.uptodate || ADRefresh(tree, item.node, item, true);
//if (tree.shown()) tree.GUI.sort(tree.tree);
//if (tree.shown()) tree.GUI.update(tree.tree, false);
return ret;
}
bool take(string name, List<string> exclude)
{
foreach(string pattern in exclude)
if (Regex.IsMatch(name, pattern))
{
//Console.WriteLine("Exclude: " + name + "(match " + pattern + ")");
return false;
}
return true;
}
Tree<ADNode> AD_TREE;
Tree<ColNode> SCCM_TREE, DPT_TREE;
bool REFRESH_AT_STARTUP = true;
public SCCMCollectionDesigner()
{
InitializeComponent();
AuditSec.InitializeSMS(false, MYCOMPANY_Settings.SMS_DEFAULT_SERVER, MYCOMPANY_Settings.SMS_DEFAULT_SITE);
}
void set(Control control, string text)
{
Invoke(new setControlDelegate(setControl), control, text);
}
public delegate void setControlDelegate(Control control, string text);
void setControl(Control control, string text)
{
if (control is TextBox) ((TextBox)control).Text = text;
else if (control is Label) ((Label)control).Text = text;
else if (control is Button) ((Button)control).Text = text;
else if (control is Form) ((Form)control).Text = text;
}
void add(object control, object value)
{
Invoke(new addItemDelegate(addItem), control, value);
}
public delegate void addItemDelegate(object control, object value);
void addItem(object control, object value)
{
if (control is TreeNode) ((TreeNode)control).Nodes.Add((TreeNode)value);
else if (control is TreeNodeCollection) ((TreeNodeCollection)control).Add((TreeNode)value);
else if (control is TreeView) ((TreeView)control).Nodes.Add((TreeNode)value);
else if (control is DataGridView) ((DataGridView)control).Rows.Add((object[])value);
else if (control is CheckedListBox.ObjectCollection)
{
if (!((CheckedListBox.ObjectCollection)control).Contains((string)value))
((CheckedListBox.ObjectCollection)control).Add((string)value);
DepartmentSubstract();
}
else if (control is CheckedListBox)
{
if (!((CheckedListBox)control).Items.Contains((string)value))
((CheckedListBox)control).Items.Add((string)value);
DepartmentSubstract();
}
}
void clear(object collection)
{
Invoke(new clearCollectionDelegate(clearCollection), collection);
}
public delegate void clearCollectionDelegate(object collection);
void clearCollection(object collection)
{
if (collection is TreeNodeCollection) ((TreeNodeCollection)collection).Clear();
else if (collection is TreeView) ((TreeView)collection).Nodes.Clear();
else if (collection is CheckedListBox.ObjectCollection) ((CheckedListBox.ObjectCollection)collection).Clear();
else if (collection is CheckedListBox) ((CheckedListBox)collection).Items.Clear();
}
void update(Control control, bool begin)
{
Invoke(new updateControlDelegate(updateControl), control, begin);
}
public delegate void updateControlDelegate(Control control, bool begin);
void updateControl(Control control, bool begin)
{
if (control == DptTree)
{
updateControl(ADDptBox, begin);
updateControl(TemplDptBox, begin);
}
else
{
if (begin)
{
PrepareSite.Enabled = CheckConsistency.Enabled = PrepareDpts.Enabled = ClearButton.Enabled = ProceedButton.Enabled =
SiteCodeBox.Enabled = SiteNameBox.Enabled = startBox.Enabled = hourSpanBox.Enabled =
DomainsBox.Enabled = dptStartBox.Enabled = dptHourSpanBox.Enabled =
ADDptBox.Enabled = ADDptBox.Enabled = control.Visible = false;
DisabledControls.Add(control);
if (control is TreeView) ((TreeView)control).BeginUpdate();
else if (control is CheckedListBox) ((CheckedListBox)control).BeginUpdate();
}
else
{
if (control is TreeView) ((TreeView)control).EndUpdate();
else if (control is CheckedListBox) ((CheckedListBox)control).EndUpdate();
DisabledControls.Remove(control); ControlsToEnable.Add(control);
if (DisabledControls.Count == 0)
{
foreach (Control c in ControlsToEnable) c.Visible = true;
PrepareSite.Enabled = CheckConsistency.Enabled = PrepareDpts.Enabled = ClearButton.Enabled = ProceedButton.Enabled =
SiteCodeBox.Enabled = SiteNameBox.Enabled = startBox.Enabled = hourSpanBox.Enabled =
DomainsBox.Enabled = dptStartBox.Enabled = dptHourSpanBox.Enabled =
ADDptBox.Enabled = ADDptBox.Enabled = true;
}
if (control is TreeView && ((TreeView)control).Nodes.Count > 0)
{
((TreeView)control).SelectedNode = ((TreeView)control).Nodes[0];
((TreeView)control).Nodes[0].EnsureVisible();
}
}
}
}
List<Control> DisabledControls = new List<Control>();
List<Control> ControlsToEnable = new List<Control>();
void sort(object collection)
{
Invoke(new sortCollectionDelegate(sortCollection), collection);
}
public delegate void sortCollectionDelegate(object collection);
void sortCollection(object collection)
{
if (collection is TreeView) ((TreeView)collection).Sort();
else if (collection is CheckedListBox) ((CheckedListBox)collection).Sorted = true;
}
void expand(TreeNode node, bool all)
{
Invoke(new expandNodeDelegate(expandNode), node, all);
}
public delegate void expandNodeDelegate(TreeNode node, bool all);
void expandNode(TreeNode node, bool all)
{
if (all) node.ExpandAll();
else node.Expand();
}
void DepartmentSubstract()
{
Invoke(new dptSubstractDelegate(dptSubstract));
}
public delegate void dptSubstractDelegate();
void dptSubstract()
{
for (int i = 0; i < TemplDptBox.Items.Count; i++)
{
TemplDptBox.SetItemChecked(i, true);
ADDptBox.Items.Remove(TemplDptBox.Items[i]);
}
}
private void ADTree_AfterSelect(object sender, TreeViewEventArgs e)
{
if (!ADTree.Visible) return;
if (e.Node.IsSelected)
{
string patha = e.Node.FullPath;
SiteNameBox.Text = ""; SiteCodeBox.Text = "";
DomainsBox.BeginUpdate();
for (int i = 0; i < DomainsBox.Items.Count; i++) DomainsBox.SetItemChecked(i, false);
ADNode adNode = AD_TREE == null ? null : (ADNode)AD_TREE.node2item[e.Node];
if (adNode != null)
{
TreeNode nodeb = null; string pathb = null;
if (adNode.isSite)
{
Console.WriteLine("AD: selected: " + adNode.domain + "/" + adNode.site);
AD_TREE.Open(adNode);
e.Node.Expand();
SiteCodeBox.Text = adNode.siteonly.ToUpper();
if (adNode.de.Properties["description"].Value != null)
SiteNameBox.Text = adNode.de.Properties["description"].Value.ToString();
int index = DomainsBox.Items.IndexOf(adNode.domain);
if (index != -1) DomainsBox.SetItemChecked(index, true);
foreach (ADNode adNode2 in AD_TREE.selectNodes(adNode2_ => {
return adNode2_ != null && adNode2_.node != adNode.node && adNode2_.isSite
&& ( adNode2_.site.Equals(adNode.site) || adNode2_.siteonly.Equals(adNode.siteonly)); }))
{
//Console.WriteLine("AD: related: " + adNode2.domain + "/" + adNode2.site);
index = DomainsBox.Items.IndexOf(adNode2.domain);
if (index != -1) DomainsBox.SetItemChecked(index, true);
}
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site + " " + adNode.domain);
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site);
}
else if (adNode.isDomain)
{
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain);
}
else if (adNode.isDpt)
{
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site + " " + adNode.domain
+ @"\" + adNode.site + " " + adNode.domain + " " + adNode.dpt);
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site
+ @"\" + adNode.site + " " + adNode.dpt);
}
if (nodeb != null) SCCMTree.SelectedNode = nodeb;
else MessageBox.Show(ADLabel.Text + ": Container selected: " + patha
+ "\n\n" + SCCMLabel.Text + ": Collection not found: " + pathb,
"SCCM Collection Designer");
}
DomainsBox.EndUpdate();
}
}
private void ADTree_AfterCollapse(object sender, TreeViewEventArgs e)
{
if (!ADTree.Visible) return;
if (e.Node.IsSelected)
{
ADNode adNode = AD_TREE == null ? null : (ADNode)AD_TREE.node2item[e.Node]; if (adNode != null)
{
TreeNode nodeb = null; string pathb = null;
if (adNode.isSite)
{
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site + " " + adNode.domain);
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site);
}
else if (adNode.isDomain)
{
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain);
}
else if (adNode.isDpt)
{
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site + " " + adNode.domain
+ @"\" + adNode.site + " " + adNode.domain + " " + adNode.dpt);
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site
+ @"\" + adNode.site + " " + adNode.dpt);
}
if (nodeb != null)
{
nodeb.Collapse();
SCCMTree.SelectedNode = nodeb;
}
//else MessageBox.Show(ADLabel.Text + ": Container selected: " + patha
//+ "\n\n" + SCCMLabel.Text + ": Collection not found: " + pathb,
//"SCCM Collection Designer");
}
} else ADTree.SelectedNode = e.Node;
}
private void ADTree_AfterExpand(object sender, TreeViewEventArgs e)
{
if (!ADTree.Visible) return;
if (e.Node.IsSelected)
{
ADNode adNode = AD_TREE == null ? null : (ADNode)AD_TREE.node2item[e.Node]; if (adNode != null)
{
TreeNode nodeb = null; string pathb = null;
if (adNode.isSite)
{
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site + " " + adNode.domain);
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site);
}
else if (adNode.isDomain)
{
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain);
}
else if (adNode.isDpt)
{
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site + " " + adNode.domain
+ @"\" + adNode.site + " " + adNode.domain + " " + adNode.dpt);
if (nodeb == null) nodeb = find(SCCMTree.Nodes, pathb = adNode.domain + @"\" + adNode.site
+ @"\" + adNode.site + " " + adNode.dpt);
}
if (nodeb != null)
{
nodeb.Expand();
SCCMTree.SelectedNode = nodeb;
}
//else MessageBox.Show(ADLabel.Text + ": Container selected: " + patha
//+ "\n\n" + SCCMLabel.Text + ": Collection not found: " + pathb,
//"SCCM Collection Designer");
}
}
else ADTree.SelectedNode = e.Node;
}
TreeNode find(TreeNodeCollection col, string fullpath)
{
foreach (TreeNode node in col)
{
if (fullpath.Equals(node.FullPath)) return node;
TreeNode ret = find(node.Nodes, fullpath);
if (ret != null) return ret;
}
return null;
}
private void SCCMTree_AfterSelect(object sender, TreeViewEventArgs e)
{
if (!SCCMTree.Visible) return;
TreeNode nodeb = e.Node; if (nodeb.IsSelected)
{
string pathb = nodeb.FullPath;
// (SDL) \ (BBB) (_SDL) \ BBB_ (SDL_)*(GRO OSM)
string patha = Regex.Replace(pathb, @"([^\\]+)\\([^\\ ]+)( [^\\ ]+)*\\[^\\ ]+ (\1 )*([^\\]+)", @"$1\$2\$5");
patha = Regex.Replace(patha, @"([^\\]+)\\([^\\ ]+) [^\\ ]+", @"$1\$2");
ColNode colNode = SCCM_TREE == null ? null : (ColNode)SCCM_TREE.node2item[nodeb];
if (colNode != null)
{
Console.WriteLine("Site collections: selected: " + colNode.Name + ". " + pathb + " --> " + patha);
if (colNode.depth == 2) { SCCM_TREE.Open(colNode); nodeb.Expand(); }
TreeNode nodea = find(ADTree.Nodes, patha);
if (nodea != null) ADTree.SelectedNode = nodea;
else MessageBox.Show(SCCMLabel.Text + ": Collection selected: " + pathb
+ "\n\n" + ADLabel.Text + ": Container not found: " + patha,
"SCCM Collection Designer");
}
}
}
private void SCCMTree_AfterCollapse(object sender, TreeViewEventArgs e)
{
if (!SCCMTree.Visible) return;
TreeNode nodeb = e.Node; if (nodeb.IsSelected)
{