Read machine name and bios version and write to MSSQL database
Read machine name and bios version and write to MSSQL database
(OP)
Hi
I have the following script which reads the machine name and then looks up the BIOS version on the PC, currently it only echo the results. I have a SQL server available to me, how would I connect to the server and write the results to the database? I've looked hi and low on MSDN and can't find an example of how to do this using VBscript/WSH. if someone can give me an example I'd be very grateful. Thanks, Steven
Here is the code:
'Read Machine Name
Set objComputer = CreateObject("Shell.LocalMachine")
Wscript.Echo "Computer name: " & objComputer.MachineName
'Read BIOS details
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery _
("Select * from Win32_BIOS")
For each objBIOS in colBIOS
Wscript.Echo objBIOS.Manufacturer
Wscript.Echo objBIOS.Name
Wscript.Echo objBIOS.SerialNumber
Next
I have the following script which reads the machine name and then looks up the BIOS version on the PC, currently it only echo the results. I have a SQL server available to me, how would I connect to the server and write the results to the database? I've looked hi and low on MSDN and can't find an example of how to do this using VBscript/WSH. if someone can give me an example I'd be very grateful. Thanks, Steven
Here is the code:
'Read Machine Name
Set objComputer = CreateObject("Shell.LocalMachine")
Wscript.Echo "Computer name: " & objComputer.MachineName
'Read BIOS details
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery _
("Select * from Win32_BIOS")
For each objBIOS in colBIOS
Wscript.Echo objBIOS.Manufacturer
Wscript.Echo objBIOS.Name
Wscript.Echo objBIOS.SerialNumber
Next

RE: Read machine name and bios version and write to MSSQL database
Set objConn = CreateObject("ADODB.Connection")
objConn.Open "Driver={SQL Server};" & _
"Server=Your Server;" & _
"Database=Your Database;" & _
"user id=sa;" & _
"password=Your SA Password;"
Set objRSSQL = CreateObject("ADODB.Recordset")
'Read Machine Name
Set objComputer = CreateObject("Shell.LocalMachine")
Wscript.Echo "Computer name: " & objComputer.MachineName
'Read BIOS details
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery _
("Select * from Win32_BIOS")
For each objBIOS in colBIOS
SQLQuery = "insert into tblbios (manufacture, Name, SerialNum)" & _
" values ('" & objBIOS.Manufacturer & "','" & objBIOS.Name & "','" & objBIOS.SerialNumber & "')"
objRSSQL.Open SQLQuery, objConn
Next
NOTE: You will need to enter your own information in the "objConn.Open" area. Also on the "SQLQuery =" area you will need to change the table name "tblbios" to whatever your table name is and the field names as well. Keep in mind that this will insert a new record each time it is ran even if an entry already exist. You may want to include the machine name and then check to see if the record for the machine already exist and if so update it and if not then insert it.
Good luck and I hope this helps.
RE: Read machine name and bios version and write to MSSQL database