Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Help please Creat AD Group include description

Reply
 
Old 12-03-2008   #1 (permalink)
Clubsprint


 
 

Help please Creat AD Group include description

Hi
I've hobbled together a script that creates AD groups from the contents of a
text file. I'm wondering how I can include
a description from the same text file?

script start================
Dim OrgUnit, GroupName, fso, f, objOU, objGroup
DIM sFile, oFSO, sText, oFile

Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000

Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "C:\test1.txt"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
Set objOU = GetObject("LDAP://ou=Groups,dc=FABRIKAM,dc=COM")
Do While Not oFile.AtEndOfStream
GroupName = oFile.ReadLine
If Trim(GroupName) <> "" Then
Set objOU = GetObject("LDAP://ou=Groups,dc=FABRIKAM,dc=COM")
Set objGroup = objOU.Create("Group", "cn="& GroupName)
objGroup.Put "sAMAccountName", GroupName
objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
ADS_GROUP_TYPE_SECURITY_ENABLED
objGroup.SetInfo

End If
Loop
oFile.Close
Else
WScript.Echo "C:\test1.txt does not exist."
End If
script end===========================

C:\test1.txt looks like this

EngDept Engineering Department
TechDef Technical Definitions
MoPool Motor Pool
HR Human Resources
etc.




My System SpecsSystem Spec
Old 12-03-2008   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: Help please Creat AD Group include description

Clubsprint wrote:
Quote:

> I've hobbled together a script that creates AD groups from the contents of
> a text file. I'm wondering how I can include
> a description from the same text file?
>
> script start================
> Dim OrgUnit, GroupName, fso, f, objOU, objGroup
> DIM sFile, oFSO, sText, oFile
>
> Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
> Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> sFile = "C:\test1.txt"
> If oFSO.FileExists(sFile) Then
> Set oFile = oFSO.OpenTextFile(sFile, 1)
> Set objOU = GetObject("LDAP://ou=Groups,dc=FABRIKAM,dc=COM")
> Do While Not oFile.AtEndOfStream
> GroupName = oFile.ReadLine
> If Trim(GroupName) <> "" Then
> Set objOU = GetObject("LDAP://ou=Groups,dc=FABRIKAM,dc=COM")
> Set objGroup = objOU.Create("Group", "cn="& GroupName)
> objGroup.Put "sAMAccountName", GroupName
> objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
> ADS_GROUP_TYPE_SECURITY_ENABLED
> objGroup.SetInfo
>
> End If
> Loop
> oFile.Close
> Else
> WScript.Echo "C:\test1.txt does not exist."
> End If
> script end===========================
>
> C:\test1.txt looks like this
>
> EngDept Engineering Department
> TechDef Technical Definitions
> MoPool Motor Pool
> HR Human Resources
> etc.
>
Group objects have a description attribute. The trick is to parse each line
of your file to retrieve the values. One potential problem is that
sAMAccountName values can have spaces, so it can be difficult to tell where
the sAMAccountName ends and the description begins. In my example below, I
assume the sAMAccountNames have no embedded spaces, so the first space
delimits the two values.

A few notes. You bind to objOU twice in the script, and the second statement
gets executed for every line read from the file. You only need to bind once
(assuming all groups are created in the same OU). Also, you Dim a few unused
variables. And I would recommend using Option Explicit. My suggestion
follows:
============
Option Explicit

Dim GroupName, objOU, objGroup
Dim sFile, oFSO, oFile
Dim sLine, iIndex, sDesc

Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000

Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "C:\test1.txt"

If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
Set objOU = GetObject("LDAP://ou=Groups,dc=FABRIKAM,dc=COM")

Do While Not oFile.AtEndOfStream
sLine = Trim(oFile.ReadLine)
If (sLine <> "") Then
' Parse line for sAMAccountName and description.
' It is assumed that the first space delimits the two values.
iIndex = InStr(sLine, " ")
GroupName = Left(sLine, iIndex - 1)
sDesc = Trim(Mid(sLine, iIndex + 1))

' Create the group and assign values.
Set objGroup = objOU.Create("Group", "cn=" & GroupName)
objGroup.Put "sAMAccountName", GroupName
objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
ADS_GROUP_TYPE_SECURITY_ENABLED
objGroup.Put "description", sDesc
objGroup.SetInfo

End If
Loop
oFile.Close
Else
WScript.Echo "C:\test1.txt does not exist."
End If

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 12-04-2008   #3 (permalink)
Clubsprint


 
 

Re: Help please Creat AD Group include description

Thanks Richard. as usual a whole lot of help plus some education as well.
I was trying to do it with the split function and failing dismally.
Mark

"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
message news:uK0Uw8bVJHA.5420@xxxxxx
Quote:

> Clubsprint wrote:
>
Quote:

>> I've hobbled together a script that creates AD groups from the contents
>> of a text file. I'm wondering how I can include
>> a description from the same text file?
>>
>> script start================
>> Dim OrgUnit, GroupName, fso, f, objOU, objGroup
>> DIM sFile, oFSO, sText, oFile
>>
>> Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
>> Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
>>
>> Set oFSO = CreateObject("Scripting.FileSystemObject")
>> sFile = "C:\test1.txt"
>> If oFSO.FileExists(sFile) Then
>> Set oFile = oFSO.OpenTextFile(sFile, 1)
>> Set objOU = GetObject("LDAP://ou=Groups,dc=FABRIKAM,dc=COM")
>> Do While Not oFile.AtEndOfStream
>> GroupName = oFile.ReadLine
>> If Trim(GroupName) <> "" Then
>> Set objOU = GetObject("LDAP://ou=Groups,dc=FABRIKAM,dc=COM")
>> Set objGroup = objOU.Create("Group", "cn="& GroupName)
>> objGroup.Put "sAMAccountName", GroupName
>> objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
>> ADS_GROUP_TYPE_SECURITY_ENABLED
>> objGroup.SetInfo
>>
>> End If
>> Loop
>> oFile.Close
>> Else
>> WScript.Echo "C:\test1.txt does not exist."
>> End If
>> script end===========================
>>
>> C:\test1.txt looks like this
>>
>> EngDept Engineering Department
>> TechDef Technical Definitions
>> MoPool Motor Pool
>> HR Human Resources
>> etc.
>>
>
> Group objects have a description attribute. The trick is to parse each
> line of your file to retrieve the values. One potential problem is that
> sAMAccountName values can have spaces, so it can be difficult to tell
> where the sAMAccountName ends and the description begins. In my example
> below, I assume the sAMAccountNames have no embedded spaces, so the first
> space delimits the two values.
>
> A few notes. You bind to objOU twice in the script, and the second
> statement gets executed for every line read from the file. You only need
> to bind once (assuming all groups are created in the same OU). Also, you
> Dim a few unused variables. And I would recommend using Option Explicit.
> My suggestion follows:
> ============
> Option Explicit
>
> Dim GroupName, objOU, objGroup
> Dim sFile, oFSO, oFile
> Dim sLine, iIndex, sDesc
>
> Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
> Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> sFile = "C:\test1.txt"
>
> If oFSO.FileExists(sFile) Then
> Set oFile = oFSO.OpenTextFile(sFile, 1)
> Set objOU = GetObject("LDAP://ou=Groups,dc=FABRIKAM,dc=COM")
>
> Do While Not oFile.AtEndOfStream
> sLine = Trim(oFile.ReadLine)
> If (sLine <> "") Then
> ' Parse line for sAMAccountName and description.
> ' It is assumed that the first space delimits the two values.
> iIndex = InStr(sLine, " ")
> GroupName = Left(sLine, iIndex - 1)
> sDesc = Trim(Mid(sLine, iIndex + 1))
>
> ' Create the group and assign values.
> Set objGroup = objOU.Create("Group", "cn=" & GroupName)
> objGroup.Put "sAMAccountName", GroupName
> objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
> ADS_GROUP_TYPE_SECURITY_ENABLED
> objGroup.Put "description", sDesc
> objGroup.SetInfo
>
> End If
> Loop
> oFile.Close
> Else
> WScript.Echo "C:\test1.txt does not exist."
> End If
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
>

My System SpecsSystem Spec
Old 12-04-2008   #4 (permalink)
Richard Mueller [MVP]


 
 

Re: Help please Creat AD Group include description

I first thought of using Split, but your example had too many space
characters. I can't tell if the file uses tab characters, but if so, you
might be able to use that in the Split function. Or, if the description
starts at a specific column (which did not appear to be the case), you might
use that. I mention this as my method will fail if the group name has
embedded spaces, which is possible. For example, if one row in the file is:

Test Group Group to test applications

there may be no reliable way to parse it. If the values are delimited by tab
characters, you might try Split(sLine, vbTab).

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

"Clubsprint" <spamspamspamspam@xxxxxx> wrote in message
news:gh9qo3$qp7$1@xxxxxx-01.bur.connect.com.au...
Quote:

> Thanks Richard. as usual a whole lot of help plus some education as well.
> I was trying to do it with the split function and failing dismally.
> Mark
>
> "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
> message news:uK0Uw8bVJHA.5420@xxxxxx
Quote:

>> Clubsprint wrote:
>>
Quote:

>>> I've hobbled together a script that creates AD groups from the contents
>>> of a text file. I'm wondering how I can include
>>> a description from the same text file?
>>>
>>> script start================
>>> Dim OrgUnit, GroupName, fso, f, objOU, objGroup
>>> DIM sFile, oFSO, sText, oFile
>>>
>>> Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
>>> Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
>>>
>>> Set oFSO = CreateObject("Scripting.FileSystemObject")
>>> sFile = "C:\test1.txt"
>>> If oFSO.FileExists(sFile) Then
>>> Set oFile = oFSO.OpenTextFile(sFile, 1)
>>> Set objOU = GetObject("LDAP://ou=Groups,dc=FABRIKAM,dc=COM")
>>> Do While Not oFile.AtEndOfStream
>>> GroupName = oFile.ReadLine
>>> If Trim(GroupName) <> "" Then
>>> Set objOU = GetObject("LDAP://ou=Groups,dc=FABRIKAM,dc=COM")
>>> Set objGroup = objOU.Create("Group", "cn="& GroupName)
>>> objGroup.Put "sAMAccountName", GroupName
>>> objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
>>> ADS_GROUP_TYPE_SECURITY_ENABLED
>>> objGroup.SetInfo
>>>
>>> End If
>>> Loop
>>> oFile.Close
>>> Else
>>> WScript.Echo "C:\test1.txt does not exist."
>>> End If
>>> script end===========================
>>>
>>> C:\test1.txt looks like this
>>>
>>> EngDept Engineering Department
>>> TechDef Technical Definitions
>>> MoPool Motor Pool
>>> HR Human Resources
>>> etc.
>>>
>>
>> Group objects have a description attribute. The trick is to parse each
>> line of your file to retrieve the values. One potential problem is that
>> sAMAccountName values can have spaces, so it can be difficult to tell
>> where the sAMAccountName ends and the description begins. In my example
>> below, I assume the sAMAccountNames have no embedded spaces, so the first
>> space delimits the two values.
>>
>> A few notes. You bind to objOU twice in the script, and the second
>> statement gets executed for every line read from the file. You only need
>> to bind once (assuming all groups are created in the same OU). Also, you
>> Dim a few unused variables. And I would recommend using Option Explicit.
>> My suggestion follows:
>> ============
>> Option Explicit
>>
>> Dim GroupName, objOU, objGroup
>> Dim sFile, oFSO, oFile
>> Dim sLine, iIndex, sDesc
>>
>> Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
>> Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
>>
>> Set oFSO = CreateObject("Scripting.FileSystemObject")
>> sFile = "C:\test1.txt"
>>
>> If oFSO.FileExists(sFile) Then
>> Set oFile = oFSO.OpenTextFile(sFile, 1)
>> Set objOU = GetObject("LDAP://ou=Groups,dc=FABRIKAM,dc=COM")
>>
>> Do While Not oFile.AtEndOfStream
>> sLine = Trim(oFile.ReadLine)
>> If (sLine <> "") Then
>> ' Parse line for sAMAccountName and description.
>> ' It is assumed that the first space delimits the two values.
>> iIndex = InStr(sLine, " ")
>> GroupName = Left(sLine, iIndex - 1)
>> sDesc = Trim(Mid(sLine, iIndex + 1))
>>
>> ' Create the group and assign values.
>> Set objGroup = objOU.Create("Group", "cn=" & GroupName)
>> objGroup.Put "sAMAccountName", GroupName
>> objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
>> ADS_GROUP_TYPE_SECURITY_ENABLED
>> objGroup.Put "description", sDesc
>> objGroup.SetInfo
>>
>> End If
>> Loop
>> oFile.Close
>> Else
>> WScript.Echo "C:\test1.txt does not exist."
>> End If
>>
>> --
>> Richard Mueller
>> MVP Directory Services
>> Hilltop Lab - http://www.rlmueller.net
>> --
>>
>>
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Error 183 Unable to creat new folder Vista General
add user to group /group scope - Global /Group type - Security PowerShell
creat account Vista mail
Help. Can't creat photo emails Live Mail
cannot creat new mail Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

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