I am trying to insert MailboxDisplayName and LastLogonTime from my Exchange server into a SQL database. The script executes without errors, but the data is not being inserted. Can someone please tell me what I am doing wrong? Thanks
On Error Resume Next
strComputer = "EXCHANGE"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & _
"\ROOT\MicrosoftExchangeV2")
Set colItems = objWMIService.ExecQuery _
("Select * from Exchange_Mailbox")
strComputerSQL = "SQLSERVER"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open _
"Provider=SQLOLEDB;Data Source=" & strComputerSQL & ";" & _
"Trusted_Connection=Yes;Initial Catalog=MailboxUsage"
For Each objItem in colItems
sql = "INSERT INTO TestTable (MailboxName, LastLogonTime) VALUES (MailboxDisplayName, LastLogonTime)"
objConnection.Execute(sql)
Next
On Error Resume Next
strComputer = "EXCHANGE"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & _
"\ROOT\MicrosoftExchangeV2")
Set colItems = objWMIService.ExecQuery _
("Select * from Exchange_Mailbox")
strComputerSQL = "SQLSERVER"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open _
"Provider=SQLOLEDB;Data Source=" & strComputerSQL & ";" & _
"Trusted_Connection=Yes;Initial Catalog=MailboxUsage"
For Each objItem in colItems
sql = "INSERT INTO TestTable (MailboxName, LastLogonTime) VALUES (MailboxDisplayName, LastLogonTime)"
objConnection.Execute(sql)
Next