Solved

check if a server is in a domain then add the domain groups to the local administrators group

Posted on 2015-02-17
3
80 Views
Last Modified: 2015-02-18
Scripting gurus.

I am stuck. I need a script when run on a server in Domain A, it adds domain groups to the local administrators group. If the server is in Domain B, it adds these groups. Here is what I have so far.
Thanks

Set objWMISvc = GetObject( "winmgmts:\\.\root\cimv2" )
Set colItems = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
For Each objItem In colItems
    strComputerName = objItem.Name
    
   strComputerDomain = LCase(objItem.Domain)
    If objItem.PartOfDomain Then
        WScript.Echo "Computer Domain: " & strComputerDomain
    Else
        WScript.Echo "Workgroup: " & strComputerDomain
    End If
Next
server = strComputerName
domain = strComputerDomain
-----------------------------------------------------

Set objLocalGroup = GetObject("WinNT://./Administrators")
Set objADGroup1 = GetObject("WinNT://DOMAIN1/DomainGroup1")
Set objADGroup2 = GetObject("WinNT://DOMAIN1/DomainGroup2")

Set objADGroup3 = GetObject("WinNT://DOMAIN2/DomainGroup3")

objLocalGroup.Add(objADGroup1.ADsPath)
objLocalGroup.Add(objADGroup2.ADsPath)
objLocalGroup.Add(objADGroup3.ADsPath)
Set objLocalGroup = Nothing
Set objADGroup = Nothing

Open in new window

0
Comment
Question by:Flounder
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 40617880
Hi there, give this revision a try. Let me know if you have any questions.

Regards,

Rob.

Set objWMISvc = GetObject( "winmgmts:\\.\root\cimv2" )
Set colItems = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
blnInDomain = False
For Each objItem In colItems
	strComputerName = objItem.Name    
	strComputerDomain = LCase(objItem.Domain)
    If objItem.PartOfDomain Then
        blnInDomain = True
        WScript.Echo "Computer Domain: " & strComputerDomain
    Else
        WScript.Echo "Workgroup: " & strComputerDomain
    End If
Next
If blnInDomain = True Then
	Set objLocalGroup = GetObject("WinNT://./Administrators")
	If strComputerDomain = "domain1.local" Then
		Set objADGroup1 = GetObject("WinNT://" & strComputerDomain & "/DomainGroup1")
		objLocalGroup.Add(objADGroup1.ADsPath)
		Set objADGroup2 = GetObject("WinNT://" & strComputerDomain & "/DomainGroup2")
		objLocalGroup.Add(objADGroup2.ADsPath)
	ElseIf strComputerDomain = "domain2.local" Then
		Set objADGroup3 = GetObject("WinNT://" & strComputerDomain & "/DomainGroup3")
	objLocalGroup.Add(objADGroup3.ADsPath)
	End If
	Set objLocalGroup = Nothing
End If

Open in new window

0
 

Author Closing Comment

by:Flounder
ID: 40617993
Rob - works perfectly. Once again, you Rock!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40618015
No problem. Glad to help.

Rob.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now