1

I've been banging my head against the wall for a couple of days now and can't work out whats wrong with my code. I'm sure its fairly simple but just can't see it. I'm trying to return a list of users who logged on to a machine and the time. Due to the environment being locked down I've had to read the registry.

Option Explicit
Const HKEY_LOCAL_MACHINE = &H80000002

Dim oNet, WMI, strComputer, tz, os, objRegistry, strKeyPath, strSubPath, strValueName, strValue, arrSubkeys, objSubKey, strSID, strUser, objReg, lngHighValue, lngLowValue, Return, strReturn, NanoSecs, DT
Set oNet = CreateObject("WScript.Network")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
strComputer = oNet.Computername

tz = 0
For Each os In GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem")
  tz = os.CurrentTimeZone
  Exit For  
Next

'Set objRegEx = CreateObject("VBScript.RegExp")
'objRegEx.Global = True   
'objRegEx.IgnoreCase = True
'objRegEx.Pattern = "default|all users|administrator|localservice|networkservice|ueit-admn-[0-9]|3rd-admn-[0-9]|systemprofile"

Set objRegistry=GetObject("winmgmts:\\" & _ 
    strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys

For Each objSubkey In arrSubkeys
    strValueName = "ProfileImagePath"
    strSID = objSubKey
    strSubPath = strKeyPath & "\" & objSubkey
    objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strSubPath,strValueName,strValue
    'strUser = Replace(strValue,"C:\Documents and Settings\","")
    'Set colMatches = objRegEx.Execute(strUser)
    'If colMatches.Count < 1 Then
    Call ProfileTime(strSID)
    'WScript.echo ProfileTime
    'End If
Next

Function ProfileTime(strSID)

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv"  )
strKeyPath = "SOFTWARE\MICROSOFT\Windows NT\CurrentVersion\ProfileList\" & strSID

strValueName = "ProfileLoadTimeHigh"
Return = objReg.GetDWORDValue(HKEY_LOCAL_MACHINE,strKeyPath,strValueName,lngHighValue)
strValueName = "ProfileLoadTimeLow"
Return = objReg.GetDWORDValue(HKEY_LOCAL_MACHINE,strKeyPath,strValueName,lngLowValue)
If typename(lngHighValue) <> "Null" then
  NanoSecs = (lngHighValue * 2 ^ 32 + lngLowValue)
  '' /* Returns time in Workstation Timezone */
  DT = #1/1/1601# + (NanoSecs / 600000000 / 1440) + (tz / 1440)
  Set ProfileTime = CDate(DT)
  End If
End Function

Runnning the above returns

profile.vbs(36, 5) Microsoft VBScript runtime error: Wrong number of arguments or invalid property assignment: 'ProfileTime'

Bit lost a the moment

1
  • Your code above doesn't have 36 lines. Please post your whole script and indicate which one is line number 36. Commented Apr 5, 2012 at 12:45

1 Answer 1

3
  1. Get rid of the on error resume next, until your bugs are resolved.
  2. Use Option Explicit and declare all your variables. You'll see that you have some scoping issues (with strReturn for example)
  3. Try to figure out how a function in VBScript returns it value. Hint, it is not Return = "my return value"
  4. A function needs to be called, so use Call ProfileTime(objsubkey). Otherwise, it can be handled as a Sub, then you ue it as ProfileTime objsubkey. Using ProfileTime(objsubkey) means "evaluate objsubkey before throwing it into the the function." Look at this blog article how it works.

These steps will make your code cleaner, giving you hints what is going wrong. You cannot create code by looking the other way when an error occurs (=On error resume next) and expecting it to work correctly. Also not respecting your workers by not putting them on the proper loan list (=option explicit, declaring and scoping) will make them go insubordinate on you.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the pointers mate. I've removed on error and defined all my variables. The value from the function should be returned as the name of the function, ProfileTime but now I get "Wrong Number of arguments or property assignment" I've tried using SET to assign the value to ProfileTime but I'm getting the same error. How I can return the value and avoid this error?
CData returns a primitive, not an object, so you don't have to Set ProfileTime, you ca njust assign it as with all other primitives.
I've tried returning the value without Set and the same error occurs
Tried your code, but it is working fine at my side (probably not resulting exactly what you want, but I am not getting any errors). It could be that you tried something like WScript.echo ProfileTime, that won't work because the function ProfileTime needs exactly one argument.
Thanks, got it to return the date and time. How can I return both the username and date. I can pass strValue to the function as well so when it spits out the date the username will come with it?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.