PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.win.server.scripting > query logged on users on remote system
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
query logged on users on remote system

Réponse
 
LinkBack Outils de la discussion
Vieux 09/10/2008, 14h06   #1
devries.pieter@googlemail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut query logged on users on remote system

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.
  Réponse avec citation
Vieux 09/10/2008, 16h55   #2
urkec
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: query logged on users on remote system

"devries.pieter@googlemail.com" wrote:

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



Split function returns an array and you are trying to use it as a string.
What does scanlist.txt look like?


--
urkec
  Réponse avec citation
Vieux 09/10/2008, 21h50   #3
Richard Mueller [MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: query logged on users on remote system


<devries.pieter@googlemail.com> wrote in message
news:f86e43c7-9edd-4d4c-9621-3f4a717e1f92@a19g2000pra.googlegroups.com...
> 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
--


  Réponse avec citation
Vieux 11/10/2008, 07h08   #4
devries.pieter@googlemail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: query logged on users on remote system

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
  Réponse avec citation
Vieux 11/10/2008, 11h25   #5
devries.pieter@googlemail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: query logged on users on remote system

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

  Réponse avec citation
Vieux 11/10/2008, 14h45   #6
Richard Mueller [MVP]
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: query logged on users on remote system


<devries.pieter@googlemail.com> wrote in message
news:1f548549-3ff2-4415-ac04-50b0aa345db8@u46g2000hsc.googlegroups.com...
> 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\desk_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
--


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h51.


Édité par : vBulletin® version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,61349 seconds with 14 queries