Solved

Powershell trouble connecting to Access table

Posted on 2012-03-14
7
1,048 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 10

Expert Comment

by:JoeKlimis
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 10

Expert Comment

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

Are you using user level security or DB password ?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 22

Accepted Solution

by:
Nico Bontenbal earned 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now