Windows Server - query logged on users on remote system

Asked By devries.piete
11-Oct-08 09:10 AM
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.
NetBIOS
(1)
ObjWMIService
(1)
ObjFSO
(1)
IntStartingAddress
(1)
IntEndingAddress
(1)
MicrosoftScript
(1)
ComputerSystem
(1)
ObjExecObject
(1)
  urke replied...
09-Oct-08 10:55 AM
Split function returns an array and you are trying to use it as a string.
What does scanlist.txt look like?


--
urkec
  Richard Mueller [MVP] replied...
09-Oct-08 03:50 PM
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
--
  devries.piete replied...
11-Oct-08 09:10 AM
Hi Richard and urkec,

Thank you both for the advise. I changed the line to "strComputer =
objTextFile.ReadLine " and it reads the scanlist.txt correctly now as
it is a text file with one entry per line. I actually use IP's and not
computer names but it works fine.

The next step I have to do now is change the output of the script so
that it writes a text or csv file that writes the IP that is scanned
and the user logged into the computer on that IP.

I will be reading up on how to do this but if you could possibly point
me in the right direction,it will be greatly appreciated.

Best regards,

Pieter
  devries.piete replied...
11-Oct-08 09:10 AM
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
  Richard Mueller [MVP] replied...
11-Oct-08 08:45 AM
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
--
Create New Account
help
UNIQUE REGISTERED IS~NTSERVER. . . . <00> UNIQUE REGISTERED . . _ _MSBROWSE_ _.<01> GROUP REGISTERED ADMINISTRATOR <03> UNIQUE REGISTERED NetBios Resolution : Enabled Netbios Remote Cache Table Name Type HostAddress Life [sec] - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- kast <1C> GROUP 192.168.1.253 Type. . . . . . . . . . . : Broadkast NBT Scope ID . . . . . . . . . : Routing Enabled. . . . . . . . : No WINS Proxy Enabled . . . . . . : No DNS resolution for NETBIOS . . : No Domain membership test . . . . . . : Passed Machine is a . . . . . . . . . : Primary Domain Controller Emulator Netbios Domain name. . . . . . : kast Dns domain name. . . . . . . . : kast Dns forest name. . . . . . . . : kast Domain Guid. . . . . . . . . . : {7E4B7430-FBB4 Description: RSVP TCP Service Provider Provider Version :4 Max message size : Stream Oriented Description: MSAFD NetBIOS [ \ Device \ NetBT_Tcpip_{FBFE020A-CF2B-4029-8DF9-B23FBC024E03}] SEQPACKET 0 Provider Version :2 Description: MSAFD NetBIOS [ \ Device \ NetBT_Tcpip_{FBFE020A-CF2B-4029-8DF9-B23FBC024E03}] DATAGRAM 0 Provider Version :2 Description: MSAFD NetBIOS [ \ Device \ NetBT_Tcpip_{15AD6D2B-0830-4B38-AB81-5460ED1E22D6}] SEQPACKET 1 Provider Version :2 Description: MSAFD NetBIOS [ \ Device \ NetBT_Tcpip_{15AD6D2B-0830-4B38-AB81-5460ED1E22D6}] DATAGRAM 1 Provider Version :2 Description: MSAFD NetBIOS
DNS and Netbios name Windows Server Hello, I have an application that uses the NetBios name to make a connection. FQDN works fine but not the NetBios name. We manage 2 domains in our enviorment and the application resides in the other WINS. Is there a way to set up a host record that points to a netbios name? Thanks Windows Server DNS Discussions Windows Server 2008 (1) Exchange Server (1) DomainB.Forest If using a WINS server is out of the question, your only other choice, if NetBios name resolution is a must, is to edit the LMhosts file. There's an option name resolution fails, you can specify a WINS server to attempt name resolution using the NetBios name. Maybe that's what you're thinking? If you don't want to use kb / 294785 Deji The OP stated that he had an app that needed to resolve NetBios names, and WINS wasn't running on the network. And I suggested installing WINS or I was replying to the OP w / o starting another chain. Anything "needing" to resolve NetBIOS name will be able to do so without WINS if the search list is appropriately
path : Client for Microsoft Networks Binding Enabled: Yes Interfaces of the binding path: -Interface Name: netbios Upper Component: Client for Microsoft Networks Lower Component: WINS Client(TCP / IP) Protocol -Interface Name path : Client for Microsoft Networks Binding Enabled: No Interfaces of the binding path: -Interface Name: netbios Upper Component: Client for Microsoft Networks Lower Component: WINS Client(TCP / IP) Protocol -Interface Name path : Client for Microsoft Networks Binding Enabled: No Interfaces of the binding path: -Interface Name: netbios Upper Component: Client for Microsoft Networks Lower Component: WINS Client(TCP / IP) Protocol -Interface Name path : Client for Microsoft Networks Binding Enabled: Yes Interfaces of the binding path: -Interface Name: netbios Upper Component: Client for Microsoft Networks Lower Component: WINS Client(TCP / IP) Protocol -Interface Name Printer Sharing for Microsoft Networks Binding Enabled: Yes Interfaces of the binding path: -Interface Name: netbios Upper Component: File and Printer Sharing for Microsoft Networks Lower Component: WINS Client(TCP / IP Printer Sharing for Microsoft Networks Binding Enabled: No Interfaces of the binding path: -Interface Name: netbios Upper Component: File and Printer Sharing for Microsoft Networks Lower Component: WINS Client(TCP / IP Printer Sharing for Microsoft Networks Binding Enabled: No Interfaces of the binding path: -Interface Name: netbios Upper Component: File and Printer Sharing for Microsoft Networks Lower Component: WINS Client(TCP / IP Printer Sharing for Microsoft Networks Binding Enabled: Yes Interfaces of the binding path: -Interface Name: netbios Upper Component: File and Printer Sharing for Microsoft Networks Lower Component: WINS Client(TCP / IP
ve read name resolution should take place in this orded: 1. DNS 2. LLMNR 3. netBIOS I disabled netBIOS and IPV6 ( so that it could not resolve), flushed DNS resolver cache and tried to at the end DNS query succeeds. Windows Server DNS Discussions Windows 7 (1) DirectSMB (1) NetBIOS (1) Forwarders (1) Nameserver (1) Win2000 (1) Boyne (1) Machinename (1) Yes this is default you find it useful and shed some light. = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = DNS & WINS Resolution Process, Computer Browser Service, NetBIOS and disabling NetBIOS, and Direct Hosted SMB (DirectSMB) Keep in mind, Win2000 and newer machines uses the DNS (hostname) process FIRST before the NetBIOS resolution process. If it does not get resolved using the DNS process, then it uses theh NetBIOS process. Legacy clients use the NetBIOS process FIRST, and if it does not get resolved using NetBIOS, it uses the DNS
path : Client for Microsoft Networks Binding Enabled: Yes Interfaces of the binding path: -Interface Name: netbios Upper Component: Client for Microsoft Networks Lower Component: WINS Client(TCP / IP) Protocol -Interface Name path : Client for Microsoft Networks Binding Enabled: Yes Interfaces of the binding path: -Interface Name: netbios Upper Component: Client for Microsoft Networks Lower Component: WINS Client(TCP / IP) Protocol -Interface Name path : Client for Microsoft Networks Binding Enabled: Yes Interfaces of the binding path: -Interface Name: netbios Upper Component: Client for Microsoft Networks Lower Component: WINS Client(TCP / IP) Protocol -Interface Name Sharing for Microsoft Netwo rks Binding Enabled: Yes Interfaces of the binding path: -Interface Name: netbios Upper Component: File and Printer Sharing for Microsoft Networks Lower Component: WINS Client(TCP / IP Sharing for Microsoft Netwo rks Binding Enabled: Yes Interfaces of the binding path: -Interface Name: netbios Upper Component: File and Printer Sharing for Microsoft Networks Lower Component: WINS Client(TCP / IP Sharing for Microsoft Netwo rks Binding Enabled: Yes Interfaces of the binding path: -Interface Name: netbios Upper Component: File and Printer Sharing for Microsoft Networks Lower Component: WINS Client(TCP / IP Name: ndiswanip Upper Component: Internet Protocol (TCP / IP) Lower Component: WAN Miniport (IP) Component Name : NetBIOS Interface Bind Name: NetBIOS Binding Paths: Owner of the binding path : NetBIOS Interface Binding Enabled: Yes Interfaces of the