export users from a local group with domain membership

export users from a local group with domain membership

Post by RGF2aW » Wed, 03 Aug 2005 11:46:02


Hey guys, basically what I'm having trouble writing is a script that
basically grabs all the users from a local group on a server (which have
domain users) and export them to ldf or whatever, then import them back or
import back to another servers local group/domain group.

The idea is I'm in the process of rebuilding a server, unfort... this server
has 3 local groups which contain alot of domain user accounts. I dont want to
enter all this manually again. So I want a script where I could do a quick
export of users of that group (local) then re-import them to the new servers
"local" group.... any help would be greatly appreciated.

thanks again.
--
David
 
 
 

export users from a local group with domain membership

Post by Jerold Sch » Wed, 03 Aug 2005 22:26:47


See tip 9346 How can I generate a CSV file of all local group membership?
in the 'Tips & Tricks' at http://www.yqcomputer.com/

 
 
 

export users from a local group with domain membership

Post by RGF2aW » Wed, 03 Aug 2005 23:38:05

thanks for your reply... by the way is there a way to accomplish this via
wsh/vbs?

thanks again.
--
David
 
 
 

export users from a local group with domain membership

Post by Jerold Sch » Thu, 04 Aug 2005 21:18:36


I am sure that there is, but I don't know how.
 
 
 

export users from a local group with domain membership

Post by MV » Thu, 04 Aug 2005 22:39:18

avid wrote:

Hi,

Below is two VBScripts, one that export only domain users to a text
file (will not include any domain groups or local users), and one
that imports them back again.

The entries in the file will be of this format:
<domain name>/<account name>


Export script:

'--------------------8<----------------------
Const OpenAsASCII = 0
Const OverwriteIfExist = -1

' Group to export domain users from
sGroupName = "Administrators"

' file where result is written to
sOutFile = "C:\Logs\" & sGroupName & ".txt"


Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshNet = CreateObject("WScript.Network")

' get name of local computer
sNode = oWshNet.ComputerName

' connect to the group using ADSI
Set oGroup = GetObject("WinNT://" & sNode & "/" & sGroupName)

Set fOutFile = oFSO.CreateTextFile _
(sOutFile, OverwriteIfExist, OpenAsASCII)

' loop through all members of the group and write to the file
For Each oMember In oGroup.Members

If oMember.Class = "User" Then

On Error Resume Next ' implicit Err.Clear
' try to connect to user object to see if account is a local user
Set oUser = GetObject("WinNT://" & sNode & "/" _
& oMember.Name & ",user")

If Err.Number <> 0 Then
' user is not local, write to file!
fOutFile.WriteLine Mid(oMember.ADsPath, 9)
End If

End If
Next

fOutFile.Close

MsgBox "Export finished!", vbInformation + vbSystemModal

'--------------------8<----------------------


Import script:


'--------------------8<----------------------

Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1

' Group to import domain users from
sGroupName = "Administrators"

' file where the users are listed
sInFile = "C:\Logs\" & sGroupName & ".txt"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshNet = CreateObject("WScript.Network")

' get name of local computer
sNode = oWshNet.ComputerName

' connect to the group using ADSI
Set oGroup = GetObject("WinNT://" & sNode & "/" & sGroupName)

Set fInFile = oFSO.OpenTextFile(sInFile, ForReading, _
FailIfNotExist, OpenAsASCII)

Do Until fInFile.AtEndOfStream
sUser = fInFile.ReadLine
If sUser <> "" Then
' add user to the group
' suppress error in case some users are already a member
On Error Resume Next
oGroup.Add("WinNT://" & sUser)
End If
Loop
On Error Goto 0

fInFile.Close

MsgBox "Import finished!", vbInformation + vbSystemModal

'--------------------8<----------------------


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx
 
 
 

export users from a local group with domain membership

Post by RGF2aW » Fri, 05 Aug 2005 03:26:09

his is exactly what I was looking for - I appreciate you guys taking out
some time.

thanks again!
--
David


"Torgeir Bakken (MVP)" wrote: