Sunday, August 23, 2009

Accessing MS Access from PowerShell

I was trying to modify some scripts on MS Technet Script Guy's blog for working with PowerShell and MS Access.

Kept getting an error when trying to open the database, the example was using an .mdb file extension. I left my test file with the Office 2007 default of .accdb.

The example had the filename and path in a variable thought that was the problem at first then realized the connection string seemed to be referring to version 4.

I found that the ADO string need to be "Microsoft.ACE.OLEDB.12.0" rather then "Microsoft.Jet.OLEDB.4.0"

Example of the error
PS C:\code\examples-powershell\msaccess> $cn.Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = C:\code\examples-pow
Exception calling "Open" with "1" argument(s): "Unrecognized database format 'C:\code\examples-powershell\msaccess\msac
At line:1 char:9
+ $cn.Open <<<< ("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = C:\code\examples-powershell\msaccess\msaccessdatadb1
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

PS C:\code\examples-powershell\msaccess>


2008 Winter Scripting Games - Event 9 - MS Access PowerShell
The file I was working with - ScriptingGamesBeginnerEvent4.ps1

Other reference:
How to find a record using ADO and Jet OLE DB provider