-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolKit.AssafM.WSC
1074 lines (970 loc) · 35.4 KB
/
ToolKit.AssafM.WSC
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
<?xml version="1.0"?>
<package>
<component id="ToolKit.AssafM.WSC">
<comment>
This component enables scripts to call procedures That are
relevant for Remote and Local Computer Registry, Event Log Access
and Service Status.
Log File Writing, WMI Access, AD Object Find.
</comment>
<?component error="true" debug="true"?>
<registration
description="Assaf Miron's ToolKit Component"
progid="ToolKit.AssafM.WSC"
version="1.00"
classid="{052cb1e5-6b17-4f5c-abed-9c3f7e2cc31b}"
remotable="True">
<script language="VBScript">
<![CDATA[
strComponent = "Assaf Miron's ToolKit Component"
'******************************************************************************
Function Register
MsgBox strComponent & " - Windows Script Component registered."
End Function
'******************************************************************************
Function Unregister
MsgBox strComponent & " - Windows Script Component unregistered."
End Function
'******************************************************************************
]]>
</script>
</registration>
<public>
<method name="WriteTextFile">
<parameter name="strFileName"/>
<parameter name="strOutput"/>
<parameter name="blnWriteEmptyLines"/>
</method>
<method name="RunLocalCommand">
<parameter name="strCommand"/>
</method>
<method name="RunRemoteCommand">
<parameter name="strComputer"/>
<parameter name="strCommand"/>
</method>
<method name="GetServiceStatus">
<parameter name="strComputer"/>
<parameter name="strService"/>
</method>
<method name="WMIDateStringToDate">
<parameter name="dtmEventDate"/>
</method>
<method name="GetEvents">
<parameter name="strComputer"/>
<parameter name="dStartDate"/>
<parameter name="dEndDate"/>
<parameter name="arrEventIDs"/>
<parameter name="evtLogName"/>
<parameter name="blnGetDistinct"/>
</method>
<method name="CheckWMIAccess">
<parameter name="strComputer"/>
</method>
<method name="FindADObject">
<parameter name="strObj"/>
<parameter name="ObjClass"/>
<parameter name="objSearchCat"/>
</method>
<method name="CheckADPathName">
<parameter name="strADPath"/>
</method>
<method name="GetEnvPath">
<parameter name="strEnvPath"/>
</method>
<method name="SendMail">
<parameter name="ToAddress"/>
<parameter name="CCAddress"/>
<parameter name="BCCAddress"/>
<parameter name="MessageSubject"/>
<parameter name="MessageBody"/>
<parameter name="AttachmentPath"/>
<parameter name="blnSend"/>
<parameter name="blnDisplay"/>
</method>
<method name="GetWMICollectionItem">
<parameter name="strComputer"/>
<parameter name="strWMIQuery"/>
</method>
<method name="RegReadRemoteValue">
<parameter name="strComputer"/>
<parameter name="strValuePath"/>
</method>
<method name="RegKeyExsists">
<parameter name="strComputer"/>
<parameter name="strKeyPath"/>
</method>
<method name="RegValueExsists">
<parameter name="strComputer"/>
<parameter name="strValuePath"/>
</method>
<method name="RegWriteRemoteValue">
<parameter name="strComputer"/>
<parameter name="strValuePath"/>
<parameter name="strValueData"/>
<parameter name="strValueType"/>
</method>
<method name="RegReadLocalValue">
<parameter name="strValuePath"/>
</method>
<method name="RegWriteLocalValue">
<parameter name="strValuePath"/>
<parameter name="strValueData"/>
<parameter name="strValueType"/>
</method>
</public>
<private>
<method name="ReadNDelete">
<parameter name="strPath"/>
</method>
<method name="RegExFind">
<parameter name="strText"/>
<parameter name="strPattern"/>
</method>
<method name="fFormat2Digits">
<parameter name="strNum"/>
</method>
<method name="strToRegConst">
<parameter name="strRegConst"/>
</method>
<method name="ReadValue">
<parameter name="objReg"/>
<parameter name="lngHive"/>
<parameter name="strSubKey"/>
<parameter name="strValueName"/>
<parameter name="lngValueType"/>
<parameter name="vntValueData"/>
</method>
<method name="WriteValue">
<parameter name="objReg"/>
<parameter name="lngHive"/>
<parameter name="strSubKey"/>
<parameter name="strValueName"/>
<parameter name="lngValueType"/>
<parameter name="vntValueData"/>
</method>
<method name="KeyExists">
<parameter name="objReg"/>
<parameter name="lngHive"/>
<parameter name="strSubKey"/>
<parameter name="strKeyName"/>
</method>
<method name="ValueExists">
<parameter name="objReg"/>
<parameter name="lngHive"/>
<parameter name="strSubKey"/>
<parameter name="strValueName"/>
</method>
</private>
<script language="VBScript">
<![CDATA[
' *-*-*-*-*-*-*-*-*-*
' Constsnts
' *-*-*-*-*-*-*-*-*-*
' StdRegProv Constants
'-------------------------------
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7
' File Constants
'---------------------------------
Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8
' Service Constants
'----------------------------
Const ADS_SERVICE_STOPPED = 1
Const ADS_SERVICE_START_PENDING = 2
Const ADS_SERVICE_STOP_PENDING = 3
Const ADS_SERVICE_RUNNING = 4
Const ADS_SERVICE_CONTINUE_PENDING = 5
Const ADS_SERVICE_PAUSE_PENDING = 6
Const ADS_SERVICE_PAUSED = 7
Const ADS_SERVICE_ERROR = 8
' *-*-*-*-*-*-*-*-*-*
' Public Functions
' *-*-*-*-*-*-*-*-*-*
'******************************************************************************
' Description : Converts String Environment Constants to the Environment Path Requested
' Input : String Environment Path Enclosed in %<ENV_PATH>%
' Output : The Environment Real Path
Function GetEnvPath(strEnvPath)
Dim WshShell
Dim objRegEx
Dim colMatches
Dim strMatch, strNewPath
Set WshShell = CreateObject("WScript.Shell")
' Check Input For Environments Constants
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "%(.*)%"
Set colMatches = objRegEx.Execute(strEnvPath)
' Check that The Path Contains Env Consts
If colMatches.Count > 0 Then
For Each strMatch in colMatches
strEnvPath = Replace(strEnvPath,strMatch,WshShell.ExpandEnvironmentStrings(strMatch))
Next
Else
' Return The Path Unchanged
End If
' Clean Up
Set objRegEx = Nothing
Set WshShell = Nothing
GetEnvPath = strEnvPath
End Function
'******************************************************************************
' Description : Write appended data to text file.
' Creates the File if it Does Not exists.
' Input : File Path, Output Text, (Boolean) Write One Empty Line?
' Output : Append the Output Text to the Log File
Sub WriteTextFile(strFileName, strOutput, blnWriteEmptyLines)
Dim objFSO, objTextStream
'Open text file for output.
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check if the File Exists - Append it
If objFSO.FileExists(strFileName) Then
Set objTextStream = objFSO.OpenTextFile(strFileName, FOR_APPENDING)
Else ' File does not Exists - Create it
Set objTextStream = objFSO.CreateTextFile(strFileName)
End If
'Write data to file.
objTextStream.WriteLine "[" & Now & "]" & vbTab & strOutput
If blnWriteEmptyLines Then
' Write an Empty line
objTextStream.WriteBlankLines(1)
End If
' Close the File for Editing
objTextStream.Close
' Clean Up
Set objTextStream = Nothing
Set objFSO = Nothing
End Sub
'******************************************************************************
' Description : This Function Checks the Service State and returns it
' Input : Computer Name, Service Name
' Output : Service Status
Function GetServiceStatus (strComputer, strService)
Dim cp 'As IADsComputer
Dim sr 'As IADsService
Dim so 'As IADsServiceOperations
Dim strSvcState
' Connect to the Computer
Set cp = GetObject("WinNT://" & strComputer & ",computer")
' Bind to the Service
Set sr = cp.GetObject("Service", strService)
' Bid to the Service Operations
Set so = sr
' Get the Service Status
Select Case so.Status
Case ADS_SERVICE_STOPPED
strSvcState = "Stopped"
Case ADS_SERVICE_RUNNING
strSvcState = "Running"
Case ADS_SERVICE_PAUSED
strSvcState = "Paused"
Case ADS_SERVICE_ERROR
strSvcState = "Errors"
End Select
' Return the Service State
GetServiceStatus = strSvcState
End Function
'******************************************************************************
' Description : Runs a command in the Windows Shell and Returns its Output
' Input : Command String
' Output : The Command Output as seen on Windows Shell (CMD)
Function RunLocalCommand(strCommand)
'On Error Resume Next
Dim objShell, objWshScriptExec, objStdOut
Dim intRes
Set objShell = CreateObject("WScript.Shell")
' Run The Command With Run - Get error Code
intRes = objShell.Run("cmd /c " & strCommand & " >> C:\tmp.txt", 0, 1)
If intRes = 0 Then
' Get the Text From the File and Delete it
RunCommand = ReadNDelete("C:\tmp.txt")
Else
RunCommand = intRes ' Return the Error Code
End If
End Function
'******************************************************************************
' Description : This Function Runs a Remote Command Using WMI on a Remote Computer
' Input : Computer Name, Command to Run
' Output : Process ID of the Command
Function RunRemoteCommand(strComputer,strCommand)
Dim objWMIProcess
Dim intProcessID
' Bind to the WMI Process Name Spaec
Set objWMIProcess = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_Process")
Err = objWMIProcess.Create("cmd /c " & Command, null, null, intProcessID)
' Check the Error Number
If Err.Number = 0 Then
' No Error - Return the Process ID
RunCommand = intProcessID
Else
' Process Could not Run - Return Error
RunCommand = -1
End If
End Function
'******************************************************************************
' Description : Connects to the WMI Service and Runs a Query
' Input : Computer to Connect to, Blank or '.' are considerd a Local Computer
' A WMI Query to get Results from
' Output : The Collection Returned from the WMI Query
Function GetWMICollectionItem(strComputer, strWMIQuery)
On Error Resume Next
Dim objWMIService
' Check if the Connection is to a Local or Remote Computer
If strComputer = "" Or strComputer = "." Then
' Connect to the WMI Service in the Local Computer
Set objWMIService = GetObject("winmgmts:root\cimv2")
Else
' Connect to the WMI Service in the Input Computer Name
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Security)}!\\" & strComputer & "\root\cimv2")
End If
' Return the Query Results
Set GetWMICollectionItem = objWMIService.ExecQuery(strWMIQuery)
' Clean up
Set objWMIService = Nothing
End Function
'******************************************************************************
' Description : This Function Converts the WMI Date time Format to Normal Date Time
' Input : WMI Date Time
' Output : Normal Date Time
Function WMIDateStringToDate(dtmEventDate)
WMIDateStringToDate = CDate(Mid(dtmEventDate, 5, 2) & "/" & _
Mid(dtmEventDate, 7, 2) & "/" & Left(dtmEventDate, 4) _
& " " & Mid (dtmEventDate, 9, 2) & ":" & _
Mid(dtmEventDate, 11, 2) & ":" & Mid(dtmEventDate, _
13, 2))
End Function
'******************************************************************************
' Description : Checks WMI Access on a Computer
' Input : Computer Name
' Output : Enum of WMI Access
' 0 - No Problems with WMI Access
' 1 - No Permissions
' 2 - No Such Computer
' 3 - WMI Error
Function CheckWMIAccess(strComputer)
On Error Resume Next
Err = 0
Set objClass = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2:Win32_Service")
' No Permissions
If Err.Number = -2147217405 then
CheckWMIAccess = 1
Exit Function
End If
' No Such Computer
If Err.Number = 462 Then
CheckWMIAccess = 2
Exit Function
End If
' No Problems
If Err.Number = 0 Then
CheckWMIAccess = 0
Exit Function
End If
' Unknown WMI Error
CheckWMIAccess = 3
Exit Function
End Function
'******************************************************************************
' Description : Sends an Email from the Users Outlook to a Dest. Address
' Input : To Address, CC Address, BCC Address, Message Subject, Message Body, Attachment Path
' Output : NONE
Sub SendMail(ToAddress, CCAddress, BCCAddress, MessageSubject, MessageBody, AttachmentPath, blnSend, blnDisplay)
Const olMail = 0
Dim objFSO, objOutlook, objMail
' Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Create the Outlook Object
Set objOutlook = CreateObject("Outlook.Application")
' Create a new Mail Item
Set objMail = objOutlook.CreateItem(olMail)
' Set Message Properties
With objMail
.To = ToAddress ' Set the To Address
.CC = CCAddress ' Set the CC Address
.BCC = BCCAddress ' Set the BCC Address
.Subject = MessageSubject ' Set the E-Mail Subject
.Body = MessageBody ' Set the E-Mail Body
' Check that The Attachment Path is not Empty
If Not AttachmentPath = "" Then
' Check that the Attachment File exists
If objFSO.FileExists(AttachmentPath) Then
.Attachments.Add AttachmentPath ' Add the Attachment
Else
' Inform the User that the File does not Exists
MsgBox "לא נמצא קובץ לצירוף", msgInformation, "צירוף קובץ להודעה"
End If
End If
If blnSend Then
.Send ' Send the Mail
ElseIf blnDisplay Then
.Display ' Display the Message - Don't Send
End If
End With
' Clean up
Set objMail = Nothing
Set objOutlook = Nothing
Set objFSO = Nothing
End Sub
'******************************************************************************
' Description : This Function Will return an Array of Events Between Dates
' This Function Uses LogParser as the Event Log Collector.
' Input : Computer Name, Start Date, End Date, Event IDs Array, Event Log Name
' Output : Array with the Requested Events
Function GetEvents(strComputer, dStartDate, dEndDate, arrEventIDs, evtLogName, blnGetDistinct)
'----------------------------
' DataList Constants
Const adPersistXML = 1
Const adVarChar = 200
Const adInteger = 3
Const MaxCharacters = 500
Const adFldIsNullable = 32
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
'----------------------------
Dim strQuery, strEventIDsQuery
Dim evtMessage, evtID
Dim arrEvents(), arrEvent
Dim colLoggedEvents, objEvent
Dim dtmStartDate, dtmEndDate
Dim oLogQuery
Dim oEVTInputFormat
Dim oCSVOutputFormat
Dim DataList
Dim i
' Create a LogParser Object
Set oLogQuery = CreateObject("MSUtil.LogQuery")
' Create a New Record Set
Set DataList = CreateObject("ADOR.Recordset")
' Create Input Format object
Set oEVTInputFormat = CreateObject("MSUtil.LogQuery.EventLogInputFormat")
oEVTInputFormat.direction = "BW"
' Create Output Format object
Set oCSVOutputFormat = CreateObject("MSUtil.LogQuery.CSVOutputFormat")
oCSVOutputFormat.tabs = True
' Set the Start Date - Today - 7 => Last Week
dtmStartDate = Year(dStartDate) & "-" & fFormat2Digits(Month(dStartDate)) & "-" & fFormat2Digits(Day(dStartDate)) & " 00:00:00"
' Set the End Date - Today
dtmEndDate = Year(dEndDate) & "-" & fFormat2Digits(Month(dEndDate)) & "-" & fFormat2Digits(Day(dEndDate)) & " 00:00:00"
' Assemble the Event IDs
strEventIDsQuery = "(EventID = '" & Join(arrEventIds,"' OR EventID = '") & "')"
' Create query text
strQuery = "SELECT TimeWritten, EventID, Message FROM " & evtLogName
strQuery = strQuery & " WHERE " & strEventIDsQuery
strQuery = strQuery & " AND TimeWritten > '" & dtmStartDate & "' "
strQuery = strQuery & " AND TimeWritten <= '" & dtmEndDate & "'"
' Execute query
Set colLoggedEvents = oLogQuery.Execute(strQuery, oEVTInputFormat)
' Create the DataList Table
DataList.Fields.Append "TimeWritten", adVarChar, MaxCharacters, adFldIsNullable
DataList.Fields.Append "EventID", adVarChar, MaxCharacters, adFldIsNullable
DataList.Fields.Append "EventMessage", adVarChar, MaxCharacters, adFldIsNullable
DataList.Open
' Sort the DataList by DateTime
DataList.Sort = "TimeWritten"
' Check that there are any Events in the Collection
If colLoggedEvents.getColumnCount() > 0 Then
' Loop on Each Event in the collection
Do While Not colLoggedEvents.atEnd
Set objEvent = colLoggedEvents.getRecord
' Get the Event ID
evtID = objEvent.getValue("EventID")
' Get the Event Message
evtMessage = objEvent.getValue("Message")
If blnGetDistinct Then
' Define a Filter:
' If the Event ID and The Message are the Same
DataList.Filter = "EventID = '" & evtID & "' AND EventMessage = '" & evtMessage & "'"
If Not (DataList.BOF) And Not(DataList.EOF) Then
' Move to the Beginning of the DataList
DataList.MoveFirst
Do While Not DataList.EOF
' Found a Matching Event - Delete this Event (Save Only the Last Event)
DataList.Delete
' Get the Next Row
DataList.MoveNext
Loop
End If
End If
' Create a New Row in the Data List
DataList.AddNew
' Append Data to the new Row
DataList("TimeWritten") = FormatDateTime(objEvent.getValue("TimeWritten"),0)
DataList("EventID") = evtID
DataList("EventMessage") = evtMessage
' Update the New Row Data
DataList.Update
' Move to the Next Recordset
colLoggedEvents.moveNext
Loop
' Clear the Filter - If Any
DataList.Filter = ""
' Get the First Row
DataList.MoveFirst
' Re-Dim the Array as the Number of Events in the Dictionary
ReDim arrEvents(DataList.RecordCount-1)
' Init the Index
i = 0
' Copy the Events from the DataList to the Returned Array
Do Until DataList.EOF
ReDim arrEvent(2)
arrEvent(0) = DataList.Fields.Item(0)
arrEvent(1) = DataList.Fields.Item(1)
arrEvent(2) = DataList.Fields.Item(2)
' Add the Event Array to the Return Array
arrEvents(i) = arrEvent
' Increment the Index
i = i + 1
' Get the next Row
DataList.MoveNext
Loop
Else
' Set Events Array to 0
ReDim arrEvents(0)
End If
' Close the DataList
DataList.Close
' Return the Events Array
GetEvents = arrEvents
End Function
'******************************************************************************
' Description : Finds Objects in the Active Directory by Object Class and Search by Category
' Input : Object Name, Object Class, Search By (Category)
' Output : The ADsPath of the Object
Function FindADObject(strObj, ObjClass, objSearchCat)
Const ADS_SCOPE_SUBTREE = 2
Dim objRootDSE,objConnection,objCommand,objRecordSet
Dim strDomainLdap
' Bind to the LDAP Root DSE
Set objRootDSE = GetObject ("LDAP://rootDSE")
' Get the Default Naming Context
strDomainLdap = objRootDSE.Get("defaultNamingContext")
' Create an Active Directory Connection
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
' Set the AD Provider
objConnection.Provider = "ADsDSOObject"
' Open the Connection with the Proper Provider
objConnection.Open "Active Directory Provider"
' Set the Active Connection
Set objCommand.ActiveConnection = objConnection
' Set the Command Text
objCommand.CommandText = _
"SELECT AdsPath FROM 'LDAP://" & strDomainLdap & "' WHERE objectClass='" & ObjClass & "' and " & objSearchCat & "='" &_
strObj & "'"
' Set the Search Properties
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
' Execute the Command
Set objRecordSet = objCommand.Execute
' Check if no Results were found
If objRecordSet.RecordCount = 0 Then
' Return 0
FindADObject= 0
Else
' Found (at Least) One Result
' Re-Query to get the Real Results
objRecordSet.Requery
' Get the First Record
objRecordSet.MoveFirst
' Loop on all Records Found
Do Until objRecordSet.EOF
' Return the ADsPAth Value of the Record
FindADObject= objRecordSet.Fields("AdsPath").Value
' Get the Next Record
objRecordSet.MoveNext
Loop
End If
End Function
'******************************************************************************
' Description : Checks the AD Path Name for Special Characters and Replaces them with the Proper Escape Characters
' Input : AD string Path
' Output : AD string Path with Escape Characters
Function CheckADPathName(strADPath)
Dim NewADPathName
' Save the Original Path - Dont Change the Original Path
NewADPathName = strADPath
' Check if there is a Double Qutes (")
If instr(strADPath,chr(34)) Then
' Insert an Escape Character \"
NewADPathName = Replace(strADPath,chr(34),"\" & chr(34))
' Return the New Path
CheckADPathName = NewADPathName
End If
If instr(strADPath,"\" & chr(34)) Then
' Return the New Path
CheckADPathName = NewADPathName
End If
' Check if there is a Slash (/)
If instr(strADPath,"/") Then
' Insert an Escape Character \/
NewADPathName = Replace(strADPath,"/","\/")
' Return the New Path
CheckADPathName = NewADPathName
End If
' Return the New Path Set before
CheckADPathName = NewADPathName
End Function
'******************************************************************************
' Description : Read a Value from a Local Registry Path
' Input : Key Path
' Output : Value Data OR Error Number, On Error
Function RegReadLocalValue(strValuePath)
Dim WshShell
' Create the Wscript Shell Object
Set WshShell = CreateObject("WScript.Shell")
' Return the Value Data
RegReadLocalValue = WshShell.RegRead(strValuePath)
End Function
'******************************************************************************
' Description : Write a Value to a Local Registry Path
' Input : Value Path, Value Data, Value Data Type
' Output : Error Number
Function RegWriteLocalValue(strValuePath, strValueData, strValueType)
Dim WshShell
' Create the Wscript Shell Object
Set WshShell = CreateObject("WScript.Shell")
' Return the Error Code
RegWriteLocalValue = WshShell.RegWrite(strValuePath, strValueData, strValueType)
End Function
'******************************************************************************
' Description : Read a Value from a Remote Registry Path
' Input : Computer Name, Key Path
' Output : Value Data OR Error Number, On Error
Function RegReadRemoteValue(strComputer, strValuePath)
Dim g_objReg
Dim strHive, strSubKey, strValueName
Dim lngValueType, oValueData
Dim intError
Dim i
Set g_objReg = GetObject("WinMgmts:" _
& "{impersonationlevel=impersonate}!\\" & strComputer & "/root/default:StdRegProv")
' Get the Hive text
strHive = Mid(strValuePath,1,InStr(strValuePath, "\")-1)
' Get the Sub Key Path
strSubKey = Mid(strValuePath, Len(strHive)+2, InStrRev(strValuePath, "\")-1-Len(strHive))
' Get the Value Name
strValueName = Mid(strValuePath, Len(strHive)+ Len(strSubKey)+2, Len(strValuePath))
' Run the Function and get the Error Code
intError = ReadValue(g_objReg, strHive, strSubKey, strValueName, lngValueType, oValueData)
' Check For Errors
If intError = 0 Then
' No Error - Return the Value Data
RegReadRemoteValue = oValueData
Else
' Error on Accessing the Remote Registry - Return Error Code
RegReadRemoteValue = "Error: " & intError
End If
End Function
'******************************************************************************
' Description : Write a Value to a Remote Registry Path
' Input : Computer Name, Value Path, Value Data, Value Data Type
' Output : Value Data OR Error Number, On Error
Function RegWriteRemoteValue(strComputer, strValuePath, strValueData, strValueType)
Dim g_objReg
Dim strHive, strSubKey, strValueName
Dim lngValueType, oValueData
Dim intError
Set g_objReg = GetObject("WinMgmts:" _
& "{impersonationlevel=impersonate}!\\" & strComputer & "/root/default:StdRegProv")
' Get the Hive text
strHive = Mid(strValuePath,1,InStr(strValuePath, "\")-1)
' Get the Sub Key Path
strSubKey = Mid(strValuePath, Len(strHive)+2, InStrRev(strValuePath, "\")-1-Len(strHive))
' Get the Value Name
strValueName = Mid(strValuePath, Len(strHive)+ Len(strSubKey)+2, Len(strValuePath))
' Run the Function and get the Error Code
intError = WriteValue(g_objReg, strHive, strSubKey, strValueName, strValueType, strValueData)
' Check For Errors
If intError = 0 Then
' No Error - Return the Value Data
RegWriteRemoteValue = True
Else
' Error on Accessing the Remote Registry - Return Error Code
RegWriteRemoteValue = "Error: " & intError
End If
End Function
'******************************************************************************
' Description : Check if a Value from a Remote Registry Path exists
' Input : Computer Name, Value Path
' Output : True / False
Function RegValueExsists(strComputer, strValuePath)
Dim g_objReg
Dim strHive, strSubKey, strValueName
Set g_objReg = GetObject("WinMgmts:" _
& "{impersonationlevel=impersonate}!\\" & strComputer & "/root/default:StdRegProv")
' Get the Hive text
strHive = Mid(strValuePath,1,InStr(strValuePath, "\")-1)
' Get the Sub Key Path
strSubKey = Mid(strValuePath, Len(strHive)+2, InStrRev(strValuePath, "\")-1-Len(strHive))
' Get the Value Name
strValueName = Mid(strValuePath, Len(strHive)+ Len(strSubKey)+2, Len(strValuePath))
' Run the Function and Return the Result
RegValueExsists = ValueExists(g_objReg, strHive, strSubKey, strValueName)
End Function
'******************************************************************************
' Description : Check if a Key from a Remote Registry Path exists
' Input : Computer Name, Key Path
' Output : True / False
Function RegKeyExsists(strComputer, strKeyPath)
Dim g_objReg
Dim strHive, strSubKey, strKeyName
Set g_objReg = GetObject("WinMgmts:" _
& "{impersonationlevel=impersonate}!\\" & strComputer & "/root/default:StdRegProv")
' Get the Hive text
strHive = Mid(strValuePath,1,InStr(strValuePath, "\")-1)
' Get the Sub Key Path
strSubKey = Mid(strValuePath, Len(strHive)+2, InStrRev(strValuePath, "\")-1-Len(strHive))
' Get the Key Name
strKeyName = Mid(strValuePath, Len(strHive)+ Len(strSubKey)+2, Len(strValuePath))
' Run the Function and Return the Result
RegKeyExsists = KeyExists(strHive, strSubKey, strKeyName)
End Function
' *-*-*-*-*-*-*-*-*-*
' Private Functions
' *-*-*-*-*-*-*-*-*-*
'******************************************************************************
' Description : Reads a Text File and Then Deletes it
' Input : Path to a Text File
' Output : The Text in the File
Function ReadNDelete(strPath)
'On Error Resume Next
Dim objFSO, objFile
Dim strText
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Open the Text File For Reading
Set objFile = objFSO.OpenTextFile(strPath, FOR_READING)
' Read All the Text File
strText = objFile.ReadAll
' Close the File
objFile.Close
Set objFile = Nothing
' Delete the File
objFSO.DeleteFile strPath, True
' Return the Text
ReadNDelete = strText
End Function
'******************************************************************************
' Description : This Function Will Find a RegEx Pattern on the Input Text
' Function will Return an Array of Results.
' Input : Text to run RegEx on, RegEx Pattern
' Output : Array of Results
Function RegExFind(strText,strPattern)
Dim regEx
Dim Match, Matches, SubMatch
Dim objDictionary
Set regEx = New RegExp
' Set RegEx Properties
regEx.IgnoreCase = True
regEx.Global = True
regEx.Pattern = strPattern
' Create a Dictionary Object
Set objDictionary = CreateObject("Scripting.Dictionary")
' Execute the Regex Command and Save the Matches
Set Matches = regEx.Execute(strText)
' Loop Each Match for Results
For Each Match In Matches
' Loop Each Match For Submatches
For Each SubMatch In Match.Submatches
' Check that the Submatch is not in the Dictionary
If Not objDictionary.Exists(Trim(SubMatch)) Then
' Add Submatch to the Dictionary
objDictionary.Add Trim(SubMatch),Trim(SubMatch)
End If
Next
Next
' Return an Array of Dictionary Keys
RegExFind = objDictionary.Keys
End Function
'******************************************************************************
' Description : This Function Will Format a Number to 2 Digits Format
' Input : Number
' Output : Formated Number
Function fFormat2Digits(strNum)
If Len(strNum) = 1 Then
fFormat2Digits = "0" & strNum
ElseIf Len(strNum) = 0 Then
fFormat2Digits = "00"
Else
fFormat2Digits = strNum
End If
End Function
'******************************************************************************
' Description : Converts String Registry Constant to a Hex Registry Constant
' Input : String Registry Constant
' Output : Hex Registry Constant
Function strToRegConst(strRegConst)
' Check the Input Value
Select Case StrRegConst
Case "HKEY_CLASSES_ROOT"
strToRegConst = HKEY_CLASSES_ROOT
Case "HKCR"
strToRegConst = HKEY_CLASSES_ROOT
Case "ClassesRoot"
strToRegConst = HKEY_CLASSES_ROOT
Case "HKEY_LOCAL_MACHINE"
strToRegConst = HKEY_LOCAL_MACHINE
Case "HKLM"
strToRegConst = HKEY_LOCAL_MACHINE
Case "LocalMachine"
strToRegConst = HKEY_LOCAL_MACHINE
Case "HKEY_CURRENT_USER"
strToRegConst = HKEY_CURRENT_USER
Case "HKCU"
strToRegConst = HKEY_CURRENT_USER
Case "CurrentUser"
strToRegConst = HKEY_CURRENT_USER
Case "HKEY_USERS"
strToRegConst = HKEY_USERS
Case "HKU"
strToRegConst = HKEY_USERS
Case "Users"
strToRegConst = HKEY_USERS
Case "DWORD"
strToRegConst = REG_DWORD
Case "String"
strToRegConst = REG_SZ
Case "Binary"
strToRegConst = REG_BINARY
Case "ExString"
strToRegConst = REG_EXPAND_SZ
Case "MulString"
strToRegConst = REG_MULTI_SZ
End Select
End Function
'******************************************************************************
' Description : Read a Value from a Rgistry Path
' Input : Hive,Sub Key Path , Value Name
' Output : Value Type (by Referance), Value Data(by Referance), Read Error Code (0 - Success, Else Fail)
Function ReadValue(objReg, ByVal lngHive, ByVal strSubKey, ByVal strValueName, ByRef lngValueType, ByRef vntValueData)
Dim lngRC, arrNames, arrTypes, lngN
' Wrapper for the StdRegProv class Get<xxx>Value methods. With this function,
' the calling code doesn't need to know the registry data type beforehand. The
' lngHive, strSubKey, and strValueName parameters are input parameters that
' specify the data to retrieve, and lngValueType and vntValueData are output
' parameters that will contain the retrieved value type and data. Returns 0 for
' success, or non-zero failure.
lngValueType = 0
vntValueData = Null
lngHive = strToRegConst(lngHive) ' Convert Hive to Registry Constant
lngRC = objReg.EnumValues(lngHive, strSubKey, arrNames, arrTypes)
If lngRC = 0 Then
For lngN = 0 To UBound(arrNames)
If LCase(arrNames(lngN)) = LCase(strValueName) Then
Select Case arrTypes(lngN)
Case REG_SZ
lngRC = objReg.GetStringValue(lngHive, strSubKey, arrNames(lngN), vntValueData)
lngValueType = REG_SZ
Exit For
Case REG_EXPAND_SZ
lngRC = objReg.GetExpandedStringValue(lngHive, strSubKey, arrNames(lngN), vntValueData)
lngValueType = REG_EXPAND_SZ
Exit For
Case REG_BINARY
lngRC = objReg.GetBinaryValue(lngHive, strSubKey, arrNames(lngN), vntValueData)
lngValueType = REG_BINARY
Exit For
Case REG_DWORD
lngRC = objReg.GetDWORDValue(lngHive, strSubKey, arrNames(lngN), vntValueData)
lngValueType = REG_DWORD
Exit For
Case REG_MULTI_SZ
lngRC = objReg.GetMultiStringValue(lngHive, strSubKey, arrNames(lngN), vntValueData)
lngValueType = REG_MULTI_SZ
Exit For
End Select
End If
Next
End If
ReadValue = lngRC
End Function
'******************************************************************************
' Description : Writes a Value to a Registry Path
' Input : Hive, Sub Key Path, Value Name, Value Type, Value Data
' Output : Write Error Code (0 - Success, Else Fail)
Function WriteValue(objReg, ByVal lngHive, ByVal strSubKey, ByVal strValueName, ByVal lngValueType, ByVal vntValueData)
Dim lngRC
' Wrapper for the StdRegProv class Set<xxx>Value methods. Using this function,
' the calling code can specify the desired data type in the lngValueType
' parameter and the function will execute the corresponding WMI method. Note