0

I'm trying to concatenate a variable with a value from an array, and having problems.

The output I get is the variable value, plus the ENTIRE array, not just the desired value (index of $i).

$Database = "Checklist.dbo."

$ExtractTables = @("Page"
    , "HotelOwner"
    , "Hotel"
)

for ($i=0; $i -le $ExtractTables.Length – 1; $i++)  {

    write-host $Database$ExtractTables[$i]     # <<<<<<< takes ENTIRE array
}

My ultimate goal is to call an executable (bcp) something like this:

# & bcp $Database$ExtractTables[$i] out $OutputDirectory$ExtractTables[$i].txt -c -T -SCHELDEV02

Any pointers for a newbie?

Thanks!

2 Answers 2

1
$Database = "Checklist.dbo."

$ExtractTables = @("Page"
    , "HotelOwner"
    , "Hotel"
)

for ($i=0; $i -le $ExtractTables.Length – 1; $i++)  {
    write-host "$Database$($ExtractTables[$i])"
}

To evaluate a sub-expression before the rest of the expression put it inside $()

Sign up to request clarification or add additional context in comments.

Comments

0
$Database = "Checklist.dbo."

$ExtractTables = @("Page"
    , "HotelOwner"
    , "Hotel"
) 

$ExtractTables | ForEach {
    write-host "$Database$_"
}

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.