Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

Add Domain Group to Local Server Group, need help with script - RobSampson

I found a script on EE from RobSampson that is listed below that I am able to use but have a few questions.

The script is working as far as adding the group to the power users local group. In the code below the group is paul.

But when I run the script I get the error below and no results file is created.

Line 31: Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)

Is there a way to get it to show results so I know if the group was added to all the servers in the list? I need to run it remotely on 187 servers. Is there a way to specify a different domain than the one you are logged in with? Like Domain2\paul instead of paul (domain1\paul).

The servers are in several different domains but the account I am using has access to all of them.

strInputFile = "Computers.txt"
strOuputFile = "Results.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objNetwork = CreateObject("WScript.Network")
Set objUser = GetObject("WinNT://" & objNetwork.UserDomain & "/paul")
strResults = "Adding " & objNetwork.UserDomain & "/paul to local Power Users group"
strResults = strResults & VbCrLf & "========================================================================"
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
While Not objInputFile.AtEndOfStream
	strComputer = objInputFile.ReadLine
	If Ping(strComputer) = True Then
		Set objGroup = GetObject("WinNT://" & strComputer & "/Power Users")
		On Error Resume Next
		objGroup.Add objUser.ADsPath
		If Err.Number = 0 Then
			strResults = strResults & VbCrLf & strComputer & ": Successfully added"
			strResults = strResults & VbCrLf & strComputer & ": Failed to add: " & Err.Number & " - " & Err.Description
		End If
		On Error GoTo 0
		Set objGroup = Nothing
		strResults = strResults & VbCrLf & strComputer & ": Unable to ping"
	End If
Set objInputFile = Nothing
Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.Write strResults
Set objOutputFile = Nothing
Set objFSO = Nothing
Set objNetwork = Nothing
MsgBox "Finished. Please see " & strOutputFile
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
		Ping = False
	End If
End Function                      

Open in new window

1 Solution
Maybe you get an error because you spelled wrong variable name:
strOuputFile = "Results.txt"

Open in new window

I guess it should be:
strOutputFile = "Results.txt"

Open in new window

After running this script you can see results in Results.txt file where this was successfully.
For other domain try to change this line:
Set objUser = GetObject("WinNT://" & objNetwork.UserDomain & "/paul")

Open in new window

Set objUser = GetObject("WinNT://domain2/paul")

Open in new window

REIUSAAuthor Commented:
Sweet, thanks. It works great now.

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now