We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Add Domain administrator in each machine in the txt files group call Sophos Administrator.

Medium Priority
464 Views
Last Modified: 2012-05-06
Hi,

Add Domain administrator in each machine in the txt files group call Sophos Administrator.
Need a success & failure report
Has to skip permission issues and ping failures and WMI errors

Regards
Sharath
Comment
Watch Question

Commented:
hey change this lines :
Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading) -- >> list of machines
Set objlog = objfso.CreateTextFile("c:\log.txt", ForWriting) --- >> log file

put your domain name and the group name here
domain only the name without com or net:

strGroupName = "WinNT://fabrikam/accounting" --- >>  "WinNT://domain/Sophos Administrator"
Const ForReading = 1
Const ForWriting = 2
  
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading)
Set objlog = objfso.CreateTextFile("c:\log.txt", ForWriting)
 
strGroupName = "WinNT://fabrikam/accounting"
 
Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
If Reachable(strComputer) Then
   If per(strcomputer) then
Set objAdmins = GetObject("WinNT://" & strComputer & "/Administrators")
Set objGroup = GetObject(strGroupName)
On Error Resume Next
objAdmins.Add(objGroup.ADsPath)
If Err.Number = 0 Then
    Err.Clear
    objlog.WriteLine "group created on " & strcomputer & vbCrLf
    On Error Goto 0
    Else
    Err.Clear
    objlog.WriteLine "error creatring the group on " & strcomputer & vbCrLf
    On Error Goto 0
End if
Else
objlog.WriteLine "you don't have permission on " & strcompuer & vbcrlf
End If
Else
objlog.WriteLine strcomputer & " is not reachable ! " & vbCrLf
End if
Loop
 
Function Reachable(strComputer)
 
 strCmd = "ping -n 1 " & strComputer
 
 Set objShell = CreateObject("WScript.Shell")
 Set objExec = objShell.Exec(strCmd)
 strTemp = UCase(objExec.StdOut.ReadAll)
 
 If InStr(strTemp, "REPLY FROM") Then
 Reachable = True 
 Else
 Reachable = False
 End If
End Function
 
Function per(computer)
	strcomputer = computer
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	If err.number <> 0 Then
		err.Clear
		per = False
		On Error goto 0
	Else
		per = True
		On Error goto 0
	End If
End Function

Open in new window

Commented:
don't use it

Commented:
Sophos Administrator local or domain and to add only one user to it domain admin ?

be more clear please

Author

Commented:
Thank U
Need to add Domain\Administrator to each machines local group called Sophos Administrator.

Add User Domain\Administrator to the group

Author

Commented:
The below code works but get errors when off or when permissions has an issue. Or when the user already exists in the group...
strOutputFile = "C:\Workstations.txt"
strUser = "Administrator"
 
Const OPEN_FILE_FOR_READING = 1
Const OPEN_FILE_FOR_APPENDING = 8
 
' Set Objects
 
Set oFSO = CreateObject("Scripting.fileSystemObject")
Set objInputFile = oFSO.OpenTextFile(strOutputFile, _
  OPEN_FILE_FOR_READING)
 
inputData = Split(objInputFile.ReadAll, vbNewline)
 
For each strData In inputData
 
strComputer = strData
 
Set objGroup = GetObject("WinNT://" & strComputer & "/SophosAdministrator")
Set objUser = GetObject("WinNT://Development/" & strUser)
objGroup.Add(objUser.ADsPath)
 
Next

Open in new window

Author

Commented:
The below code works but get errors when off or when permissions has an issue. Or when the user already exists in the group...
strOutputFile = "C:\Workstations.txt"
strUser = "Administrator"
 
Const OPEN_FILE_FOR_READING = 1
Const OPEN_FILE_FOR_APPENDING = 8
 
' Set Objects
 
Set oFSO = CreateObject("Scripting.fileSystemObject")
Set objInputFile = oFSO.OpenTextFile(strOutputFile, _
  OPEN_FILE_FOR_READING)
 
inputData = Split(objInputFile.ReadAll, vbNewline)
 
For each strData In inputData
 
strComputer = strData
 
Set objGroup = GetObject("WinNT://" & strComputer & "/SophosAdministrator")
Set objUser = GetObject("WinNT://Development/" & strUser)
objGroup.Add(objUser.ADsPath)
 
Next

Open in new window

Commented:
this wil do it

change here as nedd

Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading)
Set objlog = objfso.CreateTextFile("c:\log.txt", ForWriting)

user to add here :
 
strUser = "sharath"

i notice in the script you posted you wrote the group name without space : SophosAdministrator

i wrote it with space so please fix this line if needed:
Set objGroup = GetObject("WinNT://" & strComputer & "/Sophos Administrator")
Const ForReading = 1
Const ForWriting = 2
  
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading)
Set objlog = objfso.CreateTextFile("c:\log.txt", ForWriting)
 
strUser = "sharath"
 
Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
If Reachable(strComputer) Then
   If per(strcomputer) Then
Set objGroup = GetObject("WinNT://" & strComputer & "/Sophos Administrator")
Set objUser = GetObject("WinNT://" & strComputer & "/" & strUser)
On Error Resume Next
objGroup.Add(objUser.ADsPath)
If Err.Number = 0 Then
    Err.Clear
    objlog.WriteLine "user added on " & strcomputer & vbCrLf
    On Error Goto 0
    Else
    Err.Clear
    objlog.WriteLine "error adding the user on " & strcomputer & vbCrLf
    On Error Goto 0
End if
Else
objlog.WriteLine "you don't have permission on " & strcompuer & vbcrlf
End If
Else
objlog.WriteLine strcomputer & " is not reachable ! " & vbCrLf
End if
Loop
 
Function Reachable(strComputer)
 
 strCmd = "ping -n 1 " & strComputer
 
 Set objShell = CreateObject("WScript.Shell")
 Set objExec = objShell.Exec(strCmd)
 strTemp = UCase(objExec.StdOut.ReadAll)
 
 If InStr(strTemp, "REPLY FROM") Then
 Reachable = True 
 Else
 Reachable = False
 End If
End Function
 
Function per(computer)
        strcomputer = computer
        On Error Resume Next
        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        If err.number <> 0 Then
                err.Clear
                per = False
                On Error goto 0
        Else
                per = True
                On Error goto 0
        End If
End Function

Open in new window

Author

Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add User To All Machines.vbs
Line:      15
Char:      1
Error:      The group name could not be found.
Code:      800708AC
Source:       (null)

---------------------------
OK  
---------------------------

I tried changing the group name also

Author

Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add User To All Machines.vbs
Line:      15
Char:      1
Error:      The group name could not be found.
Code:      800708AC
Source:       (null)

---------------------------
OK  
---------------------------

I tried changing the group name also

Commented:
please notice if the group name have space or not

Commented:
happens to the first machine ?

Author

Commented:
I just put 1 machine it gave this error.
The log file is empty. I tried with and without space but the group anme is without space

Author

Commented:
I just put 1 machine it gave this error.
The log file is empty. I tried with and without space but the group anme is without space
Commented:
or god i'm so sorry i did salad

a new line to change added

DomainName = "pcchief"

add only you doamin name without com or net extension
Const ForReading = 1
Const ForWriting = 2
  
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading)
Set objlog = objfso.CreateTextFile("c:\log.txt", ForWriting)
 
strUser = "script"
DomainName = "pcchief"
 
Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
If Reachable(strComputer) Then
   If per(strcomputer) Then
Set objGroup = GetObject("WinNT://" & strComputer & "/SophosAdministrator")
Set objUser = GetObject("WinNT://" & DomainName & "/" & strUser)
On Error Resume Next
objGroup.Add(objUser.ADsPath)
If Err.Number = 0 Then
    Err.Clear
    objlog.WriteLine "user added on " & strcomputer & vbCrLf
    On Error Goto 0
    Else
    Err.Clear
    objlog.WriteLine "error adding the user on " & strcomputer & vbCrLf
    On Error Goto 0
End if
Else
objlog.WriteLine "you don't have permission on " & strcompuer & vbcrlf
End If
Else
objlog.WriteLine strcomputer & " is not reachable ! " & vbCrLf
End if
Loop
 
Function Reachable(strComputer)
 
 strCmd = "ping -n 1 " & strComputer
 
 Set objShell = CreateObject("WScript.Shell")
 Set objExec = objShell.Exec(strCmd)
 strTemp = UCase(objExec.StdOut.ReadAll)
 
 If InStr(strTemp, "REPLY FROM") Then
 Reachable = True 
 Else
 Reachable = False
 End If
End Function
 
Function per(computer)
        strcomputer = computer
        On Error Resume Next
        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        If err.number <> 0 Then
                err.Clear
                per = False
                On Error goto 0
        Else
                per = True
                On Error goto 0
        End If
End Function

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Should i change
strUser = "administrator"
Development = "administrator"


I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add User To All Machines.vbs
Line:      16
Char:      1
Error:      0x80005000
Code:      80005000
Source:       (null)

---------------------------
OK  
---------------------------

Author

Commented:
Should i change
strUser = "administrator"
Development = "administrator"


I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add User To All Machines.vbs
Line:      16
Char:      1
Error:      0x80005000
Code:      80005000
Source:       (null)

---------------------------
OK  
---------------------------

Commented:
what is this Development = "administrator" ?

as i wrote

user to add can be

strUser = "administrator"

domain name
DomainName = "pcchief"

don't change nothing else i didn't use Development variable in the script


Author

Commented:
Sorry my mistake

I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add user to the administrators group.vbs
Line:      16
Char:      1
Error:      The network path was not found.
Code:      80070035
Source:       (null)

---------------------------
OK  
---------------------------

For 1 machine it worked perfect

Author

Commented:
Sorry my mistake

I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add user to the administrators group.vbs
Line:      16
Char:      1
Error:      The network path was not found.
Code:      80070035
Source:       (null)

---------------------------
OK  
---------------------------

For 1 machine it worked perfect

Commented:
what's diffrent in this machine ?

another domain ? workgroup ? non xp os ? non english os

Commented:
maybe the group not exist on this machine ?

Author

Commented:
Its a vista machine

Author

Commented:
Its a vista machine

Commented:
the user you trying to add not there or your syntax in this line not good:

DomainName = "pcchief" -- >> good

example

DomainName = "pcchief.com"  --> not good
DomainName = "pcchie "  -- > not good  forgot to write the f

and so on

Commented:
try remove the vista machines i'm checking it

Author

Commented:
Thank U worked perfect sorry for the confusion

Author

Commented:
Sorry to get back on this

For all i get this

error adding the user on BP-DCT
error adding the user on BP-DCT2

I have these 2 lines changed

strUser = "administrator"    (This being the user i want to add)
DomainName = "development"  (Domain name)

Author

Commented:
Sorry to get back on this

For all i get this

error adding the user on BP-DCT
error adding the user on BP-DCT2

I have these 2 lines changed

strUser = "administrator"    (This being the user i want to add)
DomainName = "development"  (Domain name)

Author

Commented:
Any luck on this

Author

Commented:
Just a remonder incase you missed this

Author

Commented:
This is trhe error i get

---------------------------
Windows Script Host
---------------------------
Script:      C:\Add User To All Machines.vbs
Line:      16
Char:      1
Error:      The group name could not be found.
Code:      800708AC
Source:       (null)

---------------------------
OK  
---------------------------

Can this be logged and proceeded further
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.