0
$info = Invoke-SQLCmd -ServerInstance $Server -Database $Database -inputfile "$queriespath/compare_quantity.sql"
$changes = $info.length
for($i=0; $i-le$changes; $i++) {
    $text = "Revise,$info[$i].quantity,$info[$i].itemid" | Set-Content      'C:\Users\pmaho\Dropbox\SAMPOWERSPORTS\CustomChrome\revision.txt'
}

I'm trying to write the value of $info[$i].quantity,$info[$i].itemid to the file.

This is what my code outputs now Revise,System.Data.DataRow System.Data.DataRow[2].quantity,System.Data.DataRow System.Data.DataRow[2].itemid

I want it to say the actual value of the varables how can I do this?

EDIT:

for($i=0; $i-le$changes; $i++) {
$text = $($info[$i].quantity) | Set-Content 'C:\Users\pmaho\Dropbox\SAMPOWERSPORTS\CustomChrome\revision.txt'
}

The above is printing nothing.

Patrick Mahoney
  • 95
  • 1
  • 3
  • 9

2 Answers2

0
for($i=0; $i-le$changes; $i++) {
    $text = "$(Revise,$info[$i].quantity,$info[$i].itemid)" | Set-Content      'C:\Users\pmaho\Dropbox\SAMPOWERSPORTS\CustomChrome\revision.txt'
}

The $() just tells powershell to interpret the expression contained in the parentheses, you still need the quotes to assign a string value to the variable.

Eric Walker
  • 1,042
  • 10
  • 15
0

You're running into an issue of String Expansion here.

Take this property, for instance:

$e = [psobject]@{Name='StackOverFlow';URL='http://stackoverflow.com'}
$e.URL
> http://stackoverflow.com

This works as expect. However, when I try to wrap the whole thing in quotes, look what happens:

"the URL of StackOverflow is $e.Url"
>the URL of StackOverflow is System.Collections.Hashtable.Url

Notice the lame 'lil .Url appended to the end there? PowerShell is doing String expansion for me, but it does this by scanning left to right and dropping in variable values for us, but it doesn't check to see if I'm really after a property or method name. It just thinks I see $e, let me replace it with what I've got for $e. Annoying!

So the easy way around this is to use the Order of Operation symbols (remember Please-Excuse-My-Dear-Aunt-Sally? Parenthesis, Exponent, Multiplication, Division, Addition, Subtraction) and just wrap the property references in parenthesis.

In my example then, I'd do this.

"the URL of StackOverflow is $($e.Url)"
> the URL of StackOverflow is http://stackoverflow.com

Going back to your question, try this instead:

"Revise,$($info[$i].quantity),$($info[$i].itemid)" |
     Set-Content 'C:\Users\pmaho\Dropbox\SAMPOWERSPORTS\CustomChrome\revision.txt'
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48