Powershell trouble connecting to Access table

Posted on 2012-03-14
Medium Priority
Last Modified: 2012-03-16
I'm having trouble connecting to a Table held in a Microsoft Access DB by using Powershell as the language to connect. The issue comes into play by the uid and pwd commands. This is what is causing the issue. If I were to issue the command without the uid and pwd commands, it works successfully but requires that you manually put in the username and password through a prompt which pops up. Problem being is that once this is done I want the script to run as a scheduled task in the middle of the night when no one will be here to type in the username and password.

What am I doing wrong to pass these credentials to the program?

## Create our object to access Access Table
$dataSource = "<.mdb file location>"
$strQuery = "SELECT * FROM table"
$dsn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$dataSource;uid=<username>;pwd=<password>;"

## create connection object and open the database
$objConn = New-Object System.Data.OleDb.OleDbConnection $dsn
$objCmd  = New-Object System.Data.OleDb.OleDbCommand $strQuery,$objConn

## get query results, populate data-adapter, close connection
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter $objCmd
$dataset = New-Object System.Data.DataSet
[void] $adapter.Fill($dataSet)

Open in new window

After that I just use the $dataset.Tables[0].Rows to retrieve data. But besides the point. As I stated earlier, the code works fine if the uid and pwd commands are not used.

Here's the error message when they are used:

Exception calling "Open" with "0" argument(s): "Could not find installable ISAM."
At <script location>:56 char:14
+ $objConn.Open <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
Exception calling "Fill" with "1" argument(s): "Could not find installable ISAM."
At <script location>:61 char:21
+ [void] $adapter.Fill <<<< ($dataSet)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Open in new window

An important factor to consider: This Access DB is using linked tables to a Pervasive DB. So the credentials are not to authenticate to Microsoft Access Table/DB, but rather to access the DB on the other server.

***Do not ask why it is done this way, nor suggest that the connection be directly to the Database rather than to a linked file. This was give careful consideration previously.***
Question by:Alex_MPM
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 11

Expert Comment

by:Joe Klimis
ID: 37724672
a working example  has been posted here, and the artical contains links to a few good sources.


Let me know if this helps


ps   why it is done this way,? thought of  connecting  directly   :-o

Author Comment

ID: 37724748
Problem being with that post is that it does not pass any credentials. And again, the problem is not with the script, it works just fine but asks you for credentials each time. So I checked the links, and they are things I've already tried like "Integrated Security=SSPI", and using the uid= and pwd= commands. Nothing seems to want to pass credentials to this.
LVL 11

Expert Comment

by:Joe Klimis
ID: 37724968
which version of access are you using ?

Are you using user level security or DB password ?
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

LVL 22

Accepted Solution

Nico Bontenbal earned 2000 total points
ID: 37725055
You need to store the credentials with the linked table in Access itself. See step 6 in the section "From SQL or another ODBC data source" of this page: http://office.microsoft.com/en-us/access-help/import-or-link-data-and-objects-HP005187610.aspx

Author Closing Comment

ID: 37729393
That sucks, because it is definitely not a secure way of accessing the data. But it does do the job. In this case the data isn't crucial anyways, but I would not recommend doing this for any kind of accounting DB or anything else with sensitive data.
LVL 22

Expert Comment

by:Nico Bontenbal
ID: 37729419
Having the pwd in a script is not secure either. Just make sure the database is in a secure location. If you schedule it at night I assume you'll use a server for that. Just put the file on a local drive on the server, not on a network share.

Author Comment

ID: 37729426
Good call. Thanks for your feedback.

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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