After experiencing a lot of down time, We decided to move this site to
CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.
Accessing MS Access files in Powershell
|
Author |
Message
|
sanket.shetye
-
Total Posts
:
2
- Scores: 0
-
Reward points
:
0
- Joined: 2/13/2007
-
Status: offline
|
Accessing MS Access files in Powershell
Tuesday, February 13, 2007 4:40 AM
( permalink)
Guys, I need help in accessing MS Access database files in Powershell, I am using the following code (Taken from scriptcenter and modified a bit) ------------------------- $adOpenStatic = 3 $adLockOptimistic = 3 $objConnection = New-Object -comobject ADODB.Connection $objRecordset = New-Object -comobject ADODB.Recordset $objConnection.Connectionstring = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = status.mdb" $objConnection.Open() $objRecordset.Open("Select * from app1", $objConnection,$adOpenStatic,$adLockOptimistic) $objRecordset.MoveFirst() do {$objRecordset.Fields.Item("counter").Value; $objRecordset.MoveNext()} until ($objRecordset.EOF -eq $True) $objRecordset.Close() $objConnection.Close() --------------------------- The error I am getting is as follows- Exception calling "Open" with "0" argument(s): "Could not find file 'C:\Documents and Settings\ssanket\status.mdb '." --------------------------- The location of the script is 'c:\scripts', my problem is when I give full path to the mdb file it works fine, but I dont want to do that, the script should accept only the filename, the script file and database file both are in the same folder. The script should ideally look into the same folder first for the database file in absense of a fully defined path, but that is not happening here, can someone tell me what I am doing wrong? Thanks a bunch!
"I thought I knew everything...Then came PowerShell..."
|
|
|
|
SAPIENScripter
-
Total Posts
:
283
- Scores: 2
-
Reward points
:
0
- Joined: 11/1/2006
- Location: SAPIEN Technologies
-
Status: offline
|
RE: Accessing MS Access files in Powershell
Wednesday, February 14, 2007 2:19 AM
( permalink)
I always specify the full path, even in VBScript. And while a vbscript version of your script doesn't require the path, obviously PowerShell is different. Remember, it is not a scripting engine like VBScript. It is an interactive shell with batch-file like capabilities. By that I mean you can put your commands in a ps1 file and execute them much the same way you build a batch file (conceptually) in CMD.exe. Powershell is more secure by design. For example, you have to explicity state the path of the script your want to run. I expect path access to the database file is the same way. I'm not saying it makes things easier for you, just that I suspect there's a security reason behind it.
|
|
|
|
SAPIENScripter
-
Total Posts
:
283
- Scores: 2
-
Reward points
:
0
- Joined: 11/1/2006
- Location: SAPIEN Technologies
-
Status: offline
|
RE: Accessing MS Access files in Powershell
Wednesday, February 14, 2007 2:26 AM
( permalink)
The more I play with this, the more I think the path issue is related to the COM object OLE DB provider. COM objects under PowerShell are not 100% foolproof and completely compatible. It may be that under PowerShell the provider is defaulting to a certain directory for the database unless you specify a complete path.
|
|
|
|
SAPIENScripter
-
Total Posts
:
283
- Scores: 2
-
Reward points
:
0
- Joined: 11/1/2006
- Location: SAPIEN Technologies
-
Status: offline
|
RE: Accessing MS Access files in Powershell
Wednesday, February 14, 2007 2:34 AM
( permalink)
Try this: $objConnection.Connectionstring = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = $pwd\status.mdb" I think you have to specify the path. Using $pwd at least doesn't force you to hard code a path.
|
|
|
|
sanket.shetye
-
Total Posts
:
2
- Scores: 0
-
Reward points
:
0
- Joined: 2/13/2007
-
Status: offline
|
RE: Accessing MS Access files in Powershell
Sunday, February 18, 2007 5:31 PM
( permalink)
Hey that works great! thanks mate
"I thought I knew everything...Then came PowerShell..."
|
|
|
|
Online Bookmarks Sharing: