Zack
asked on
Powershell script to run a query against an MS Access database, getting unexpected token error
Heyas,
I have following code:
When I run this code I get the following error, see the attached file.
My question is how I do I modify the '$cn.Open' command to accept multiple tokens? I have to use the ADO classic method of connecting due to the age of the MS access database.
My reason for the connecting to the system database of the MS Access DB is described in the following article:
http://stackoverflow.com/questions/32843563/grant-read-permission-for-msysobjects
To summarize it's required to grant the valid permissions to run the query against the database via the PowerShell script.
Any assistance is appreciated.
I am using PowerShell version 3.
Thank you.
I have following code:
$path = “C:\test\alms.mdb”
$systempath "C:\test\almssys.mdw"
$adOpenStatic = 3
$adLockOptimistic = 3
$cn = new-object -comobject ADODB.Connection
$rs = new-object -comobject ADODB.Recordset
$cn.Open(“Provider = Microsoft.Jet.OLEDB.4.0;Data Source = $path", "Jet OLEDB:System database=$Systempath")
$rs.Open(“Update Userlogins Set LoggedIn = 0”,
$cn, $adOpenStatic, $adLockOptimistic)
$rs.MoveFirst()
Write-host “Success"
When I run this code I get the following error, see the attached file.
My question is how I do I modify the '$cn.Open' command to accept multiple tokens? I have to use the ADO classic method of connecting due to the age of the MS access database.
My reason for the connecting to the system database of the MS Access DB is described in the following article:
http://stackoverflow.com/questions/32843563/grant-read-permission-for-msysobjects
To summarize it's required to grant the valid permissions to run the query against the database via the PowerShell script.
Any assistance is appreciated.
I am using PowerShell version 3.
Thank you.
ASKER
Hi oBda,
Just tried your script I'm still getting the same error.
Any ideas?
Thank you.
Just tried your script I'm still getting the same error.
Any ideas?
Thank you.
"Same error" doesn't help at all since you didn't post the error in the first place.
ASKER
Hi oBda,
My apologies the script:
The error:
At C:\OpenAlmsMaxUserClear.ps 1:2 char:13
+ $systempath "C:\test\oa.mdw"
+ ~~~~~~~~~~~~~~~~
Unexpected token '"C:\test\oa.mdw"' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
The database names have changed from the initial question. But the same issue.
Thank you.
My apologies the script:
$path = "C:\test\oaserver.mdb"
$systempath "C:\test\oa.mdw"
$adOpenStatic = 3
$adLockOptimistic = 3
$cn = New-Object -ComObject ADODB.Connection
$rs = New-Object -ComObject ADODB.Recordset
$cn.Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = $path;Jet OLEDB:System database=$Systempath")
$rs.Open("Update Userlogins Set LoggedIn = 0",
$cn, $adOpenStatic, $adLockOptimistic)
$rs.MoveFirst()
Write-host "Success"
The error:
At C:\OpenAlmsMaxUserClear.ps
+ $systempath "C:\test\oa.mdw"
+ ~~~~~~~~~~~~~~~~
Unexpected token '"C:\test\oa.mdw"' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
The database names have changed from the initial question. But the same issue.
Thank you.
Missed that; just a "=" missing:
$path = "C:\test\alms.mdb"
$systempath = "C:\test\almssys.mdw"
$adOpenStatic = 3
$adLockOptimistic = 3
$cn = New-Object -ComObject ADODB.Connection
$rs = New-Object -ComObject ADODB.Recordset
$cn.Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = $path;Jet OLEDB:System database=$Systempath")
$rs.Open("Update Userlogins Set LoggedIn = 0",
$cn, $adOpenStatic, $adLockOptimistic)
$rs.MoveFirst()
Write-host "Success"
ASKER
Hi oBda,
One last query if I may, I realize this outside the bounds my initial query. How do I include a username/password in the connection string?
This in relation to following error that has occurred:
Exception calling "Open" with "1" argument(s): "Not a valid account name or password."
At C:\OpenAlmsMaxUserClear.ps 1:9 char:1
+ $cn.Open("Provider = Microsoft.Jet.OLEDB.4.0;Da ta Source = $path;Jet OLEDB:Syste ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : COMException
http://www.w3schools.com/asp/ado_ref_connection.asp isn't providing with many details.
Thank you.
One last query if I may, I realize this outside the bounds my initial query. How do I include a username/password in the connection string?
This in relation to following error that has occurred:
Exception calling "Open" with "1" argument(s): "Not a valid account name or password."
At C:\OpenAlmsMaxUserClear.ps
+ $cn.Open("Provider = Microsoft.Jet.OLEDB.4.0;Da
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : COMException
http://www.w3schools.com/asp/ado_ref_connection.asp isn't providing with many details.
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank very much for your help. ADO classic connections info is hard to find, guess it's legacy now days.
Cheers.
Cheers.
For connection strings look at www.connectionstrings.com, obviously.
ASKER
HI Qlemo,
Thank you for the site, bookmarking this one.
Thank you for the site, bookmarking this one.
Have a close look at the double quotes in your script above - several of them are "pretty" ones (<“> instead of <">), probably due to copying from a web site or using a Word processing software instead of a text editor to create your script.
The connection string should be just one single string, with the elements separated by ';'.
https://msdn.microsoft.com/en-us/library/ms254500(v=vs.110).aspx
Open in new window