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
77 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
Comment Utility
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
Comment Utility
Rob - works perfectly. Once again, you Rock!
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
No problem. Glad to help.

Rob.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

10 Experts available now in Live!

Get 1:1 Help Now