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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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