?
Solved

Powershell trouble connecting to Access table

Posted on 2012-03-14
7
Medium Priority
?
1,154 Views
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
$objConn.Open()

## 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)
$objConn.Close()

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.***
0
Comment
Question by:Alex_MPM
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

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

http://skatterbrainz.blogspot.com/2009/05/using-powershell-with-ms-access.html

Let me know if this helps


Joe

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

Author Comment

by:Alex_MPM
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.
0
 
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 ?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 22

Accepted Solution

by:
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
0
 

Author Closing Comment

by:Alex_MPM
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.
0
 
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.
0
 

Author Comment

by:Alex_MPM
ID: 37729426
Good call. Thanks for your feedback.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
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…

807 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