|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
<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 -- |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
<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 -- |
|
![]() |
| Outils de la discussion | |
|
|