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

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
LVL 11
bsharathAsked:
Who is Participating?
 
yehudahaConnect With a Mentor 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

0
 
yehudahaCommented:
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

0
 
yehudahaCommented:
don't use it
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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

be more clear please
0
 
bsharathAuthor Commented:
Thank U
Need to add Domain\Administrator to each machines local group called Sophos Administrator.

Add User Domain\Administrator to the group
0
 
bsharathAuthor 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

0
 
bsharathAuthor 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

0
 
yehudahaCommented:
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

0
 
bsharathAuthor 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
0
 
bsharathAuthor 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
0
 
yehudahaCommented:
please notice if the group name have space or not
0
 
yehudahaCommented:
happens to the first machine ?
0
 
bsharathAuthor 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
0
 
bsharathAuthor 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
0
 
bsharathAuthor 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  
---------------------------
0
 
bsharathAuthor 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  
---------------------------
0
 
yehudahaCommented:
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


0
 
bsharathAuthor 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
0
 
bsharathAuthor 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
0
 
yehudahaCommented:
what's diffrent in this machine ?

another domain ? workgroup ? non xp os ? non english os
0
 
yehudahaCommented:
maybe the group not exist on this machine ?
0
 
bsharathAuthor Commented:
Its a vista machine
0
 
bsharathAuthor Commented:
Its a vista machine
0
 
yehudahaCommented:
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
0
 
yehudahaCommented:
try remove the vista machines i'm checking it
0
 
bsharathAuthor Commented:
Thank U worked perfect sorry for the confusion
0
 
bsharathAuthor 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)
0
 
bsharathAuthor 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)
0
 
bsharathAuthor Commented:
Any luck on this
0
 
bsharathAuthor Commented:
Just a remonder incase you missed this
0
 
bsharathAuthor 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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.