On startup of a machine record the Users & groups of the Machine.Just accounts that are manually created.

Hi,

On startup of a machine record the Users & groups of the Machine.Just accounts that are manually created.
Any user or groups that are created manually needs to be logged into a txt file in the UNC path.

Regards
Sharath
LVL 11
bsharathAsked:
Who is Participating?
 
AmazingTechConnect With a Mentor Commented:
The above code logs:

MachineName,Group or user=Name of manually added

If you want per machine log. Is duplicating the entries OK? Maybe we can log Date and Time?

Sorry it took so long. I really wanted to do it as a batch file but couldn't find an easy way to get proper user names and group names.
On Error Resume Next
UNCPath = "C:\AT"
ApprovedUsers = "Administrator,Guest,HelpAssistant,ASPNET,SUPPORT_388945a0"
ApprovedGroups = "Administrators,Users,Power Users,Backup Operators,Guests,Remote Desktop Users"
 
 
strComputer = "."
ApprovedUsers = "," & ApprovedUsers & ","
ApprovedGroups = "," & ApprovedGroups & ","
 
Set objComputer = GetObject("WinNT://" & strComputer & "")
 
 
objComputer.Filter = Array("User")
 
For Each objUser In objComputer
    If InStr(1, ApprovedUsers, "," & objUser.Name & ",", vbTextCompare) = 0 Then WriteToUNC ("LocalUser=" & objUser.Name)
Next

objComputer.Filter = Array("group")
For Each objGroup In objComputer
    If InStr(1, ApprovedGroups, "," & objGroup.Name & ",", vbTextCompare) = 0 Then WriteToUNC ("LocalGroup=" & objGroup.Name)
Next


Sub WriteToUNC(Message)
    Const ForWriting = 2
    Const ForAppending = 8
    UNCFile = UNCPath & "\" & WSHSHELL.ExpandEnvironmentStrings("%ComputerName%") & ".txt"
    Set WSHSHELL = CreateObject("Wscript.Shell")
 
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(UNCFile) Then
        Set objTextFile = objFSO.OpenTextFile(UNCFile, ForAppending)
    Else
        Set objTextFile = objFSO.CreateTextFile(UNCFile, ForWriting)
    End If
    objTextFile.Writeline  Date & "," & Time & "," & Message
    objTextFile.Close
    Set objFSO = Nothing
    Set WSHSHELL = Nothing
End Sub

Open in new window

0
 
vikas_madhusudanaCommented:
to get the users you can go to c:\documents and settings and do

dir /b


to get the localgroups you can use net command

net localgroup
0
 
AmazingTechCommented:
Are you going to setup a GPO Machine Startup script for this?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
bsharathAuthor Commented:
Hi AT yes via GPO
0
 
bsharathAuthor Commented:
Hi AT any Views...
0
 
AmazingTechCommented:
OK. Try out this .vbs script.

Modify these 3 string variables for your environment.


UNCFile = "C:\AT\ComputerUserGrouplist.txt"
ApprovedUsers = "Administrator,Guest,HelpAssistant,ASPNET,SUPPORT_388945a0"
ApprovedGroups = "Administrators,Users,Power Users,Backup Operators,Guests,Remote Desktop Users"

On Error Resume Next
UNCFile = "C:\AT\ComputerUserGrouplist.txt"
ApprovedUsers = "Administrator,Guest,HelpAssistant,ASPNET,SUPPORT_388945a0"
ApprovedGroups = "Administrators,Users,Power Users,Backup Operators,Guests,Remote Desktop Users"


strComputer = "."
ApprovedUsers = "," & ApprovedUsers & ","
ApprovedGroups = "," & ApprovedGroups & ","

Set objComputer = GetObject("WinNT://" & strComputer & "")


objComputer.Filter = Array("User")

For Each objUser In objComputer
    If InStr(1, ApprovedUsers, "," & objUser.Name & ",", vbTextCompare) = 0 Then WriteToUNC ("LocalUser=" & objUser.Name)
Next

objComputer.Filter = Array("group")
For Each objGroup In objComputer
    If InStr(1, ApprovedGroups, "," & objGroup.Name & ",", vbTextCompare) = 0 Then WriteToUNC ("LocalGroup=" & objGroup.Name)
Next


Sub WriteToUNC(Message)
    Const ForWriting = 2
    Const ForAppending = 8
    Set WSHSHELL = CreateObject("Wscript.Shell")


    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(UNCFile) Then
        Set objTextFile = objFSO.OpenTextFile(UNCFile, ForAppending)
    Else
        Set objTextFile = objFSO.CreateTextFile(UNCFile, ForWriting)
    End If
    objTextFile.Writeline WSHSHELL.ExpandEnvironmentStrings("%ComputerName%") & "," & Message
    objTextFile.Close
    Set objFSO = Nothing
    Set WSHSHELL = Nothing
End Sub

Open in new window

0
 
bsharathAuthor Commented:
Thanks  AT it works fine

Will all machines data get into 1 file?
If yes then can i get them seperate files with Machine name as the txt file name in the UNC folder
0
 
bsharathAuthor Commented:
Thank U AT....Works perfect

Can you help on this...
http://www.experts-exchange.com/Programming/Languages/Q_25081166.html
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.