Solved

VBS script - help needed

Posted on 2006-07-20
11
834 Views
Last Modified: 2008-01-09
Hi,

Bit of a noob when it comes to this I'm afraid :-(

I have a script that pics up computer names (that are on the network) from a file (comps.txt) and creates another file with information (results.txt) about the PC's in the list.

What I want to end up with is a file with the computer name, followed by the serial number of the PC and the username of the person who's PC it is.  I have found a script that will do most of this but I cannot integrate the code for finding the user name.

The script is as follows:

----------------------------------------------------------------------------------------------------------------

'START CODE
const FSO_READ_MODE = 1
const FSO_WRITE_MODE = 2
const COMPUTER_NAMES_FILE = "D:\comps.txt"
const RESULTS_FILE = "D:\results.txt" 'warning - will be overwritten each time script runs

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")

ReadFile(COMPUTER_NAMES_FILE)
Set fso = Nothing

function CheckSerial(ComputerName)
     Dim System
     Dim retVal
     retVal=""
     if Wscript.Arguments.Count > 0 then
          sSystem=Wscript.Arguments(0)
     end if
     winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//"& ComputerName &""
     Set SNSet = GetObject( winmgmt1 ).InstancesOf ("Win32_BIOS")
     for each SN in SNSet
          retVal = retVal & "The serial number for " & ComputerName & " is: " & SN.SerialNumber
     Next
     CheckSerial = retVal
end function

function ReadFile(sFilePath)
     If fso.FileExists(sFilePath) then
          'The file exists, so open it and output its contents
          dim file: set file = fso.OpenTextFile(sFilePath, FSO_READ_MODE)
          dim sLine
          dim sResults: sResults = ""
          Do While file.AtEndOfStream = False
              sLine = file.ReadLine
              sResults = sResults & CheckSerial(sLine) & vbcrlf
          Loop
          file.Close()
          Set file = Nothing
          WriteResults sResults
     Else
          'The file did not exist
          msgbox sFilePath & " was not found."
     End If
end function

sub WriteResults(sResults)
     dim file: set file = fso.OpenTextFile(RESULTS_FILE, FSO_WRITE_MODE, true)
     file.write(sResults)
     file.Close()
     Set file = Nothing
end sub
'END CODE

----------------------------------------------------------------------------------------------------------------

If anyone could shed any light on how I can get the user name into the text file as well I would be very greatful.

Thanks,

Giles
0
Comment
Question by:Gilesmp
11 Comments
 
LVL 7

Expert Comment

by:yotamsher
ID: 17149145
what do you mean the user of this computer
currently logged on?
list of all users?

Yotam
0
 
LVL 5

Expert Comment

by:Dbergert
ID: 17149413
0
 
LVL 5

Expert Comment

by:Isaac
ID: 17149416
Do you have an example of what the contents of the file "comps.txt" looks like?
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17149422
That just gets the currently logged on user ( how do you know if that user is the person who owns the pc or not ? ) I mean you can have more then one user per pc and just because you get a username at the time you run the script, doesnt mean to say that the person who is logged on there and then owns it, unless I am missing something.

You can enumerate all the user accounts on each machine but then how do you know who owns the machine ?
0
 
LVL 7

Expert Comment

by:yotamsher
ID: 17149439
'the following will give you all the users of the machine

'START CODE
const FSO_READ_MODE = 1
const FSO_WRITE_MODE = 2
const COMPUTER_NAMES_FILE = "c:\comps.txt"
const RESULTS_FILE = "c:\results.txt" 'warning - will be overwritten each time script runs

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")

ReadFile(COMPUTER_NAMES_FILE)
Set fso = Nothing

function CheckSerial(ComputerName)
     Dim System
     Dim retVal
     retVal=""
     if Wscript.Arguments.Count > 0 then
          sSystem=Wscript.Arguments(0)
     end if
     winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//"& ComputerName &""
     Set SNSet = GetObject( winmgmt1 ).InstancesOf ("Win32_BIOS")
     for each SN in SNSet
          retVal = retVal & "The serial number for " & ComputerName & " is: " & SN.SerialNumber
     Next
     CheckSerial = retVal
end function

function CheckUsers(ComputerName)
     Dim retVal
     retVal=""
     if Wscript.Arguments.Count > 0 then
          sSystem=Wscript.Arguments(0)
     end if
      Set objWMIService = GetObject("winmgmts:\\" & ComputerName & "\root\cimv2")
      Set colItems = objWMIService.ExecQuery("Select * from Win32_Account",,48)
      retVal = retVal & "The users of " & ComputerName & " are: "
      For Each objItem in colItems
            if objItem.SIDType = 1 then
                retVal = retVal & " , "& objItem.Name
            end if
      Next
      CheckSerial = retVal
end function

function ReadFile(sFilePath)
     If fso.FileExists(sFilePath) then
          'The file exists, so open it and output its contents
          dim file: set file = fso.OpenTextFile(sFilePath, FSO_READ_MODE)
          dim sLine
          dim sResults: sResults = ""
          Do While file.AtEndOfStream = False
              sLine = file.ReadLine
              sResults = sResults & CheckSerial(sLine) & CheckUsers(sLine) & vbcrlf
          Loop
          file.Close()
          Set file = Nothing
          WriteResults sResults
     Else
          'The file did not exist
          msgbox sFilePath & " was not found."
     End If
end function

sub WriteResults(sResults)
     dim file: set file = fso.OpenTextFile(RESULTS_FILE, FSO_WRITE_MODE, true)
     file.write(sResults)
     file.Close()
     Set file = Nothing
end sub
'END CODE
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Expert Comment

by:yotamsher
ID: 17149456
if you want to know who "owns" the machine, I guess you will have to create some methodology of your own:
something like a file called owner.txt which resides in some directory.

good luck Yotam
0
 
LVL 3

Author Comment

by:Gilesmp
ID: 17152336
Yes, the username of the user currently logged on ... am I correct in saying this is what this script would give you?

------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colCSItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objCSItem In colCSItems
  WScript.Echo "User Name: " & objCSItem.UserName
Next
------------------------

The comps.txt file is just the name of each PC per line i.e.

computername1
computername2
computername3
etc.


------------------------

Thanks Yotam,

I tried that script, however, it came back with error:

Line: 41
Char: 6
Error: Illegal assignment: 'CheckSerial'
Code: 800A01F5
Source: Microsoft VBScript runtime error

Any ideas?

Thanks for all your help so far guys,

Giles
0
 
LVL 7

Expert Comment

by:yotamsher
ID: 17152387
sorry my mistake
I had moved this between vbscript and jscript, and then mad a mistake in the returning from the function      
line 41 which now shows:
CheckSerial = retVal
should be:
CheckUsers = retVal

Yotam

but I see that you want the currently logged on user, I'll post it soon
0
 
LVL 3

Author Comment

by:Gilesmp
ID: 17153232
Great, that ammendment has made the script run fine now.

I eagerly await your next post :-)

Many thanks,

Giles
0
 
LVL 7

Accepted Solution

by:
yotamsher earned 500 total points
ID: 17156455
'I hope this is what you need:
 
'Yotam
'START CODE
const FSO_READ_MODE = 1
const FSO_WRITE_MODE = 2
const COMPUTER_NAMES_FILE = "c:\comps.txt"
const RESULTS_FILE = "c:\results.txt" 'warning - will be overwritten each time script runs

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")

ReadFile(COMPUTER_NAMES_FILE)
Set fso = Nothing

function CheckSerial(ComputerName)
     Dim System
     Dim retVal
     retVal=""
     if Wscript.Arguments.Count > 0 then
          sSystem=Wscript.Arguments(0)
     end if
   
     winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//"& ComputerName &""

     Set SNSet = GetObject( winmgmt1 ).InstancesOf ("Win32_BIOS")
     for each SN in SNSet
          retVal = retVal & "The serial number for " & ComputerName & " is: " & SN.SerialNumber
     Next
     CheckSerial = retVal
end function

function CheckLoggedOn(ComputerName)
     Dim retVal

     retVal=""
     if Wscript.Arguments.Count > 0 then
          sSystem=Wscript.Arguments(0)
     end if
     winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//"& ComputerName &"\root\cimv2"
     Set objWMIService = GetObject(winmgmt1)

     Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
    retVal = retVal & "Loggedon users of " & ComputerName & " are: "
    For Each objComputer in colComputer
       retVal = retVal &  CRLF & objComputer.UserName
    Next

    CheckLoggedOn = retVal
end function


function ReadFile(sFilePath)
     If fso.FileExists(sFilePath) then
          'The file exists, so open it and output its contents
          dim file: set file = fso.OpenTextFile(sFilePath, FSO_READ_MODE)
          dim sLine
          dim sResults: sResults = ""
          Do While file.AtEndOfStream = False
              sLine = file.ReadLine
              sResults = sResults & CheckSerial(sLine) & vbcrlf & CheckLoggedOn(sLine) & vbcrlf
          Loop
          file.Close()
          Set file = Nothing
          WriteResults sResults
     Else
          'The file did not exist
          msgbox sFilePath & " was not found."
     End If
end function

sub WriteResults(sResults)
     dim file: set file = fso.OpenTextFile(RESULTS_FILE, FSO_WRITE_MODE, true)
     file.write(sResults)
     file.Close()
     Set file = Nothing
end sub
'END CODE
0
 
LVL 3

Author Comment

by:Gilesmp
ID: 17166017
Brilliant - that works a charm

Thank you very much

Giles
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is Python programming? 3 103
how to split this type of line? 5 84
tvirtualstringtree freeze when load too manny images 10 52
Import filename in batch 2 24
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now