?
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
Medium Priority
?
86 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

770 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