x11tech
Programmer
- Dec 26, 2007
- 190
Starting with
I modified the script as follows:
I scheduled the second script to run every 2 minutes on a Windows 2003 server. The script successfully retrieves username information when the account running the script is the same as the account logged in to the queried server.
When the user running the script and the user logged in to the queried server differ, the script returns that no one is logged in. (This is in error.)
I experience the same problems regardless of the computer that I run the script from. (I've also tried running it from a Windows XP SP2 workstation.)
The user accounts being used to test this all have Domain Admins membership.
This appears to be some kind of user rights issue. Any assistance would be appreciated.
Code:
' WhoLogonInput.vbs
' Sample VBScript to discover which user is logged on
' Author Guy Thomas [URL unfurl="true"]http://computerperformance.co.uk/[/URL]
' Version 2.4 - December 2005
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objComputer, colComputer
Dim strLogonUser, strLogonUser1, strComputer
strComputer = "."
strComputer = InputBox("Enter Computer name", "Find Logon User", strComputer)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
'Note: if objComputer.UserName is null, this generates a
'script error. objComputer.UserName is null when no one is logged in.
strLogonUser = Split(objComputer.UserName,"\")
strLogonUser(1) = UCase(Left(strLogonUser(1),1)) & Trim(Mid(strLogonUser(1),2,20))
Wscript.Echo strLogonUser(1) & " is logged on at " & strComputer
Next
' End of Sample Logged on VBScript
I modified the script as follows:
Code:
Option Explicit
Dim oWMIService, oComputer, colComputer
Dim oFSO, oFileInfo
Dim sLogonUser, sComputer, sTime
Dim sInfoFile ', sHtmlFile
Dim sOldLogonUser, sOldTime
' Dim sLogonUser1
' Dim oFileHtml
const ForReading = 1
const ForWriting = 2
const ForAppending = 8
sComputer = "mycomputer"
'sHtmlFile = "c:\inetpub\[URL unfurl="true"]wwwroot\operations_ts1.asp"[/URL]
sInfoFile = "c:\inetpub\[URL unfurl="true"]wwwroot\mycomputer_status.txt"[/URL]
set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &_
sComputer & "\root\cimv2")
Set colComputer = oWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each oComputer in colComputer
if not isnull(oComputer.UserName) then
wscript.echo oComputer.UserName
sLogonUser = Split(oComputer.UserName,"\")
wscript.echo sLogonUser
sLogonUser(1) = UCase(Left(sLogonUser(1),1)) & Trim(Mid(sLogonUser(1),2,20))
'Wscript.Echo sLogonUser(1) & " is logged on at " & sComputer
if oFSO.FileExists(sInfoFile) then
set oFileInfo = oFSO.OpenTextFile(sInfoFile, ForReading)
sOldLogonUser = oFileInfo.ReadLine
sOldTime = oFileInfo.ReadLine
set oFileInfo = nothing
else
sOldLogonUser = ""
sOldTime = ""
end if
sTime = Now
if sOldLogonUser <> sLogonUser(1) then
if oFSO.FileExists(sInfoFile) then
set oFileInfo = oFSO.GetFile(sInfoFile)
oFileInfo.Delete
set oFileInfo = nothing
end if
set oFileInfo = oFSO.OpenTextFile(sInfoFile, ForWriting, True)
oFileInfo.WriteLine(sLogonUser(1))
oFileInfo.WriteLine(sTime)
set oFileInfo = nothing
'Wscript.Echo sOldLogonUser & " <> " & sLogonUser(1)
end if
else
'wscript.echo "No one is logged in at " & sComputer
sTime = Now
if oFSO.FileExists(sInfoFile) then
set oFileInfo = oFSO.OpenTextFile(sInfoFile, ForReading)
sOldLogonUser = oFileInfo.ReadLine
sOldTime = oFileInfo.ReadLine
set oFileInfo = nothing
set oFileInfo = oFSO.GetFile(sInfoFile)
oFileInfo.Delete
set oFileInfo = nothing
end if
set oFileInfo = oFSO.OpenTextFile(sInfoFile, ForWriting, True)
oFileInfo.WriteLine("No one")
if sOldLogonUser = "No one" then
oFileInfo.WriteLine(sOldTime)
else
oFileInfo.WriteLine(sTime)
end if
set oFileInfo = nothing
end if
Next
' End of Sample Logged on VBScript
I scheduled the second script to run every 2 minutes on a Windows 2003 server. The script successfully retrieves username information when the account running the script is the same as the account logged in to the queried server.
When the user running the script and the user logged in to the queried server differ, the script returns that no one is logged in. (This is in error.)
I experience the same problems regardless of the computer that I run the script from. (I've also tried running it from a Windows XP SP2 workstation.)
The user accounts being used to test this all have Domain Admins membership.
This appears to be some kind of user rights issue. Any assistance would be appreciated.