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
83 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 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

Industry Leaders: 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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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