Jack
Jack

Reputation: 23

PowerShell variables not being expanded in string?

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 ...

Upvotes: 1

Views: 348

Answers (2)

Snak3d0c
Snak3d0c

Reputation: 626

.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.

Upvotes: -2

G42
G42

Reputation: 10019

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

Upvotes: 5

Related Questions