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

x
?
Solved

Add the currently logged in user to a group called "Sophos Administrator" in each machine in the file.

Posted on 2009-02-19
38
Medium Priority
?
1,327 Views
Last Modified: 2012-05-06
Hi,

Add the currently logged in user to a group called "Sophos Administrator" in each machine in the file.
Need a success & failure report that can show which user was added to the group.

I have the machine names in a txt file
On servers there may be multiple persons so it has to add all of them into the Sophos administrators group. And mention the same in the report.
Skip permission errors,Ping errors & WMI errors. Note them in the results file.

Regards
sharath
0
Comment
Question by:bsharath
  • 25
  • 13
38 Comments
 
LVL 14

Expert Comment

by:yehudaha
ID: 23686328
Sophos Administrator local group or domain group ?
0
 
LVL 11

Author Comment

by:bsharath
ID: 23688416
Sophos Administrator is a local Group.
This group is available in each machine
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23689440
you want to add the user logged on to the remote machine or the one running the script ?

i think you ment the one running the script how you can guess who is in the remote machine ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Author Comment

by:bsharath
ID: 23689678
The user logged into the machine.
Need to check for the currently logged in user and add him to the group.

Or.

I can provide the names in a file like
Machinename;NTlogin
If the ntlogin can be added to the group that would be great
0
 
LVL 11

Author Comment

by:bsharath
ID: 23689679
The user logged into the machine.
Need to check for the currently logged in user and add him to the group.

Or.

I can provide the names in a file like
Machinename;NTlogin
If the ntlogin can be added to the group that would be great
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23689810
the script is very similir to another post we have add domain admin to the group

i changed it a little bit

change as need:

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

the list file will be line by line :

Machinename;NTlogin
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)
 
Do Until objlist.AtEndOfStream
strLine = split(objlist.ReadLine,";")
strComputer = strLine(0)
strUser = strLine(1)
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
 
LVL 11

Author Comment

by:bsharath
ID: 23691442
All the nt logins are domain logins.

When added it just shows the name
not
Domainname\username in the group
0
 
LVL 11

Author Comment

by:bsharath
ID: 23691443
All the nt logins are domain logins.

When added it just shows the name
not
Domainname\username in the group
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23691713
a new line addes please chnage to your domain name without com or net:

StrDomain = "pcchief"
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)
 
StrDomain = "pcchief"
 
Do Until objlist.AtEndOfStream
strLine = split(objlist.ReadLine,";")
strComputer = strLine(0)
strUser = strLine(1)
If Reachable(strComputer) Then
   If per(strcomputer) Then
Set objGroup = GetObject("WinNT://" & strComputer & "/SophosAdministrator")
Set objUser = GetObject("WinNT://" & StrDomain & "/" & 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
 
LVL 11

Author Comment

by:bsharath
ID: 23692107
Please forgive

The error i posted in the other post was from this post..

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  
---------------------------
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23692184
vista ?

domain name syntax error ?
0
 
LVL 11

Author Comment

by:bsharath
ID: 23692345
45 machines went on fine then get this error
This is an XP machine. Syntax is right. Remove the name and have run the script again...Shall let you know what happens
0
 
LVL 11

Author Comment

by:bsharath
ID: 23692346
45 machines went on fine then get this error
This is an XP machine. Syntax is right. Remove the name and have run the script again...Shall let you know what happens
0
 
LVL 11

Author Comment

by:bsharath
ID: 23692443
I think there may be machines that may not have this group at all. Can we skip such cases and log this error as group not found
0
 
LVL 11

Author Comment

by:bsharath
ID: 23692444
I think there may be machines that may not have this group at all. Can we skip such cases and log this error as group not found
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23700503
try this
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)
 
StrDomain = "pcchief"
 
Do Until objlist.AtEndOfStream
	strLine = Split(objlist.ReadLine, ";")
	strComputer = strLine(0)
	strUser = strLine(1)
	If Reachable(strComputer) Then
		If per(strcomputer) Then
			On Error Resume Next
			Set objGroup = GetObject("WinNT://" & strComputer & "/SophosAdministrator")
			If Err.Number = 0 Then
				Err.Clear
				Set objUser = GetObject("WinNT://" & StrDomain & "/" & strUser)
				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
				Err.Clear
				objlog.WriteLine "didn't find 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
 
LVL 11

Author Comment

by:bsharath
ID: 23700578
I get this for machines that are on too
error adding the user on Dev-ch
0
 
LVL 11

Author Comment

by:bsharath
ID: 23700579
I get this for machines that are on too
error adding the user on Dev-ch
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23704179
the user is there ? the group there ?
0
 
LVL 11

Author Comment

by:bsharath
ID: 23704281
Yes user and group are there. if they are not there in some case can they be moved to the next machine...
Skipping them and mentioning that in the log
0
 
LVL 11

Author Comment

by:bsharath
ID: 23704282
Yes user and group are there. if they are not there in some case can they be moved to the next machine...
Skipping them and mentioning that in the log
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23704291
the script don't Skipping it ?
0
 
LVL 11

Author Comment

by:bsharath
ID: 23704296
It skips but even for machines that have the user and group.

0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23704301
i'm loosing you if the user and the group is there so all is good
0
 
LVL 11

Author Comment

by:bsharath
ID: 23704348
Here is the actuals

The txt file is as
Machinename;Username
I have changed the script with just this part
StrDomain = "development"
development is my domain name
Even when the machine is on and the user is live i get as
error adding the user on Dev-ch
dev-ch = my machinename in the txt file.

The machine is on there are no permission issues and i get the error in the log file. Its not for all machines but just a few.
0
 
LVL 11

Author Comment

by:bsharath
ID: 23704349
Here is the actuals

The txt file is as
Machinename;Username
I have changed the script with just this part
StrDomain = "development"
development is my domain name
Even when the machine is on and the user is live i get as
error adding the user on Dev-ch
dev-ch = my machinename in the txt file.

The machine is on there are no permission issues and i get the error in the log file. Its not for all machines but just a few.
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23708849
only option as i see is the user is in the group all ready

becasue it's just a few mahine it's very hard to say.

if the script didn't work at all of curse i could something

more i don't know what diffrent in the machine you get the error (network inftrucsure)
0
 
LVL 11

Author Comment

by:bsharath
ID: 23708864
Can we skip

Non English
User already exist
Group does not exist
Permission error
Ping error
WMI error

So we can skip them from not stopping the script running
0
 
LVL 11

Author Comment

by:bsharath
ID: 23708865
Can we skip

Non English
User already exist
Group does not exist
Permission error
Ping error
WMI error

So we can skip them from not stopping the script running
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23708891
what do you mean ?

SophosAdministrator exist in another language ?



the script will report all what listed here :

User already exist
objlog.WriteLine "error adding the user on " & strcomputer & vbCrLf

Group does not exist
objlog.WriteLine "didn't find the group on " & strcomputer & vbCrLf

Permission error
objlog.WriteLine "you don't have permission on " & strcompuer & vbcrlf

Ping error
objlog.WriteLine strcomputer & " is not reachable ! " & vbCrLf

WMI error
the same as permission
0
 
LVL 11

Author Comment

by:bsharath
ID: 23708944
Yes we have other languages too if other language skip
0
 
LVL 11

Author Comment

by:bsharath
ID: 23708945
Yes we have other languages too if other language skip
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23709039
try this :
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)
 
StrDomain = "pcchief"
 
Do Until objlist.AtEndOfStream
        strLine = Split(objlist.ReadLine, ";")
        strComputer = strLine(0)
        strUser = strLine(1)
        If Reachable(strComputer) Then
                If per(strcomputer) Then
                  If OSlang(strcomputer) <> 1033 then
                        On Error Resume Next
                        Set objGroup = GetObject("WinNT://" & strComputer & "/SophosAdministrator")
                        If Err.Number = 0 Then
                                Err.Clear
                                Set objUser = GetObject("WinNT://" & StrDomain & "/" & strUser)
                                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
                                Err.Clear
                                objlog.WriteLine "didn't find the group on " & strcomputer & vbCrLf
                                On Error Goto 0
                        End If
                        Else
                        objlog.WriteLine "non english os on " & strcomputer
                        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
 
Function OSlang(strComputer) 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
 
For Each objItem in colItems
	OSlang = objItem.OSLanguage
Next
End Function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 23709140
I get 100's of machines as
non english os on
But i have just few say 50 + but i get 200+
0
 
LVL 11

Author Comment

by:bsharath
ID: 23709141
I get 100's of machines as
non english os on
But i have just few say 50 + but i get 200+
0
 
LVL 14

Accepted Solution

by:
yehudaha earned 2000 total points
ID: 23709150
try this:
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)
 
StrDomain = "pcchief"
 
Do Until objlist.AtEndOfStream
        strLine = Split(objlist.ReadLine, ";")
        strComputer = strLine(0)
        strUser = strLine(1)
        If Reachable(strComputer) Then
                If per(strcomputer) Then
                  If OSlang(strcomputer) = "1033" then
                        On Error Resume Next
                        Set objGroup = GetObject("WinNT://" & strComputer & "/SophosAdministrator")
                        If Err.Number = 0 Then
                                Err.Clear
                                Set objUser = GetObject("WinNT://" & StrDomain & "/" & strUser)
                                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
                                Err.Clear
                                objlog.WriteLine "didn't find the group on " & strcomputer & vbCrLf
                                On Error Goto 0
                        End If
                        Else
                        objlog.WriteLine "non english os on " & strcomputer
                        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
 
Function OSlang(strComputer) 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
 
For Each objItem in colItems
	OSlang = objItem.OSLanguage
Next
End Function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 23713940
Thanks a lot worked fine... :-))
0
 
LVL 11

Author Comment

by:bsharath
ID: 24243415
Hi yehudaha
Long time no see... How are you...

Can you have a look at my posts.... When time permits
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

581 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