[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

How to add a new record to a sql database using a vbscript

Hello Everyone,

I am working on a computer inventory database for my company.  I have a script that will get the username, computer name, and serial number from any computer and enter into a text file.

Now I recently created a sql database to put all these records into.  I would like to modify this script so instead of saving to a text file, it would add a new record to the computerinventory table in my sql database.  Is there anyway I can do this?

Here is the script I have right now.

Thanks,

Bill





Dim objWshShell, objWMIService, colSMBIOS, objSMBIOS,objFSO,objFile
	Dim strUserName, strComputerName, strComputer 
	Const ForAppending = 8
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile("\\myserver\directory1$\myinventory.txt", ForAppending)
	Set objWshShell = CreateObject("WScript.Shell") 
	strUserName = objWshShell.ExpandEnvironmentStrings("%USERNAME	strComputerName = objWshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")   
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
	Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure") 
	For Each objSMBIOS in colSMBIOS 
		 objFile.WriteLine  strUserName& "," & strComputerName& "," & objSMBIOS.SerialNumber
	Next  
        Wscript.Echo "Finished! Thank You!"

Open in new window

0
bjennings
Asked:
bjennings
  • 2
1 Solution
 
David LeeCommented:
Hi, bjennings.

You need code like that below.  Here's a link to guidance on the connection string for SQL server: http://www.connectionstrings.com/?carrier=sqlserver2005  The code in the first block appears once at the top of the script.  The code in the second block replaces line #12 in your code.
'Create the connection to the database'
Set adoCon = CreateObject("ADODB.Connection")
'Change the connection string on the following line'
adoCon.Open "SQL Connection String"
 
 
'Insert the data into the database'
'Change the field names on the following line to those of your fields'
strFields = "UserName,ComputerName,SerialNumber"
varValues = "'" & strUserName& "','" & strComputerName& "','" & objSMBIOS.SerialNumber & "'"
adoCon.Execute "INSERT INTO ComputerInventory (" & strFields & ") VALUES(" & varValues & ")"

Open in new window

0
 
bjenningsAuthor Commented:
Awesome...It worked!  Thank you so much!!!
0
 
David LeeCommented:
You're welcome.  Glad I could help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now