#106555 - 2003-11-03 05:42 PM
Re: Inventory Management
|
Breaker
Hey THIS is FUN
   
Registered: 2001-06-15
Posts: 268
Loc: Yorkshire, England
|
Bob,
I have my logon script write a hardware inventory gleaned from WMI to an Access db, but have as yet never bothered with creating any kind of interface, web-based or otherwise, with which to query this info. I just look at the tables.
Here's the script - if you're interested I'll mail you a blank copy of the DB.
[Edit] Sorry about the long lines!! [/Edit]
code:
;Script to write info to database at logon - uses WMI and ADO for connection and collection ;Created Feb 2002 by Neil Moran
;First, set variables for the connection to the database and other general options $x=SetOption("WrapAtEOL","On") $DATABASE = "\\server\share\logon.mdb" $DSN = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=$DATABASE" $WMI = "WINMGMTS:{IMPERSONATIONLEVEL=IMPERSONATE}!//@WKSTA" $WMI_Test = GetObject($WMI) If @ERROR <> 0 Goto WMI_Error Endif $WMI_Test = 0
;Create SQL statements to check/write/update database $SQL_USERS = "SELECT * FROM TBL_USERS WHERE USERNAME='@USERID';" $SQL_COMPUTERS = "SELECT * FROM TBL_COMPUTERS WHERE WORKSTATION='@WKSTA';"
;Create database objects $Connection = CreateObject("ADODB.Connection") ;? "Create Connection object " + @ERROR + ": " + @SERROR Get $x $Command = CreateObject("ADODB.Command") ;? "Create Command object " + @ERROR + ": " + @SERROR Get $x $Recordset = CreateObject("ADODB.Recordset") ;? "Create recordset object " + @ERROR + ": " + @SERROR Get $x ;Set properties of DB objects and open connection to database $Connection.ConnectionString = $DSN $Connection.Open() ;? "Open connection " + @ERROR + ": " + @SERROR Get $x If @ERROR <> 0 Goto ADO_Error Endif $Command.ActiveConnection = $Connection $Recordset.CursorType = 3 $Recordset.LockType = 3 $Recordset.ActiveCommand = $Command
;Check for User details first $Command.CommandText = $SQL_USERS $Recordset.Open($Command) ;? "Open user recordset " + @ERROR + ": " + @SERROR Get $x ;Create new record if none exists to update If $Recordset.RecordCount < 1 $Recordset.AddNew ;? "Add new user record " + @ERROR + ": " + @SERROR Get $x Endif
If InGroup("\\" + @WKSTA + "\Administrators") $IsLocalAdmin = "True" Else $IsLocalAdmin = "False" Endif
;Write user values into database field by field $Recordset.Fields("UserName").Value = @USERID $Recordset.Fields("FullName").Value = @FULLNAME $Recordset.Fields("Workstation").Value = @WKSTA $Recordset.Fields("IsLocalAdmin").Value = $IsLocalAdmin $Recordset.Fields("PrivilegeLevel").Value = @PRIV $Recordset.Fields("HomeDrive").Value = @HOMESHR $Recordset.Fields("LastUpdate").Value = @DATE + " " + @TIME $Recordset.Fields("Description").Value = @COMMENT
;Update the new record and close the recordset object $Recordset.Update ;? "Update users " + @ERROR + ": " + @SERROR Get $x $UserIdentifier = $Recordset.Fields("UserID").Value $Recordset.Close() ;? "Close user recordset " + @ERROR + ": " + @SERROR Get $x
;Check Computer details next - refresh $Recordset object with new query $Command.CommandText = $SQL_COMPUTERS $Recordset.Open($Command) ;? "Open computer recordset " + @ERROR + ": " + @SERROR Get $x
;Check for existing record to update If $Recordset.RecordCount < 1 $Recordset.AddNew ;? "Add new computer record " + @ERROR + ": " + @SERROR Get $x Endif
$SystemManufacturer = WMIQuery("Manufacturer","Win32_ComputerSystem") $SystemModel = WMIQuery("Model","Win32_ComputerSystem") $SerialNo = WMIQuery("SerialNumber","Win32_BIOS") $PhysicalMemory = val(WMIQuery("TotalPhysicalMemory","Win32_LogicalMemoryConfiguration"))/1024 $ProcessorSpeed = WMIQuery("CurrentClockSpeed","Win32_Processor") $PagefileSpace = val(WMIQuery("TotalPageFileSpace","Win32_LogicalMemoryConfiguration"))/1024 $VidMode = WMIQuery("VideoModeDescription","Win32_VideoController") $VidCard = WMIQuery("VideoProcessor","Win32_VideoController") If Len($VidCard) = 0 $VidCard = WMIQuery("Description","Win32_VideoController") Endif $DiskController = WMIQuery("InterfaceType","Win32_DiskDrive") $Modem = WMIQuery("Description","Win32_POTSModem")
;Write values into table $Recordset.Fields("Workstation").Value = @WKSTA $Recordset.Fields("NTDomain").Value = @DOMAIN $Recordset.Fields("SystemManufacturer").Value = $SystemManufacturer $Recordset.Fields("SystemModel").Value = $SystemModel $Recordset.Fields("SerialNo").Value = $SerialNo $Recordset.Fields("OS").Value = @PRODUCTTYPE $Recordset.Fields("ServicePack").Value = @CSD $Recordset.Fields("PhysicalMemory").Value = $PhysicalMemory $Recordset.Fields("ProcessorSpeed").Value = $ProcessorSpeed $Recordset.Fields("PagefileSpace").Value = $PagefileSpace $Recordset.Fields("VidMode").Value = $VidMode $Recordset.Fields("VidCard").Value = $VidCard $Recordset.Fields("Modem").Value = $Modem $Recordset.Fields("LoggedOnUser").Value = @USERID $Recordset.Fields("LastUpdate").Value = @DATE + " " + @TIME
;Update and close recordset object $Recordset.Update ;? "Update computer record " + @ERROR + ": " + @SERROR Get $x $PCIdentifier = $Recordset.Fields("PCID").Value ;? "PC ID number is " + $PCIdentifier $Recordset.Close() ;? "Close computer recordset " + @ERROR + ": " + @SERROR Get $x
;Slightly different process for update of Disks table $DiskSet = GetObject($WMI).ExecQuery("SELECT * FROM WIN32_LOGICALDISK WHERE DRIVETYPE=3") ;? "Get DiskSet info from WMI " + @ERROR + ": " + @SERROR Get $x For Each $Disk In $DiskSet $Name = $Disk.Name ; ? "Disk name is " + $Name $FreeSpace = $Disk.FreeSpace ; ? "Free space is " + $FreeSpace $TotalSpace = $Disk.Size ; ? "Total size is " + $TotalSpace $Format = $Disk.FileSystem ; ? "Disk format is " + $Format $SQL_DISKS = "SELECT * FROM TBL_DISKS WHERE COMPID=" + $PCIDENTIFIER + " AND DRIVENAME='" + $NAME + "';" $Command.CommandText = $SQL_DISKS $Recordset.Open($Command) ; ? "Open disks recordset " + @ERROR + ": " + @SERROR Get $x If $Recordset.RecordCount < 1 $Recordset.AddNew ; ? "Add new disk record " + @ERROR + ": " + @SERROR Get $x Endif $Recordset.Fields("compID").Value = $PCIdentifier $Recordset.Fields("DriveName").Value = $Name $Recordset.Fields("FreeSpace").Value = $FreeSpace $Recordset.Fields("TotalSpace").Value = $TotalSpace $Recordset.Fields("Format").Value = $Format $Recordset.Update ; ? "Update disk record " + @ERROR + ": " + @SERROR Get $x $Recordset.Close() ; ? "Close disk recordset " + @ERROR + ": " + @SERROR Get $x Next
;Collect share info on the local machine $WMI_SHARE = "SELECT * FROM WIN32_SHARE" $Shares = GetObject($WMI).ExecQuery($WMI_SHARE) ;? "Get share info from WMI " + @ERROR + ": " + @SERROR Get $x For Each $Share In $Shares $Name = $Share.Name $Path = $Share.Path $Desc = $Share.Description $Type = $Share.Type Select Case $Type = 0 $Type = "Disk Drive" Case $Type = 1 $Type = "Print Queue" Case $Type = 2 $Type = "Device" Case $Type = 3 $Type = "IPC" Case $Type = 2147483648 $Type = "Disk Drive Admin" Case $Type = 2147483649 $Type = "Print Queue Admin" Case $Type = 2147483650 $Type = "Device Admin" Case $Type = 2147483651 $Type = "IPC Admin" Endselect ;Type uint32 Read-only ;Type of resource being shared. Types include disk drives, print queues, interprocess communications (IPC), and general devices. ;Values are: ;0 = Disk Drive ;1 = Print Queue ;2 = Device ;3 = IPC ;2147483648 = Disk Drive Admin ;2147483649 = Print Queue Admin ;2147483650 = Device Admin ;2147483651 = IPC Admin $Hyperlink = "\\" + @WKSTA + "\" + $Name $SQL_SHARES = "SELECT * FROM TBL_SHARES WHERE COMPID=" + $PCIDENTIFIER + " AND SHARENAME='" + $NAME + "'" $Command.CommandText = $SQL_SHARES $Recordset.Open($Command) ; ? "Open Share recordset " + @ERROR + ": " + @SERROR Get $x If $Recordset.RecordCount < 1 $Recordset.AddNew ; ? "Add new share record " + @ERROR + ": " + @SERROR Get $x Endif $Recordset.Fields("compID").Value = $PCIdentifier $Recordset.Fields("ShareName").Value = $Name $Recordset.Fields("SharePath").Value = $Path $Recordset.Fields("Description").Value = $Desc $Recordset.Fields("ShareType").Value = $Type $Recordset.Fields("Hyperlink").Value = $Hyperlink $Recordset.Update ; ? "Update share record " + @ERROR + ": " + @SERROR Get $x $Recordset.Close() ; ? "Close share recordset " + @ERROR + ": " + @SERROR Get $x Next ;And now grab Network Adapter info and update specific table $WMI_NETWORK_1 = "SELECT * FROM WIN32_NETWORKADAPTERCONFIGURATION WHERE IPENABLED='TRUE'" $NetCards = GetObject($WMI).ExecQuery($WMI_NETWORK_1) ;? "Get simple adapter info from WMI " + @ERROR + ": " + @SERROR Get $x For Each $Card In $NetCards $DeviceID = $Card.Index ; ? "Device ID is " + $DeviceID $MACAddress = $Card.MACAddress ; ? "MAC Address is " + $MACAddress $NetCard = $Card.Description ; ? "Net card description is " + $NetCard $IPAddress = $Card.IPAddress ; ? "IP Address is " + $IPAddress[0] $DHCP = $Card.DHCPEnabled If $DHCP $DHCP = "DHCP Enabled" Else $DHCP = "Static address" Endif ; ? "DHCP state is " + $DHCP $SQL_NETWORK = "SELECT * FROM TBL_NETWORKADAPTERS WHERE COMPID=" + $PCIDENTIFIER + " AND DEVICEID=" + $DeviceID + ";" $Command.CommandText = $SQL_NETWORK $Recordset.Open($Command) ; ? "Open Adapters recordset " + @ERROR + ": " + @SERROR Get $x If $Recordset.RecordCount < 1 $Recordset.AddNew ; ? "Add new adapters record " + @ERROR + ": " + @SERROR Get $x Endif $Recordset.Fields("compID").Value = $PCIdentifier $Recordset.Fields("DeviceID").Value = $DeviceID $Recordset.Fields("NetCard").Value = $NetCard $Recordset.Fields("IPAddress").Value = $IPAddress[0] $Recordset.Fields("MACAddress").Value = $MACAddress $Recordset.Fields("DHCP").Value = $DHCP $Recordset.Update ; ? "Update net card record " + @ERROR + ": " + @SERROR Get $x $Recordset.Close() ; ? "Close net card recordset " + @ERROR + ": " + @SERROR Get $x Next
;Collect Printer Connection info - should grab local as well $WMI_PRINTERS = "SELECT * FROM WIN32_PRINTER" $Printers = GetObject($WMI).ExecQuery($WMI_PRINTERS) ; ? "Get printers from WMI " + @ERROR + ": " + @SERROR Get $x For Each $Printer In $Printers $PrinterID = $Printer.DeviceID $DriverName = $Printer.DriverName $PortName = $Printer.PortName $Description = $Printer.Description $SQL_PRINTERS = "SELECT * FROM TBL_PRINTERCONNECTIONS WHERE COMPID=" + $PCIdentifier + " AND PRINTERID='" + $PrinterID + "'" $Command.CommandText = $SQL_PRINTERS $Recordset.Open($Command) ; ? "Open printer recordset " + @ERROR + ": " + @SERROR Get $x If $Recordset.RecordCount < 1 $Recordset.AddNew ; ? "Add new printer record " + @ERROR + ": " + @SERROR Get $x Endif $Recordset.Fields("compID").Value = $PCIdentifier $Recordset.Fields("PrinterID").Value = $PrinterID $Recordset.Fields("DriverName").Value = $DriverName $Recordset.Fields("PortName").Value = $PortName $Recordset.Fields("Description").Value = $Description $Recordset.Update ; ? "Update new printer record " + @ERROR + ": " + @SERROR Get $x $Recordset.Close() ; ? "Close printer recordset " + @ERROR + ": " + @SERROR Get $x Next
;Collect info on user's network drives $WMI_MAPPEDDRIVES = "SELECT * FROM WIN32_LOGICALDISK WHERE DRIVETYPE=4" $MappedDrives = GetObject($WMI).ExecQuery($WMI_MAPPEDDRIVES) For Each $Drive in $MappedDrives $Letter = $Drive.Name $Path = $Drive.ProviderName $SQL_MAPPEDDRIVES = "SELECT * FROM TBL_MAPPEDDRIVES WHERE USERID=" + $UserIdentifier + " AND LETTER='" + $Letter + "'" $Command.CommandText = $SQL_MAPPEDDRIVES $Recordset.Open($Command) If $Recordset.RecordCount < 1 $Recordset.AddNew Endif $Recordset.Fields("UserID").Value = $UserIdentifier $Recordset.Fields("Letter").Value = $Letter $Recordset.Fields("Path").Value = $Path $Recordset.Update $Recordset.Close() Next
;Close connection to database $Connection.Close() ;? "Close database connection " + @ERROR + ": " + @SERROR Get $x
;Tidy up by releasing COM objects from memory $Connection = 0 $Recordset = 0 $Command = 0
Hope this helps
Breaker [ 03. November 2003, 17:48: Message edited by: Breaker ]
_________________________
================================================ Breaker
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 837 anonymous users online.
|
|
|