1

I have written some PowerShell

param (
    [Parameter(Mandatory=$true)][string]$company,
    [Parameter(Mandatory=$true)][string]$output
)

...

$objRecordset.Open("Select Col1, Col2, Col3 From $company_Table1.DBF", $objConnection,$adOpenStatic,$adLockOptimistic)

I am running it using

.\Test.ps1 -company A -output C:\test.txt

but for some reason the $company variable isn't being expanded even though it's in "quotes"?

Exception calling "Open" with "4" argument(s): "File '.dbf' does not exist."
At line:17 char:1
+ $objRecordset.Open("Select Col1, Col2, Col3 From $company_Table1. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

When I hardcode it as A_Table1.dbf it works fine ...

1

2 Answers 2

5

The error message it telling you that PowerShell is parsing $company_Table1 as the name of the variable; _ is not acting as a delimiter.

You need to tell PowerShell where the variable name being and ends using curl braces {}

${company}_Table1.DBF
Sign up to request clarification or add additional context in comments.

4 Comments

Of course, jeez. Thanks. I will accept once it allows me to.
I would imagine ISE would have displayed $company_Table1 as a single variable. Hard to miss that.
also a common practice is to wrap the variable into a new block like this "Select Col1, Col2, Col3 From $($company)_Table1.DBF"
@RohinSidharth Who mentioned ISE.
-2

.DBF stands for database file. You are trying to do a select on the database and not on a table in that database. So it makes sense this isn't working.

it should be Select * from TBL_Whatever LIMIT 0,1 ....

So you first need to open a connection to the DB file itself and then do your select against a TABLE in that DB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.