Solved

Trying to do a SQL insert  from powershell to a MSAccess database

Posted on 2013-12-16
1
818 Views
Last Modified: 2014-01-06
Hi,

I am trying to do a SQL insert into MSAccess database and coming up with an error in powershell.

Do u have any suggestions on how I can fix the code below:

    clear
    $db = New-Object -com "ADODB.Connection"
    
    $db.Open("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ad\VP.accdb")    

    $sql = "insert into vp_debug (Machine_Name) Values ('Test')"
    
    $test_cmd = New-Object System.Data.OleDb.OleDbCommand


    $test_cmd.Connection = $db
    $test_cmd.CommandText = $sql

    $test_cmd.ExecuteNonQuery()

Open in new window


The error I get is:

Exception setting "Connection": "Cannot convert the "System.__ComObject" value of type "System.__ComObject#{00001550-0000-0010-8000-00aa006d2ea4}" to type "System.Data.OleDb.OleDbConnection"."
At C:\ad\Untitled1.ps1:11 char:5
+     $test_cmd.Connection = $db
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting
 
Exception calling "ExecuteNonQuery" with "0" argument(s): "ExecuteNonQuery: Connection property has not been initialized."
At C:\ad\Untitled1.ps1:14 char:5
+     $test_cmd.ExecuteNonQuery()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException
0
Comment
Question by:whorsfall
1 Comment
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39721154
Try this - I don't think your connection definition is quite right:

$cn = New-Object data.OleDb.OleDbConnection "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ad\VP.accdb"
$sql = "insert into vp_debug (Machine_Name) Values ('Test')"
    
$testcmd = New-Object System.Data.OleDb.OleDbCommand $sql
$testcmd.connection = $cn
$testcmd.ExecuteNonQuery()

Open in new window

0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Set OWA language and time zone in Exchange for individuals, all users or per database.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question