我在powershell里面有下面的caml。如果我在CAML查询中硬编码$month,那么它就能工作。我使用的语法正确吗?
write-host $month
$CAML = '<Where>
<Eq>
<FieldRef Name="Period" />
<Value Type="Text">$month</Value>
</Eq>
</Where>'发布于 2016-05-02 17:47:41
在PowerShell中,字符串要么是可扩展的(就像perl中的内插字符串),要么是文字。
双引号(")中包含的任何内容都是可扩展的,而单引号(')则用于字符串文本,就像在您的例子中一样。
$month = 'Jan'
"First month is $month" # This will result in: First month is Jan
'First month is $month' # This will result in: First month is $month对于多行字符串,请使用here-string (在其他语言中通常称为here-docs )。同样的规则适用于:
$CAML = @"
<Where>
<Eq>
<FieldRef Name="Period" />
<Value Type="Text">$month</Value>
</Eq>
</Where>
"@ 如果您想使用字符串文字(即。如果字符串包含要保留的其他特殊字符或文字$,但需要插入特定的变量值,则使用-f格式运算符as shown by @jisaak。
$CAML = @'
<Where>
<Eq>
<FieldRef Name="Period" />
<Value Type="Text">{0}</Value>
</Eq>
</Where>
'@ -f $month要了解有关字符串展开和引用的更多信息,请参见Get-Help about_Quoting_Rules
发布于 2016-05-02 17:41:08
您的变量不会被替换,因为您使用的是单引号。可以使用双引号,也可以使用格式字符串(别名-f):
write-host $month
$CAML = '<Where>
<Eq>
<FieldRef Name="Period" />
<Value Type="Text">{0}</Value>
</Eq>
</Where>' -f $month发布于 2016-05-02 17:49:41
谢谢,希萨克。只是想分享下面的代码,因为这一个也是工作的。
$CAML = "<Where>
<Eq>
<FieldRef Name='Period' />
<Value Type='Text'>$($month)</Value>
</Eq>
</Where>"https://stackoverflow.com/questions/36988233
复制相似问题