Hiya,
I've created the script below, which queries each workstation in a input
text file (pclist.txt) then returns the logged on user to a second text
file (users.txt).
It works fine, but the bit I'd like some advice on is that uncontactable
(ie unpingable, not on the domain anymore etc) pcs cause the script to
pause for a long time each time, despite my error checking. Anyone got
any ideas? WOuld be greatly appreciated!
Script:
'==============
' Script to tell who is logged into a group of pcs listed in a text file
' This is then outputted to a second text file
' Ian Manning 13/02/07
'========================================
'
Dim strComputer
subCreateResultsFile "Z:\My Documents\Projects\Scripts\misc\names.txt"
'strComputer = InputBox("Please enter the name of the computer:")
'MsgBox funGetLoggedInUser(strComputer)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objPCListFile = objFSO.OpenTextFile("Z:\My
Documents\Projects\Scripts\misc\pclist.txt",1)
Do While objPCListFile.AtEndOfStream <> TRUE
strComputer = funGetLoggedInUser(objPCListFile.ReadLine)
subWriteUsers "Z:\My
Documents\Projects\Scripts\misc\names.txt",strComputer
Loop
MsgBox "-----> DONE <-----"
Function funGetLoggedInUser(strFComputer)
Dim objWMI, colOS, strUsers, objItem
Dim strNoValue,strBoxTitle,strNoPCMsg
' Enable error handling to check if the pc is switched off or otherwise
uncontactable
On Error Resume Next
set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!//" &
strFComputer & "")
If Err.Number = 462 Then
strUsers = ""
Else
set colOS = objWMI.ExecQuery("Select * from Win32_ComputerSystem")
For Each objItem In colOS
strUsers = objItem.UserName
Next
End If
On Error Goto 0
' Get the username of the currently logged in user if there is one
funGetLoggedInUser = strUsers
End Function
Sub subCreateResultsFile(strFilePath)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objCreateFile = objFSO.CreateTextFile(strFilePath,TRUE)
End Sub
Sub subWriteUsers(strFilePath,strUser)
conForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFilePath,conForAppending,FALSE,0)
' nb the "& "" " is needed as wscript errors if you try and pass it a
blank variable
' if loop only writes a line when there is a user logged in (so you
don't get massive gaps in your results file
If strUser <> "" Then
With objFile
.WriteLine strUser & ""
End With
Else
End If
End Sub
--
Ian "tutenkamu" Manning
"The greatest trick the Devil ever pulled was convincing the world he
didn't exist."
Kevin Spacey (Verbal Kint); The Usual Suspects