Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: query logged on users on remote system

Reply
Thread Tools Display Modes

Re: query logged on users on remote system

 
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      10-09-2008

<> wrote in message
news:f86e43c7-9edd-4d4c-9621-...
> Hi All,
>
> I have been working on a script to output the logged on users of a
> remote system. This is the base script that I got from Microsoft
> Script Center and it works fine:
>
> strComputer = "atl-ws-o1"
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer & "\root
> \cimv2")
>
> Set colComputer = objWMIService.ExecQuery _
> ("Select * from Win32_ComputerSystem")
>
> For Each objComputer in colComputer
> Wscript.Echo "Logged-on user: " & objComputer.UserName
> Next
>
> Then I change it slightly to be able to read from a list of computer
> names that is in a separate text file. It gives an error: Line:10,
> Char:5, Error: Type mismatch
>
> Const ForReading = 1
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objTextFile = objFSO.OpenTextFile _
> ("c:\test\test2\scanlist.txt", ForReading)
>
> Do Until objTextFile.AtEndOfStream
> strNextline = objTextFile.readline
> strComputer = Split(strNextline , ",")
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer & "\root
> \cimv2")
>
> Set colComputer = objWMIService.ExecQuery _
> ("Select * from Win32_ComputerSystem")
>
> For Each objComputer in colComputer
> Wscript.Echo "Logged-on user: " & objComputer.UserName
> Next
> Loop
>
> Please let me know if you have any advise on this.
>
> Thank you.


As urkec noted, the Split function returns an array. If the file has one
NetBIOS name per line, use:

strComputer = objTextFile.ReadLine

If each line has comma delimited values, and the first value is the NetBIOS
name of the computer, use:

arrValues = Split(objTextFile.ReadLine, ",")
strComputer = arrValues(0)

If the computer NetBIOS name is the third value in the comma delimited line,
use:

arrValues = Split(objTextFile.ReadLine, ",")
strComputer = arrValues(2)

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


 
Reply With Quote
 
 
 
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      10-11-2008

<> wrote in message
news:1f548549-3ff2-4415-ac04-...
> OK, I finalized the script with a few changes copied from Microsoft
> Script Center.
>
> It now pings a range of IP's. If it receives a reply, it reads the
> logged on user information and out puts it to a file. If no reply on
> an IP, it notes that in the output file as well.
>
> Anyway here is the script for anyone who is interested.
>
> On Error Resume Next
>
> Set objExplorer = WScript.CreateObject("InternetExplorer.Application ")
> objExplorer.Navigate "about:blank"
> objExplorer.ToolBar = 0
> objExplorer.StatusBar = 0
> objExplorer.Width=400
> objExplorer.Height = 200
> objExplorer.Left = 0
> objExplorer.Top = 0
>
> Do While (objExplorer.Busy)
> Wscript.Sleep 200
> Loop
>
> objExplorer.Visible = 1
> objExplorer.Document.Body.InnerHTML = "Scanning IP Range. " _
> & "This might take several minutes to complete."
>
> Const ForAppending = 8
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objTextFile1 = objFSO.OpenTextFile _
> ("c:\test\test2\helpdesk_output2.txt", ForAppending, True)
>
> intStartingAddress = 1
> intEndingAddress = 62
> strSubnet = "10.10.3."
>
> For i = intStartingAddress to intEndingAddress
> strComputer = strSubnet & i
>
> Set objShell = CreateObject("WScript.Shell")
> strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer & ""
> Set objExecObject = objShell.Exec(strCommand)
>
> Do While Not objExecObject.StdOut.AtEndOfStream
> strText = objExecObject.StdOut.ReadAll()
> If Instr(strText, "Reply") > 0 Then
>
> '
> ================================================== ===================
> ' Insert your code here
> '
> ================================================== ===================
>
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer
> & "\root\cimv2")
>
> Set colComputer = objWMIService.ExecQuery _
> ("Select * from Win32_ComputerSystem")
>
> For Each objComputer in colComputer
> objTextFile1.WriteLine(strComputer & vbtab & "Logged-
> on user:" & vbTab & _
> objcomputer.UserName)
> Next
>
> '
> ================================================== ===================
> ' End
> '
> ================================================== ===================
>
> Else
> objTextFile1.WriteLine(strComputer & vbtab & "No scan
> Result")
> End If
> Loop
> Next
>
> objExplorer.Document.Body.InnerHTML = "Scan complete."
> Wscript.Sleep 3000
> objExplorer.Quit
>


Looks good. I would move "Set objShell" outside the For loop, so the object
is only bound once. It can be reused for each computer. I also see no need
for the "On Error Resume Next" statement (unless any computers are pre-wk2).

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: query logged on users on remote system urkec Scripting 0 10-09-2008 02:55 PM
Query AD for logged-on users? E-Double Active Directory 3 06-27-2008 08:49 PM
Users cannot be logged on to Remote Web Workspace. JMcNally Windows Small Business Server 3 03-13-2007 02:55 PM
Remote Media Center session was logged off because system validati Suzbud Windows Media Center 4 09-10-2005 04:30 AM
How do I view a list of current users that are logged into the system? Jim Windows Small Business Server 1 03-04-2004 01:57 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59