2016-08-02 115 views
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' 
} 

我试图将$ info [$ i] .quantity,$ info [$ i] .itemid的值写入文件。将变量写入文件

这是我的代码输出现在 修改,的System.Data.DataRow的System.Data.DataRow [2] .quantity,的System.Data.DataRow的System.Data.DataRow [2] .itemid

我想要说出可变物的实际价值,我该怎么做?

编辑:

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

上面是打印什么。

+2

的可能的复制[?你怎么能在一个字符串中使用一个对象的属性(http://stackoverflow.com/questions/ 1145704 /如何使用一个对象属性在一个字符串) – briantist

+0

该帖子没有帮助。我正在尝试使用括号,并且无法正确识别 –

+0

通过将它放入问题中告诉我们你在做什么。 – briantist

回答

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' 
} 

的$()只是告诉PowerShell来解释包含在括号中的表达式,你仍然需要引号指定一个字符串值的变量。

0

您在这里遇到了字符串扩展问题。

就拿这个属性,例如:

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

这个工程作为预期。然而,当我尝试用引号括整个事情,看看会发生什么:

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

通知追加到年底有瘸腿“律.Url? PowerShell为我做了字符串扩展,但它通过从左到右扫描并为我们放入变量值来实现这一点,但它不检查是否真的在属性或方法名称之后。它只是认为I see $e, let me replace it with what I've got for $e。烦!

所以解决这个问题的简单方法是使用运算符号的顺序(记得请-借口 - 我 - 亲爱的姨妈,萨莉?括号,指数,乘法,除法,加法,减法),只是包裹属性引用括号内。

在我的例子中,我会这样做。

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

再回到你的问题,试试这个来代替:

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