Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

user connect in a PC on Domain

Status
Not open for further replies.

guif

Programmer
Sep 6, 2007
27
ES
Hi!
with this script, i know the user corrently logged in my computer:

strComputer = inputbox("Dime el PC y te dire el usuario:")
If strComputer = "" then WScript.Quit
set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & strComputer & "")
set colOS = objWMI.ExecQuery("Select * from Win32_ComputerSystem")

For Each objItem In colOS
if strUsers <> "" then
strUsers = strUsers & ", " & objItem.UserName
else
strUsers = objItem.UserName
End If
Next
wscript.echo "The following user(s) are logged on to " & strComputer & ":" & strUsers

...but i would like to know the user connect in a PC on my domain. My domain is named SERVER_1.

Help me please!!!!
 
Just put the Domain name in the place of your computer name and you're good to go. The neat thing is that if the code runs on the local machine, the domain switches to the name of the machine automatically. Happy coding.
 
in my code, where can I change it?
 
Code:
On error resume next
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("C:\workstationlist.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
arrComputers = Split(strText, VbCrLf)

For Each strComputer In arrComputers
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
    For Each objItem in colItems
        Wscript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
    Next
    Set objItem = Nothing: Set colItems = Nothing: Set objWMIService = Nothing
Next
 
Let's see if I'm understanding you. You want to know what users are connected your computer correct?
 
Got code for you. Apparently there are two approaches. One requires a field trip to your registry (Nasty I know). The other uses the approach I have written below. The rub..... it only works on Windows XP and up (2003,2008 Server and Vista).

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ServerConnection",,48)
For Each objItem in colItems
Wscript.Echo "ActiveTime: " & objItem.ActiveTime
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Status: " & objItem.Status
Wscript.Echo "UserName: " & objItem.UserName
Wscript.Echo "ComputerName: " & objItem.ComputerName
Wscript.Echo "ShareName: " & objItem.ShareName
Next
 
The script I posted will go through a list of computers you provide in the text file, and will tell you who is logged on to each machine in the list.
Is that not what you were looking for.
 
but your code is not running...
 
i would like that my script says the user connect in this moment in a PC.
My script answer what's the PC with an inputbox
 
Change this line "C:\workstationlist.txt" to where you have your file list of computers.
They should be as follows:
Computer1[Name of your computer]
Computer2
Computer3
Because the array is split with VBCRlf(Visual Basic Carriage Return Line Feed, good old type writer)
arrComputers = Split(strText, VbCrLf)
 
And I would like a Ferrari

Code:
On error resume next
Set objFSO = CreateObject("Scripting.FileSystemObject")

strComputer = inputbox("Dime el PC y te dire el usuario:")

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
    For Each objItem in colItems
        Wscript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
    Next
    Set objItem = Nothing: Set colItems = Nothing: Set objWMIService = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top