Page 1 of 1 1
Topic Options
#148282 - 2005-09-21 09:59 PM ReadValue returns nothing
crbdp Offline
Lurker

Registered: 2005-04-12
Posts: 3
I have a inventory script and the hardware portion is running fine, but the software portion of the script will only return a value for the keyName the other values are empty. I have some error checking code in the script that returns Error code 2 the system cannot find the file specified.
Code:
 

CLS
?"Checking Hardware...."

;Declare variables and setup script enviroment
Dim $objADSystemInfo, $sUserID, $sUserPW, $User, $logUser, $Loggeduser, $luser, $ldomain
Global $SysID
$sUserID = "domain\user"
$sUserPW = "password"

;If @inwin = 1 ; NT and Later
;$logUser = SIDtoName(SubStr("%ASEHKCU%",12))
;$Loggeduser = Split($logUser,"\")
;$ldomain = $Loggeduser[0]
;$luser = $Loggeduser[1]
;EndIf

Dim $k,$x,$rc,$islocaladmin,$wmi,$name,$type,$deviceid,$card,$dhcp,$printer,
$drive,$useridentifier,$pcidentifier,$disk,$share,$rs,$rss,$command,$dsn,$connection,$boot
$x =SetOption('WrapAtEOL','On')
$rc=SetOption('Explicit','Off')
$rc=SetOption('NoVarsInStrings','On')
$rc=SetOption('CaseSensitivity','On')

$wmi = 'WINMGMTS:{IMPERSONATIONLEVEL=IMPERSONATE}!//@WKSTA'
;Setup Connection string to sql server
$dsn='Driver={SQL Server};'
$dsn=$dsn+'Server=servername;'
$dsn=$dsn+'Database=dbname;'
$dsn=$dsn+'Uid=user;'
$dsn=$dsn+'Pwd=pass'
$connection = CreateObject('ADODB.Connection')
$connection.connectionstring =$dsn
$connection.Open()
$command = CreateObject('ADODB.Command')
$command.activeconnection = $connection
$rs = CreateObject('ADODB.Recordset')
$rs.cursortype = 1
$rs.locktype = 3
$rs.activecommand = $command

;before the USERID is set - it has to be reopened and then set
$rs.Open($command)
;Identify the User
$useridentifier=$rs.fields('UserID').value
$rs.Close()

;Open the Computer Recordset
$command.commandtext="SELECT * FROM TBL_COMPUTERS WHERE WORKSTATION='"+@wksta+"';"
$rs.Open($command)
If $rs.recordcount < 1 $rs.addnew
EndIf
;Set the Data
$rs.fields('LoggedOnUser').value=$luser
$rs.fields('Workstation').value=@wksta
$rs.fields('NTDomain').value=@domain
$rs.fields('SystemManufacturer').value=WMIQuery('Manufacturer','Win32_ComputerSystem')[0]
$rs.fields('SystemModel').value=WMIQuery('Model','Win32_ComputerSystem')[0]
$rs.fields('SerialNo').value=WMIQuery('SerialNumber','Win32_BIOS')[0]
$rs.fields('OS').value=@producttype
$rs.fields('ServicePack').value=@csd
$rs.fields('PhysicalMemory').value=Val(WMIQuery('TotalPhysicalMemory','Win32_LogicalMemoryConfiguration')[0])/1024
$rs.fields('ProcessorSpeed').value=Val(WMIQuery('CurrentClockSpeed','Win32_Processor')[0])/1024
$rs.fields('PagefileSpace').value=Val(WMIQuery('TotalPageFileSpace','Win32_LogicalMemoryConfiguration')[0])/1024
$rs.fields('VidMode').value=WMIQuery('VideoModeDescription','Win32_VideoController')[0]
$rs.fields('VidCard').value=WMIQuery('Description','Win32_VideoController')[0]
$rs.fields('MACAddress').value=@address
$rs.fields('IPAddress').value=Join(Split(@ipaddress0,' '),'')
$rs.fields('LastUpdate').value=@date+' '+@time
$rs.fields('CompDesc').value=WMIQuery("description","Win32_OperatingSystem")[0]
; $rs.fields('scriptPath').value=fnGetOperationalAttribute($luser,"scriptPath")
; $rs.fields('whenCreated').value=fnGetOperationalAttribute($luser,"whenCreated")

For Each $disk in GetObject($wmi).execquery('SELECT * FROM WIN32_LOGICALDISK WHERE DRIVETYPE=3')
$rs.fields('DiskSize').value=$disk.size
$rs.fields('FreeSpace').value=$disk.freespace
Next

$rs.update

$SysID=$rs.fields('PCID').value

$rs.Close

?"Hardware check is complete"

;Update / Insert tblSoftware
?"Installed Software"
Dim $Index, $KeyName, $Application, $Version, $Publisher, $cmdtext

$cmdtext = "Select * from tblsoftware where sysid = '" + $SysID + "';"
$command.commandtext = $cmdtext
$rs.Open($command)

If $rs.eof <> -1
$rs.movefirst
While $rs.eof <> -1
$rs.delete
$rs.movenext
Loop
$rs.update
EndIf

$Index = 1
$KeyName = EnumKey("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall", $Index)
$Application = ReadValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\$KeyName", "DisplayName")
$Version = ReadValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\$KeyName", "DisplayVersion")
$Publisher = ReadValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\$KeyName", "Publisher")

While Len($KeyName) > 0



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;--------Variable C H E C K I N G-----;;;;;;;;;;;;;
?$Index "index"
?$KeyName "key"
?$Application "App"
?$Version "ver"
?$Publisher "pub"

;;;;;;;;;;;;;;;;;;;--------E R R O R C H E C K I N G-----;;;;;;;;;;;;;
If @Error <> 0
; $nul=RedirectOutput (@SCRIPTDIR+"\inventError.log",0)
? "Error for " + @WKSTA + " at " + @DATE + " is " + @SError + " at error msg is " + @Error
;? "test for " + @ADDRESS + " at " + @BUILD + " is " + @CPU + " for this user " + @COMMENT


EndIf

? ? ? "Press Any Key To Exit" Get $A
;;;;;;;;;;;;;;;;;;;--------E R R O R C H E C K I N G-----;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




If Len($Version) > 0

$rs.addnew
$rs.fields.item('Workstation').value=@wksta
$rs.fields.item("SysID").value = $SysID
$rs.fields.item("Application").value = $KeyName
$rs.fields.item("Version").value = $Version
$rs.fields.item("Publisher").value = $publisher

EndIf

$Index = 1 + $Index
$KeyName = EnumKey("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall", $Index)
$Application = ReadValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\$KeyName", "DisplayName")
$Version = ReadValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\$KeyName", "DisplayVersion")
$Publisher = ReadValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\$KeyName", "Publisher")

Loop
$rs.update
$rs.Close

?"software check is complete"

;Close all
$connection.Close()

;Flush all
$connection = 0
$rs = 0
$command = 0


Function WMIQuery($sWhat, $sFrom, Optional $sComputer, Optional $sWhere, Optional $x, Optional $root, Optional $sUserID, Optional $sUserPW)

Dim $sQuery, $objEnum, $sValue, $TMP, $SystemSet, $, $objInstance, $objLocator
$sComputer = Trim(Join(Split($sComputer,'\'),''))
If Not $sComputer Or $sComputer = @wksta
$sComputer = '.'
EndIf
If Not $root
$root = '\root\cimv2'
EndIf
$sQuery = 'Select ' + $sWhat + ' From '+ $sFrom
If $sWhere And $x
$sQuery = $sQuery+" Where "+$sWhere+" = '"+$x+"'"
EndIf
If $sUserID And $sUserPW And $sComputer <> '.'
$objLocator = CreateObject('WbemScripting.SWbemLocator')
If @ERROR Or Not $objLocator Exit Val("&"+Right(DecToHex(@ERROR),4)) EndIf
$SystemSet = $objLocator.ConnectServer($sComputer, $root, $sUserID, $sUserPW)
If @ERROR Or Not $SystemSet Exit Val("&"+Right(DecToHex(@ERROR),4)) EndIf
$SystemSet.Security_.ImpersonationLevel = 3
Else
$SystemSet = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"+$sComputer+$root)
If @ERROR Or Not $SystemSet Exit Val("&"+Right(DecToHex(@ERROR),4)) EndIf
EndIf
$objEnum = $SystemSet.ExecQuery($sQuery)
If @ERROR Or Not $objEnum Exit Val("&"+Right(DecToHex(@ERROR),4)) EndIf
For Each $objInstance in $objEnum
$=Execute(Chr(36) + 'sValue = ' + Chr(36) + 'objInstance.' + $sWhat)
If VarType($sValue) & 8192
$tmp = $tmp +'|' + Join($sValue,'|')
Else
$tmp = $tmp +'|' + $sValue
EndIf
Next
$WMIQuery = Split(SubStr($tmp,2),'|')
Exit Val("&"+Right(DecToHex(@ERROR),4))
EndFunction



Function fnGetOperationalAttribute($sNTName,$sProperty)
Dim $strDNSDomain,$objCommand,$objConnection,$objRecordSet

$strDNSDomain = " If @ERROR<0 Exit Val("&"+Right(DecToHex(@ERROR),4)) EndIf

$objCommand = CreateObject("ADODB.Command")
$objConnection = CreateObject("ADODB.Connection")
$objConnection.Provider = "ADsDSOObject"
$objConnection.Open("Active Directory Provider")
$objCommand.ActiveConnection = $objConnection
$objCommand.CommandText = $strDNSDomain + ">;(sAMAccountName=" + $sNTName + ");" +
$sProperty + ";subtree"
$objCommand.Properties("Page Size").value = 100
$objCommand.Properties("Timeout").value = 30
$objCommand.Properties("Searchscope").value = 2
$objCommand.Properties("Cache Results").value = False
$objRecordSet = $objCommand.Execute
If @ERROR<0 Exit Val("&"+Right(DecToHex(@ERROR),4)) EndIf
$fnGetOperationalAttribute = "" + $objRecordSet.Fields($sProperty)
EndFunction



Top
#148283 - 2005-09-21 10:12 PM Re: ReadValue returns nothing
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Hi crbdp,

Perhaps you could point out the exact line that is giving you a problem as I don't think anyone here is going to setup you full scenario and debug it for you in real time.

As I'm sure you're aware error 2 means the call can not locate the file in question which could be from a multitude of reasons. Path wrong due to missing or too many slashes, the actual file is missing.

What version of KiXtart are you using?

Top
#148284 - 2005-09-21 10:45 PM Re: ReadValue returns nothing
crbdp Offline
Lurker

Registered: 2005-04-12
Posts: 3
"Perhaps you could point out the exact line?"
Code:
 
$Application = ReadValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\$KeyName", "DisplayName")
$Version = ReadValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\$KeyName", "DisplayVersion")
$Publisher = ReadValue("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\$KeyName", "Publisher")


when I check the variables they are empty.
What version of KiXtart are you using? 4.21
And thanks for the quick response any suggestions would be apperciated

Top
#148285 - 2005-09-21 10:50 PM Re: ReadValue returns nothing
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Well you have
$rc=SetOption('NoVarsInStrings','On')
 
and you clearly have a var in a string for $KeyName

Maybe this would be better
 
$Application = ReadValue("HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\"+$KeyName, "DisplayName")

Top
#148286 - 2005-09-22 04:17 PM Re: ReadValue returns nothing
crbdp Offline
Lurker

Registered: 2005-04-12
Posts: 3
Great language(kixtart) Great Board(kixtart.org) Great Mods. (NTDOC) I had been looking for that for a week. I know it was a rookie mistake because I'm a rookie. Thanks to all those who helped.
Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1607 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.055 seconds in which 0.027 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org