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

Re: wmi timeout on untactable workstations by Richard

Richard
Thu Mar 01 16:13:47 CST 2007

In similar situations, I use a function to ping the computers first, before
attempting to connect with WMI. Here a several such Ping functions I have
used:

http://www.rlmueller.net/PingComputers.htm

If the machine responds to a ping, but WMI is not running, you will still
experience a timeout. Otherwise, the ping is much faster.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--

"Ian Manning" <manning.ianBOO@WHATSTHIS.ntlworld.com> wrote in message
news:I8GFh.32411$s47.14608@newsfe4-gui.ntli.net...
> 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



Re: wmi timeout on untactable workstations by Ian

Ian
Fri Mar 02 13:23:27 CST 2007



Richard Mueller [MVP] wrote:
> In similar situations, I use a function to ping the computers first, before
> attempting to connect with WMI. Here a several such Ping functions I have
> used:
>
> http://www.rlmueller.net/PingComputers.htm
>
> If the machine responds to a ping, but WMI is not running, you will still
> experience a timeout. Otherwise, the ping is much faster.
>

A very elegant solution - thanks Richard (now I feel sily for not having
thought of that!).

Regards

Ian

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