Solved

Powershell trouble connecting to Access table

Posted on 2012-03-14
7
1,069 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views 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 Access…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

803 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