Login | |
|
 |
VBS from Access to SQL Server - 9/21/2007 5:24:08 AM
|
|
 |
|
| |
josborn
Posts: 6
Score: 0
Joined: 9/21/2007
Status: offline
|
Hello All, I am having an issue and was hoping you could help me. I have the velow script which connects and inserts into an Access DB. I just need to make it connect and insert into a SQL DB instead. The schema is identical. Thanks in advance for any help you can give me. Keep up the good work!!! Joe Dim oAccess Const ADS_SCOPE_SUBTREE = 2 Const NoDecimals = 0 Const Decimals = 2 Const ForReading = 1 Currentdate = Date Set objDictionary = CreateObject("Scripting.Dictionary") db = "C:\FreeSpace.mdb" tablename = "Space" dtDate = "dtDate" ServerName = "ServerName" Drive = "Drive" TotalSpace = "TotalSpace" UsedSpace = "UsedSpace" FreeSpace = "FreeSpace" PerFree = "PerFree" On error resume next Set WshShell = CreateObject("WScript.Shell") Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCOmmand.ActiveConnection = objConnection Set oAccess = CreateObject("Access.Application") oAccess.OpenCurrentDatabase db oAccess.DoCmd.OpenTable(tablename) 'Retrieve all Domain Controllers from Domain objCommand.CommandText = "Select Name from 'LDAP://OU=Servers,DC=comain,DC=com' Where operatingSystem = '*' Order by name" objCommand.Properties("Page Size") = 1000 objCommand.Properties("Timeout") = 30 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.Properties("Cache Results") = True Set objRecordSet = objCommand.Execute 'Execute Query 'objRecordSet.MoveFirst Do Until objRecordSet.EOF server = objRecordSet.Fields("Name").Value wscript.echo server Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & server & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3") For Each objDisk in colDisks TotalSpace =( objDisk.Size / 1073741824 ) perfree =( objDisk.FreeSpace / objDisk.Size ) FreeSpace =( objDisk.FreeSpace / 1073741824 ) UsedSpace = ( TotalSpace - FreeSpace ) ' SQLstmt = "INSERT INTO " & tableName & " (" & LockOutDate & "," & AuthenticationServer & "," & samAccountName & "," & ClientMachine & ") VALUES ('" ' SQLstmt = SQLstmt & dtmTimeGenerated.GetVarDate("False") &"','" & objEvent.ComputerName & "','" & strSAMAccountName & "','" & strClientMachine & "')" SQLstmt = "INSERT INTO " & tableName & " (dtDate,ServerName,Drive,TotalSpace,UsedSpace,FreeSpace,PerFree) VALUES ('" SQLstmt = SQLstmt & currentdate & "','" & server & "','" & objDisk.DeviceID & "','" & TotalSpace & "','" & UsedSpace & "','" & FreeSpace & "','" & PerFree & "')" ' SQLstmt = "INSERT INTO " & tableName & " (" & dtDate & ") VALUES ('" & currentdate & "')" oAccess.DoCmd.RunSQL(SQLstmt) ' If perfree < "0.2" Then ' Set objEmail = CreateObject("CDO.Message") ' objEmail.From = "emailaddress" ' objEmail.To = "emailaddress" ' objEmail.Subject = server &" has 20% HDD Space available on "& objdisk.DeviceID ' objEmail.Textbody = "Free Disk Space:.." & FormatNumber(FreeSpace, Decimals) &" Gigs"& VBCRLF &"Percent Free:....." & FormatPercent(perfree, NoDecimals) ' objEmail.Configuration.Fields.Item _ ' ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 ' objEmail.Configuration.Fields.Item _ ' ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "server" ' ' objEmail.Configuration.Fields.Item _ ' ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 ' objEmail.Configuration.Fields.Update ' objEmail.Send ' End if Next TotalSpace = 0 perfree = 0 FreeSpace = 0 UsedSpace = 0 objRecordSet.MoveNext Loop oAccess.DoCmd.Close oAccess.DoCmd.Quit Set oAccess = Nothing
< Message edited by josborn -- 9/21/2007 5:26:05 AM >
|
|
| |
|
|
|
 |
RE: VBS from Access to SQL Server - 9/24/2007 1:12:51 AM
|
|
 |
|
| |
josborn
Posts: 6
Score: 0
Joined: 9/21/2007
Status: offline
|
This does not seem to be working... The linked table in access does however. I would really like to get rid of the access db all together... Thanks! SQLstmt = "INSERT INTO[ODBC;DSN=DiskSpace]." & tableName & " (dtDate,ServerName,Drive,TotalSpace,UsedSpace,FreeSpace,PerFree) VALUES ('" SQLstmt = SQLstmt & currentdate & "','" & server & "','" & objDisk.DeviceID & "','" & TotalSpace & "','" & UsedSpace & "','" & FreeSpace & "','" & PerFree & "')" oAccess.DoCmd.RunSQL(SQLstmt)
|
|
| |
|
|
|
|
|