我正在嘗試將一些腳本變數傳遞給 PowerShell 中的 Invoke-Sqlcmd,如下所示:
$hello = "hello"
$params = "greeting=" $hello, "audience=world"
Invoke-Sqlcmd -Query "select '`$(greeting) `$(audience)'" -Variable $params
我收到以下錯誤:
用于為 Invoke-Sqlcmd cmdlet 定義新變數的格式無效。請使用“var=value”格式來定義新變數。
但是如果我洗掉$hello并使用文字,我就成功了:
$params = "greeting=hello", "audience=world"
.GetType()的兩個版本都回傳相同的內容$params,所以我不確定問題是什么。
uj5u.com熱心網友回復:
在您的第一個示例中,變數$params被設定為string:
$hello = "hello"
$params = "greeting=" $hello, "audience=world"
$params.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS /> $params
greeting=hello audience=world
除非你告訴 PowerShell 你想要一個object[]作為你操作的結果。即:圍繞連接操作( ):
$params = ("greeting=" $hello), "audience=world"
$params.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS /> $params
greeting=hello
audience=world
或者使用array sub-expression operator例如:
$params = @(
"greeting=" $hello
"audience=world"
)
有關這方面的官方檔案,請參閱about_Operator_Precedence。
$string = 'a'
$array = 'b','c'
PS /> ($string $array).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS /> $string $array
ab c
PS /> ($array $string).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS /> $array $string
b
c
a
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/345221.html
上一篇:PowerShell-如何從PowerShell獲得我的exe程式的記憶體消耗峰值
下一篇:哈希集到鍵和值
