我試圖弄清楚如何在字串中使用未宣告的變數。這是一個簡化的示例來演示我的問題
[string]$sentence="My dog is $age years old"
[int]$age = 6
$sentence | write-output
$age = 3
$sentence | write-output
尋找這個輸出:
My dog is 6 years old
My dog is 3 years old
得到這個輸出:
My dog is years old
My dog is years old
我嘗試了以下方法:
"My dog is `$age years old" #My dog is $age years old
$("My dog is `$age years old") #My dog is $age years old
$("My dog is $age years old") #My dog is 3 years old
有沒有可能以任何方式使它更緊湊?還是這是唯一的方法?
編輯:我注意到重點是字串。我將在有問題的地方分享我的代碼。我需要將一堆從 20 行到 2000 行的 VBS 腳本(>40)遷移到 Powershell
VBS 檔案:
Function InitializeApp
Dim sPath
...
End function
Powershell腳本:
$vbs=Get-Content -path $path #get content in array, one line per index
$rules=
@(
[pscustomobject]@{
"name"="func_start";
"vbs" = 'Function (?<V1>\w*)'; # The regex named group V1 will initialized when match is found
"ps" = {"Function $1() `{`n"} # Group V1 is to be inserted in replacement string
}
)
function Invoke-TestAndReplace($line,$ruleName){
$r_vbs=$($rules.where({$_.name -eq $ruleName}).vbs) # Function (?<V1>\w*)
$r_ps=$($rules.where({$_.name -eq $ruleName}).ps) # {"Function $1() `{`n"}
if( $regex = ($line | select-string -pattern $r_vbs )) {
$0=$regex[0].Matches.Groups.Where({$_.name -eq "0"}).value # Function InitializeApp
$1=$regex[0].Matches.Groups.Where({$_.name -eq "V1"}).value # InitializeApp
$2=$regex[0].Matches.Groups.Where({$_.name -eq "V2"}).value #
$r_ps = & $r_ps # Function InitializeApp() {
$replace = $line.replace($0,$r_ps) # Function InitializeApp -> Function InitializeApp() {
} else {$replace = $line} #keep old value
return $replace
}
$newVBS=@()
foreach($line in $vbs){
$line = Invoke-TestAndReplace -line $line -rulename "func_start"
}
uj5u.com熱心網友回復:
我不知道如何使用插值來做到這一點,但我可以使用較舊的 .NetString.Format()
方法來做到這一點,如下所示:
$sentence = "My dog is {0} years old."
$age = 6
[string]::Format($sentence, $age) | Write-Output
# => My dog is 6 years old
$age = 3
[string]::Format($sentence, $age) | Write-Output
# => My dog is 3 years old
[string]::Format($sentence, 12) | Write-Output
# => My dog is 12 years old
我們可以這樣縮短(感謝評論者):
$sentence = "My dog is {0} years old."
$age = 6
Write-Output ($sentence -f $age)
# => My dog is 6 years old
$age = 3
Write-Output ($sentence -f $age)
# => My dog is 3 years old
Write-Output ($sentence -f 12)
# => My dog is 12 years old
還要注意模板字串中的不同占位符。
uj5u.com熱心網友回復:
這個答案顯示了Joel Coehoorn的好答案的一個過于復雜的替代方案。您可以使用Script Block來存盤運算式(包括您的$age
變數)然后執行它。
$sentence = { "My dog is $age years old" }
$age = 6
& $sentence | write-output # => My dog is 6 years old
$age = 3
& $sentence | write-output # => My dog is 3 years old
如果您希望腳本塊在創建變數時“記住”變數的值,您可以使用它的.GetNewClosure()
方法,例如:
$expressions = foreach($i in 0..5) {
{ "Value of `$i was $i in this iteration" }.GetNewClosure()
}
$expressions.foreach{ & $_ }
uj5u.com熱心網友回復:
您可以使用ExpandString
$sentence = 'My dog is $age years old.'
$age = 4
$ExecutionContext.InvokeCommand.ExpandString($sentence)
# My dog is 4 years old.
$age = 5
$ExecutionContext.InvokeCommand.ExpandString($sentence)
# My dog is 5 years old.
您還可以定義一個函式并呼叫該函式
function sentence([int]$age) {
"My dog is $age years old"
}
1..5 | ForEach-Object { sentence -age $_ }
# My dog is 1 years old
# My dog is 2 years old
# My dog is 3 years old
# My dog is 4 years old
# My dog is 5 years old
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/469301.html
下一篇:為什么變數在選擇查詢中不起作用?