Saturday, 31 May 2008

VBScript to add AD group to local machine

I wanted to add a domain group to a list of windows 2003 virtual machines that i built on the production environment attached to a domain, after googling found some sample script which i modified to get the below script….thanx MS

‘ghanshyam

Dim DomainName
Dim UserAccount
Dim GroupName
DomainName = “sales.Example.ORG”
UserAccount = “userAccount”
GroupName = “groupname”
On Error

Set objShell = CreateObject(”WScript.Shell”)
Set objFSORead1 = CreateObject(”Scripting.FileSystemObject”)
Set objDictionary1 = CreateObject(”Scripting.Dictionary”)
Set objFSORead2 = CreateObject(”Scripting.FileSystemObject”)
Set objDictionary2 = CreateObject(”Scripting.Dictionary”)
Const ForReading = 1
Set objFile1 = objFSORead1.OpenTextFile (”c:\adduserserverlist.txt”, ForReading)
i = 0
Do Until objFile1.AtEndOfStream
strNextLine1 = objFile1.Readline
If strNextLine1 <> “” Then
objDictionary1.Add i, strNextLine1
End If
i = i + 1
Loop
objFile1.Close
For Each strLine1 In objDictionary1.Items
set group = GetObject(”WinNT://”& StrLine1 &”/Remote Desktop Users”)
group.Add “WinNT://”& DomainName &”/”& GroupName &”"
CheckError
Next
Sub CheckError
if not err.number=0 then
set ole = CreateObject(”ole.err”)
WScript.Echo ole.oleError(err.Number), vbCritical
err.clear
else
WScript.Echo “Done.”
end if
end Sub

-Ghanshyam Prakash

No comments: