3
Aug/10
0

VB Script: Copy Active Directory Group Members

VB Script that copies all members of Group A to Group B. Requires 2 Arguments (Source Group Name, Destination Group Name)

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
Set objSystemInfo = CreateObject("ADSystemInfo") 
strDomain = objSystemInfo.DomainShortName
 
strSGroupDN = GetObjectDN(WScript.Arguments(0), strDomain)
strDGroupDN = GetObjectDN(WScript.Arguments(1), strDomain)
 
WScript.Echo ""
WScript.Echo "     Source Group: " & strSGroupDN 
WScript.Echo "Destination Group: " & strDGroupDN 
WScript.Echo ""
 
set dicSeenGroupMember = CreateObject("Scripting.Dictionary")
set objDGroup = GetObject("LDAP://" & strDGroupDN)
 
CopyMembers "LDAP://" & strSGroupDN, dicSeenGroupMember
 
Function CopyMembers (strGroupADsPath, dicSeenGroupMember)
	set objGroup = GetObject(strGroupADsPath)
	WScript.Echo "Adding Users to " & WScript.Arguments(1)
	for each objMember In objGroup.Members
		On Error Resume Next
		If (objDGroup.Add("LDAP://" & objMember.distinguishedName)) Then
			WScript.Echo "  " & objMember.displayName & " (Already Member)"
		Else 
			WScript.Echo "  " & objMember.displayName
		End If
	next
End Function
 
Function GetObjectDN(strObject, strDomain)
	Const ADS_NAME_INITTYPE_GC = 3
	Const ADS_NAME_TYPE_1779 = 1
	Const ADS_NAME_TYPE_NT4 = 3
 
	Dim objNameTranslate
	Dim strObjectDN
 
	On Error Resume Next : Err.Clear
	Set objNameTranslate = CreateObject("NameTranslate")
 
	objNameTranslate.Init ADS_NAME_INITTYPE_GC, ""
	objNameTranslate.Set ADS_NAME_TYPE_NT4, strDomain & "\" & strObject
	strObjectDN = objNameTranslate.Get(ADS_NAME_TYPE_1779)
	If Err.Number <> 0 Then
		strObjectDN = ""
	End If
 
	Set objNameTranslate = Nothing
	On Error Goto 0
	GetObjectDN = strObjectDN
End Function
Print This Post
(2 votes, average: 5.00 out of 5)
Loading ... Loading ...
83 views
18
Feb/10
0

Add Domain Users / Groups to local Groups remotely

Script that adds a Domain User to a local Group remotely.

Change the “DomainName” variable to suit your requirements.

Requires 3 Arguments:
[Machine Name] [Local Group to modify] [Domain User/Group to add]

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
If (WScript.Arguments.Count < 3) Then
        WScript.Echo "3 Arguments required: [Machine Name] [Local Group to modify] [Domain User/Group to add]"
        WScript.Quit 1
End If
 
Dim DomainName
Set net = WScript.CreateObject("WScript.Network")
 
DomainName = "yourdomain.com"
 
WScript.Echo ""
WScript.Echo WScript.Arguments(0)
WScript.Echo "==================="
 
set group = GetObject("WinNT://"& WScript.Arguments(0) &"/"& WScript.Arguments(1) &"")
 
on error resume next
 
WScript.Echo "Before:"
For Each objUser In group.Members
  WScript.Echo objUser.Name
next
group.Add "WinNT://"& DomainName &"/"& WScript.Arguments(2) &""
CheckError
 
sub CheckError
        if not err.number=0 then
        set ole = CreateObject("ole.err")
                WScript.Echo ole.oleError(err.Number)
                err.clear
        else
                WScript.Echo ""
                WScript.Echo "After:"
                For Each objUser In group.Members
                  WScript.Echo objUser.Name
                next
        end if
end sub
Print This Post
(1 votes, average: 4.00 out of 5)
Loading ... Loading ...
517 views
18
Feb/10
0

Get Service Pack Levels of Servers via Active Directory

Script that queries Active Directory to retrieve Service Pack Levels (In this example only from Windows Server 2003 Machines)
Modify the strContainer variable to your requirements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
strContainer = "DC=yourdomain,DC=com"
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = ("ADsDSOObject")
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Page Size") = 1000
objCommand.CommandText = _
  "SELECT CN, operatingSystem, operatingSystemVersion, operatingSystemServicePack FROM 'LDAP://" & strContainer & "' " _
   & "WHERE objectCategory='computer' AND operatingSystem = 'Windows Server 2003' "
Set objRecordSet = objCommand.Execute
objRecordSet.Sort = "CN"
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
  Wscript.Echo objRecordSet.Fields("CN").Value & "," & objRecordSet.Fields("operatingSystem").Value & "," & objRecordSet.Fields("operatingSystemVersion").Value & "," 
  & objRecordSet.Fields("operatingSystemServicePack").Value
  objRecordSet.MoveNext
Loop
Print This Post
(1 votes, average: 5.00 out of 5)
Loading ... Loading ...
244 views
18
Feb/10
0

Set the local User Password remotely via script

3 simple Lines that update a local account using WinNT on a computer.
Requires 3 Arguments:
[Machine Name] [Local User] [New Password]

1
2
3
4
5
6
7
If (WScript.Arguments.Count &lt; 3) Then
   WScript.Echo "3 Arguments required: [Machine Name] [Local User] [New Password]"
   WScript.Quit 1
End If
Set objUser = GetObject("WinNT://" &amp; WScript.Arguments(0) &amp; "/" &amp; WScript.Arguments(1) &amp; ", user")
objUser.SetPassword WScript.Arguments(2)
objUser.SetInfo
Print This Post
(2 votes, average: 5.00 out of 5)
Loading ... Loading ...
234 views
30
Sep/09
0

Move Files from a Folder via script

This VBScript moves Files (and only Files!) from one to another directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
sSourceFolder = "C:\Temp1"
sDestinationFolder = "C:\Temp2"
 
For Each sFile In objFSO.GetFolder(sSourceFolder).Files
	If objFSO.FileExists(sDestinationFolder & "\" & objFSO.GetFileName(sFile)) Then
		WScript.Echo "Deleting File:" & sDestinationFolder & "\" & objFSO.GetFileName(sFile)
		objFSO.GetFile(sDestinationFolder & "\" & objFSO.GetFileName(sFile)).Delete
	End If
	WScript.Echo "Moving : " & Chr(34) & objFSO.GetFileName(sFile) & Chr(34) & " to " & sDestinationFolder 
	objFSO.GetFile(sFile).Move sDestinationFolder & "\" & objFSO.GetFileName(sFile)  
Next
Print This Post
(3 votes, average: 5.00 out of 5)
Loading ... Loading ...
298 views