chemist
chemist

Reputation: 163

returning variable from functions vbs

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

Upvotes: 1

Views: 3911

Answers (1)

AutomatedChaos
AutomatedChaos

Reputation: 7490

  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.

Upvotes: 3

Related Questions