Saj
(Fresh Scripter)
2009-02-24 02:45 PM
Hardware inventory

Hi guys,

I am after a script which tells me the following on each users system :-

- logged on users
- make/model/serial number of users system

I also want this info to be written to a file. Please assist.

Many thanks

Saj.


LonkeroAdministrator
(KiX Master Guru)
2009-02-24 02:59 PM
Re: Hardware inventory

search the forums.
lots of examples.
specially the compinfo scripts.


Glenn BarnasAdministrator
(KiX Supporter)
2009-02-24 03:17 PM
Re: Hardware inventory

The WMISysInfo() UDF gathers a fair amount of info from a local or remote system. It returns an array, which, combined with the CSV() UDF can create a file ready to import into Excel or a database.

I think its posted here, but the latest versions are always on my web site in the Resources section.

Glenn


Saj
(Fresh Scripter)
2009-02-24 04:15 PM
Re: Hardware inventory

I am new kixtart, have got a copy of your WMISysInfo script. Do I need to put in a host name anywhere to run this script?

Saj
(Fresh Scripter)
2009-02-24 05:01 PM
Re: Hardware inventory

The WMISysinfo script produces alot of info, is there anyway I can just run a simple script to get the info that I have requested above ?

Thanks

Saj


Glenn BarnasAdministrator
(KiX Supporter)
2009-02-24 05:06 PM
Re: Hardware inventory

You can use it as is and only employ the data you need, or use it as a basis for writing your own script.

As Lonk mentioned, there are many inventory script fragments here that you can modify or build into your own tool.

Glenn


Mart
(KiX Supporter)
2009-02-24 05:43 PM
Re: Hardware inventory

An example of a script that gets the info you want.
Work great on my Dell laptop.

 Code:
$wmiColl1 = GetObject("WinMgmts:root/cimv2").ExecQuery("Select * FROM Win32_ComputerSystem ")
$wmiColl2 = GetObject("WinMgmts:root/cimv2").ExecQuery("Select * FROM Win32_BIOS ")

For Each $wmiObj in $wmiColl1
	? "Manufacturer: " $wmiObj.Manufacturer
	? "Model: " $wmiObj.Model
	? "Currently logged on user: " $wmiObj.UserName
Next

For Each $wmiObj in $wmiColl2
	? "Service tag:" Trim($wmiObj.SerialNumber)
Next

Sleep 3


Saj
(Fresh Scripter)
2009-02-25 10:31 AM
Re: Hardware inventory

Thanks for that script Mart. That's exactly what I need. I just need the results to be stored in a text file or DB. Can you tell me how I can do that or provide the script.

Many thanks

Saj


Mart
(KiX Supporter)
2009-02-25 10:43 AM
Re: Hardware inventory

Hey I cannot give everything on a silver platter. You learn it by doing it so have a look at the Open, WriteLine and Close functions in the manual.
If you are new to Kix than writing it to a DB might be a huge step to start with. Writing it to a text file is imho the first step. If you are more familiar with kix then start doing DB stuff.


Saj
(Fresh Scripter)
2009-02-25 11:30 AM
Re: Hardware inventory

Am using the following command

RedirectOutput("hardware.log") = 0
ENDIF

But then it just creates a hardware.log file with this in it
Opened 'hardware.log' at 10:17:42

I need the screen output to go to this log file preferable with the users name as the filename.

Any help appreciated.


LonkeroAdministrator
(KiX Master Guru)
2009-02-25 11:40 AM
Re: Hardware inventory

what is this endif?

if you want all screen output in there, obviously you put this line before any output.

if you want the username in the filename, why don't you put it there?
look in the manual for @userid


Saj
(Fresh Scripter)
2009-02-25 12:00 PM
Re: Hardware inventory

This is the script that I am using :-

RedirectOutput("hardware.txt") = 0

$wmiColl1 = GetObject("WinMgmts:root/cimv2").ExecQuery("Select * FROM Win32_ComputerSystem ")
$wmiColl2 = GetObject("WinMgmts:root/cimv2").ExecQuery("Select * FROM Win32_BIOS ")

For Each $wmiObj in $wmiColl1
? "Manufacturer: " $wmiObj.Manufacturer
? "Model: " $wmiObj.Model
? "Currently logged on user: " $wmiObj.UserName
Next

For Each $wmiObj in $wmiColl2
? "Service tag:" Trim($wmiObj.SerialNumber)
Next

Sleep 3

Is this ok ?


Mart
(KiX Supporter)
2009-02-25 01:32 PM
Re: Hardware inventory

Ok, one more silver platter \:\)

Personally I do not like RedirectOutput so much.
I’d go for the Open, Writeline, Close option.

 Code:
Break on

$rc = Open(1, @SCRIPTDIR + "\hardware.txt", 5)

$wmiColl1 = GetObject("WinMgmts:root/cimv2").ExecQuery("Select * FROM Win32_ComputerSystem ")
$wmiColl2 = GetObject("WinMgmts:root/cimv2").ExecQuery("Select * FROM Win32_BIOS ")

For Each $wmiObj in $wmiColl1
	$rc = WriteLine(1, "Manufacturer: " + $wmiObj.Manufacturer + @CRLF)
	$rc = WriteLine(1, "Model: " + $wmiObj.Model + @CRLF )
	$rc = WriteLine(1, "Currently logged on user: " + $wmiObj.UserName + @CRLF )
Next

For Each $wmiObj in $wmiColl2
	$rc = WriteLine(1, "Service tag:" + Trim($wmiObj.SerialNumber) + @CRLF)
Next

$rc = Close(1)


This will give you issues when the next pc is writing to the file because you will not know who wrote what. Adding the system name would fix that or even create separate files for each computer. If this runs during logon the separate files option would be best imho because it will prevent issues with the file being locked for writing by system1 when system2 want to write his data.


Saj
(Fresh Scripter)
2009-02-25 01:58 PM
Re: Hardware inventory

Thanks Mart, your a star.

One more thing the script creates the file, but the logged on username does not show up, it's blank. This script will run upon logon, so ideally I want a separate file for each person who logs on with this info and the name of the file must be the users name.

Cheers


Mart
(KiX Supporter)
2009-02-25 02:36 PM
Re: Hardware inventory

I guess the logged on name is not yet known to WMI so it returns empty. Nothing you can do about that besides letting it write @USERID and not pulling it out with WMI.

Gargoyle
(MM club member)
2009-02-25 02:37 PM
Re: Hardware inventory

Then you change the line that opens the file

$rc = Open(1, @SCRIPTDIR + "\hardware.txt", 5)

Becomes

$rc = Open(1, "\\server\share\" + @UserID + ".txt", 5)

Or if you are not going to clean up the files on a regular basis...

$rc = Open(1, "\\server\share\" + @UserID + Join(Split(@Date,"/")) + ".txt", 5)


Saj
(Fresh Scripter)
2009-02-25 03:31 PM
Re: Hardware inventory

Thanks Gargoyle. That works a treat.

LonkeroAdministrator
(KiX Master Guru)
2009-02-25 03:43 PM
Re: Hardware inventory

darn golden platter'ers'

Radimus
(KiX Supporter)
2009-02-25 08:45 PM
Re: Hardware inventory

 Code:
break on
$ = setoption('wrapateol','on')
$ = SETCONSOLE("hide")

$InvVer = '2009/01/14 - VPN Subnet reporting'

$start = @ticks

	$Make 	=Trim(Split(WMIQuery("Manufacturer","Win32_ComputerSystem"))[0])
	$Model	=trim(WMIQuery("Model","Win32_ComputerSystem"))
	$SerNo	=WMIQuery("SerialNumber","Win32_BIOS")
		If Len($SerNo) < 2 	
			$SerNo=WMIQuery("SerialNumber","Win32_SystemEnclosure")
			If InStr($serno,"|")
				For Each $Return in Split($serno,"|")
					If Len($Return)<10	$serno=$Return		EndIf
					Next
				EndIf
			EndIf
	$SerNo	=Trim(Ucase($SerNo))
	if $serNo < '1'		quit		endif

	$asset	=WMIQuery("SMBIOSAssetTag","Win32_SystemEnclosure")
	if instr($asset,"|")
		for each $return in split($asset,"|")
			if $return	$asset=$return		endif
		next
	endif

	$asset	= join(Split($asset,"|"),'')
	$asset	= Trim(Ucase($asset))

	$CPUsp	=WMIQuery("CurrentClockSpeed","Win32_Processor")
		Select
			case left($CPUsp,3) = "339"		$CPUsp=3400
			case left($CPUsp,3) = "319"		$CPUsp=3200
			case left($CPUsp,3) = "299"  		$CPUsp=3000
			case left($CPUsp,3) = "279"		$CPUsp=2800
			case left($CPUsp,3) = "269"		$CPUsp=2600
			case left($CPUsp,3) = "239"		$CPUsp=2400
			case left($CPUsp,3) = "229"		$CPUsp=2200
			case left($CPUsp,3) = "220"		$CPUsp=2200
			case left($CPUsp,3) = "199"		$CPUsp=2000
   			case left($CPUsp,3) = "186"  		$CPUsp=1860
   			case left($CPUsp,3) = "182"  		$CPUsp=1830
			case left($CPUsp,3) = "179"		$CPUsp=1800
			case left($CPUsp,3) = "169"		$CPUsp=1700
			case left($CPUsp,3) = "159"		$CPUsp=1600
			case left($CPUsp,3) = "139"		$CPUsp=1400
			case left($CPUsp,3) = "106"		$CPUsp=1100
			case left($CPUsp,2) = "99"		$CPUsp=1000
			case left($CPUsp,2) = "90"		$CPUsp=900
			case left($CPUsp,2) = "85"		$CPUsp=850
			case left($CPUsp,2) = "70"		$CPUsp=700
			case left($CPUsp,2) = "66"		$CPUsp=667
			case left($CPUsp,2) = "59"		$CPUsp=600
			case left($CPUsp,2) = "49"		$CPUsp=500
			case left($CPUsp,2) = "39"		$CPUsp=400
			EndSelect

	$dimms	=Split(WMIQuery("Capacity","Win32_PhysicalMemory"),"|")
		For $a=0 to Ubound($dimms)
			$=Execute("$$dimm$a=val($$dimms[$a]) / 1048576")
			$=Execute("$$memory=val($$Memory)+val($$dimm$a)")
		Next

	$nic=0
	Do
		$ip 	= EnumIPInfo($nic,0)
		$snm	= EnumIPInfo($nic,1)
		$NicDesc= EnumIPInfo($nic,2)
		$mygw	= EnumIPInfo($nic,3)
		$nic=Val($nic)+1
	Until instr($ip,'192.168.22.') OR instr($ip,'192.168.24') OR instr($ip,'192.168.201.') OR Val($nic) > 10
	
	$network = left($ip,instrrev($ip,'.'))

	$Mac	=WMIQuery("MACAddress","Win32_NetworkAdapterConfiguration",,"Description",$NicDesc)
	$Mac	=Join(Split($mac,':'),'')
	$Mac	=Left($mac,InStr($mac,'|')-1)

	$NAITVD      ="HKLM\Software\Network Associates\TVD"
	$vsengine    =$NAITVD+"\Shared Components\VirusScan Engine\4.0.xx"
	$virusScan   =$NAITVD+'\VirusScan'+ iif(keyexist($NAITVD+'\VirusScan Enterprise\CurrentVersion'),' Enterprise\CurrentVersion','')
	$vscandir    =readvalue($virusScan,"szInstallDir")
	$vscaneng    =readvalue($vsengine,"szEngineVer")
	$vsdatdir    =readvalue($vsengine,"szInstallDir")
	$McAfee	     ="HKLM\Software\McAfee"
	if KeyExist($McAfee)
		$avengine    =$McAfee+"\AVEngine"
		$avDP	     =$McAfee+"\DesktopProtection"
		$vscanver    =readvalue($avDP,"szProductVer")
		$vscaneng    =readvalue($avengine,"EngineVersionMajor") + "." + readvalue($avengine,"EngineVersionMinor")
		$vscandat    =readvalue($avengine,"AVDatVersion")
		$vsdatdate   =readvalue($avengine,"AVDatDate")
	else if instr($virusScan,'Enterprise')  
		$vscanver    =readvalue($virusScan,"szProductVer")
		$vscandat    =readvalue($vsengine,"szVirDefVer")
		$vsdatdate   =readvalue($vsengine,"szVirDefDate")
		else
			$vscanver    =readvalue($virusScan,"szCurrentVersionNumber")
			$vscandat    =readvalue($vsengine,"szDatVersion")
			$vsdatdate   =readvalue($vsengine,"szDatDate")
		endif
	endif

	$MSOdir	=readvalue("HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe","Path")
	$excel	=getfileversion("$MSOdir\excel.exe")
	$MSOVer =split($excel,'.')[0]

	$MSOSPVer = ''

	$assigned = readvalue("HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters","srvcomment")
	if left($assigned,2) = '68'
		$assigned = left(right(join(split(join(split($assigned,' '),''),'-'),''),-2),9)
	endif

	$ini = 0

$cnstring = "DRIVER={SQL Server};SERVER=inventory;UID=Inventory;PWD=kixscript;DATABASE=inv"

$cn = CreateObject("ADODB.Connection")
$cmd= CreateObject("ADODB.Command")
$rs = CreateObject("ADODB.RecordSet")

$cn.connectionstring = $cnstring
$cn.open
$cmd.activeconnection = $cn
$rs.cursortype = 3
$rs.locktype = 3
$rs.activecommand = $cmd

$cmdtxt = "select * from tbl_Main where SerialNumber = '$serNo'"
$cmd.commandtext = $cmdtxt $rs.open ($cmd)			;? 'Error = '+@ERROR+' - '+@SERROR
	IF $rs.eof = -1		$rs.addnew	ENDIF 

	$rs.fields.item("SerialNumber").value  = $serNo
	$rs.fields.item("ComputerName").value  = @wksta
	$rs.fields.item("AssignedTo").value    = $assigned
	$rs.fields.item("NetworkID").value     = $network
	$rs.fields.item("IPAddress").value     = $ip
	$rs.fields.item("InvDate").value       = @date
	$rs.fields.item("Version").value       = $InvVer
$rs.update							;? 'Error = '+@ERROR+' - '+@SERROR
;if not @error
	$nul = WriteValue('HKLM\SOFTWARE\Information Technology\SQLInventory','LastRun',@date,REG_SZ)
;endif
$rs.close

$cmdtxt = "select * from dbo._tbl_OSEnv where SerialNumber = '$serNo'"
$cmd.commandtext = $cmdtxt $rs.open ($cmd)			;? 'Error = '+@ERROR+' - '+@SERROR
	IF $rs.eof = -1		$rs.addnew	ENDIF 

	$rs.fields.item("SerialNumber").value	= $serNo
	$rs.fields.item("OS").value 		= @ProductType
	$rs.fields.item("OSCSD").value		= @csd
	$rs.fields.item("64bit").value		= @OnWoW64
	$rs.fields.item("AVEngine").value	= $vscanver
	$rs.fields.item("AVDate").value		= $vsdatdate
	$rs.fields.item("MSO").value		= $MSOVer
	$rs.fields.item("MSOSP").value		= $MSOSPVer
$rs.update							? 'Error = '+@ERROR+' - '+@SERROR
$rs.close



$cmdtxt = "select * from dbo._tbl_OrgCodes where OrgCode = '$Asset'"
$cmd.commandtext = $cmdtxt $rs.open ($cmd)			;? 'Error = '+@ERROR+' - '+@SERROR
	$code = IIF($rs.eof = -1,1,0)
$rs.close							;? 'Error = '+@ERROR+' - '+@SERROR



$cmdtxt = "select * from tbl_Computers where SerialNumber = '$serNo'"
$cmd.commandtext = $cmdtxt $rs.open ($cmd)			;? 'Error = '+@ERROR+' - '+@SERROR
	IF $rs.eof = -1
		$rs.addnew 

		$rs.fields.item("SerialNumber").value	= $serNo
		if $code = 1
			$rs.fields.item("Tag").value= $asset	
		else
			$rs.fields.item("OrgCode").value= $asset
		endif
		$rs.fields.item("Make").value		= $Make 	
		$rs.fields.item("Model").value		= $Model	
		$rs.fields.item("CPU").value		= $CPUsp
		$rs.fields.item("Memory").value		= $memory
		$rs.fields.item("FirstInventory").value = @date
		$rs.update					;? 'Error = '+@ERROR+' - '+@SERROR
		;if not @error
			$nul = WriteValue('HKLM\SOFTWARE\Information Technology\SQLInventory','FirstRun',@date,REG_SZ)
		;endif
	else
		if $rs.fields.item("Tag").value < '0' and $code = 1
			$rs.fields.item("Tag").value= $asset	
			$rs.update				;? 'Error = '+@ERROR+' - '+@SERROR
		endif

		if $rs.fields.item("OrgCode").value < '0' and $code = 0
			$rs.fields.item("OrgCode").value= $asset
			$rs.update				;? 'Error = '+@ERROR+' - '+@SERROR
		endif

		if $rs.fields.item("FirstInventory").value < '0'
			$rs.fields.item("FirstInventory").value = @date
			$rs.update				;? 'Error = '+@ERROR+' - '+@SERROR
		endif
	ENDIF
$rs.close




$cmdtxt = "Delete from dbo._tbl_Software where SerialNumber = '$serNo'"
$cmd.commandtext = $cmdtxt 
$rs.Open($cmd) 			;? 'Error = '+@ERROR+' - '+@SERROR
$rs.close 			;? 'Error = '+@ERROR+' - '+@SERROR

$cmdtxt = "select * from dbo._tbl_Software where SerialNumber = '$serNo'"
$cmd.commandtext = $cmdtxt $rs.open ($cmd)			;? 'Error = '+@ERROR+' - '+@SERROR

$Index = 0	$err = 0	$key = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
Do
	$SubKey = ENUMKEY($Key, $Index)
	If @ERROR = 0
		if not left($subkey,'2') = 'KB'
			$DisplayName 	= Readvalue($key+$subKey,'DisplayName')
			$DisplayVersion = Readvalue($key+$subKey,'DisplayVersion')
			$InstallDate 	= Readvalue($key+$subKey,'InstallDate')
			$InstallLocation= Readvalue($key+$subKey,'InstallLocation')
			$Publisher 	= Readvalue($key+$subKey,'Publisher')

			if not $DisplayName + $DisplayVersion + $InstallDate + $InstallLocation + $Publisher < 'a'	
				$rs.addnew
				$rs.fields.item("SerialNumber").value		= $Serno 
				$rs.fields.item("RegKeyName").value		= $SubKey 
				$rs.fields.item("DisplayName").value		= $DisplayName	
				$rs.fields.item("DisplayVersion").value		= $DisplayVersion	
				$rs.fields.item("InstallDate").value		= $InstallDate
				$rs.fields.item("InstallLocation").value 	= $InstallLocation
				$rs.fields.item("Publisher").value		= $Publisher
				$rs.fields.item("InvDate").value      		= @date
				$rs.update
			endif
		endif
		$Index = $Index + 1
	else
		$err = 1
	Endif
Until $err

$rs.close



NTDOCAdministrator
(KiX Master)
2009-02-26 03:17 AM
Re: Hardware inventory

As I live an breath. Was that a post from Radimus?

Cheers Rad and glad you're still around somewhere.


Radimus
(KiX Supporter)
2009-02-26 03:33 AM
Re: Hardware inventory

i'm always around, but usually on facebook... one must keep up with the times

Ashpoint
(Starting to like KiXtart)
2009-02-26 06:44 AM
Re: Hardware inventory

Hi Guys...

Playing with the "seed" code, I'm using the following separated lines in my routine:

 Code:
$rc = WriteLine(1, "Physical Memory: " + $wmiObj.TotalPhysicalMemory + @CRLF )

And
 Code:
$rc = WriteLine(1, "Hard Drive: " + $wmiObj.Size + @CRLF)

And
 Code:
$rc = WriteLine(1, "Drive Space: " + $wmiObj.FreeSpace + @CRLF)


The code returns long numbers similar to the following (useless) values:
Physical Memory: 2146713600
Hard Drive: 160039239680
Drive Space: 105328513024


How can I make these values look like:
Physical Memory: 2.1mb
Hard Drive: 160mb
Drive Space: 90mb
etc?

I tried doing:
 Code:
$rc = WriteLine(1, "Hard Drive: " + VAL($wmiObj.Size)/1000000000 + @CRLF)

but that wasn't useful.


LonkeroAdministrator
(KiX Master Guru)
2009-02-26 10:19 AM
Re: Hardware inventory

try any of these:
meminfo() - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=83843
meminfo2() - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=113155
memory() - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=134637
physicalMemoryInfo() - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=176025


Glenn BarnasAdministrator
(KiX Supporter)
2009-02-26 01:17 PM
Re: Hardware inventory

Try this:
 Code:
$X = '160039239680'
$X ?
Val($X) ?

Does the result surprise you?

Why do you think you got the answers you did?

This is a common issue in most languages.. you might recognize the problem yourself and find the solution in the manual. If not, reply and I'll explain further..

Glenn


Ashpoint
(Starting to like KiXtart)
2009-02-27 06:13 AM
Re: Hardware inventory

Hi Glen...

Mark me as a bit dumb.

I created TEST1.Kix as follows:

 Code:
$x = '160039239680'
? $x
? VAL($x)


I got
160039239680 and
1125449728

That surprised me!

So, put me out of my misery and explain (please).

Michael


LonkeroAdministrator
(KiX Master Guru)
2009-02-27 01:09 PM
Re: Hardware inventory

before he respons, you could search the manual for "-21"

Glenn BarnasAdministrator
(KiX Supporter)
2009-02-27 01:11 PM
Re: Hardware inventory

The function you were using returns the value as a string, so in my example, $X is a string - the value has quotes around it.

Val() converts strings to numbers, but is limited to Integers. What you are seeing is the result of "wrap" when the excess bits are dropped.

Because the size of your value exceeds the limit of Integer data, you need to employ double-precision math. Some languages have other (or even several) class names such as Real, Float, Single.. these simply reflect the maximum range that can be represented. The higher precision employed, the more data memory consumed, so languages like C have several options. Kix just offers Double and Integer.

So - you need to convert the string containing a large value to a double-precision value first. You need to recognize the size of your data and plan for it in the code (or adapt once you see the strange values!) One method is to use the CDbl() function, and another common "shortcut" is to multiply by 1.0, but the multiplier must come first - as so:
 Code:
$x = '160039239680'
$x ?
VAL($x) ?

CDbl($X) ?
1.0 * $X ?

Also, it's considered a matter of preference, but.. "?" represents a CR/LF sequence - same as @CRLF. It is not a shortcut for a "print" statement as in old versions of BASIC. If you place the "?" in front of your output statements, you will always start with a blank line and leave the cursor at the end of the output. Try this:
 Code:
CLS
For $X = 1 to 24
  ? $X
Next
Get $X
Where is the #1 displayed? Where is the cursor? Try
 Code:
CLS
For $X = 1 to 24
  $X ?
Next
Get $X
Now where is the #1 displayed? See how the leading "?" offsets the output? This can affect screen formatting as well as file data. In fact, not having a closing CRLF could affect some file-based applications. The CRLF sequence should follow your data instead of preceeding it.

Glenn


River911
(Just in Town)
2009-05-05 10:46 PM
Re: Hardware inventory

A little lost here on your Hardware SQL WMI queries. Is there a UDF that i am supposed to reference.