0
我想在PowerShell中做一些值替換。我有一個包含一個通用查詢的文本文件,即使用DataSet替換文件文本PowerShell
-- query.sql
SELECT
'varTableName' AS myTableName
, COUNT(DISTINCT parmColumnName) AS myDistinctCount
, SUM(parmColumnName2) AS mySum
FROM varDatabaseName.varSchemaName.varTableName WITH (NOLOCK);
我試圖取代「VAR」和「PARM」的價值觀。我有兩個不同的datarows。在我的腳本中,我遍歷第一個數據行,並使用焦點行進行替換。這很好。我的問題是下一部分。然後我需要遍歷包含多行的第二個數據行,並對任何匹配的值執行替換。
我試過了,未果,做這樣的事情:
# myScript.ps1 -- does not work
# ...
# Code here to populate $MyDataRow
ForEach ($MyRow In $MyDataRow) {
[string]$Query = Get-Content query.sql | ForEach-Object {
$_ -replace "varTableName", $Table `
-replace "varDatabaseName", $MyRow.DatabaseName `
-replace "varSchemaName", $MyRow.SchemaName `
-replace "varTableName", $MyRow.TableName
-replace $MyOtherDataRow.SearchString, $MyOtherDataRow.ReplaceString
}
}
然而,這工作:
# myScript.ps1 -- works
# ...
# Code here to populate $MyDataRow
ForEach ($MyRow In $MyDataRow) {
[string]$Query = Get-Content query.sql | ForEach-Object {
$_ -replace "varTableName", $Table `
-replace "varDatabaseName", $MyRow.DatabaseName `
-replace "varSchemaName", $MyRow.SchemaName `
-replace "varTableName", $MyRow.TableName
}
ForEach($MyOtherRow In $MyOtherDataRow) {
$Query = $Query | ForEach-Object {
$_ -replace $MyOtherRow.SearchString, $MyOtherRow.ReplaceString
}
}
}
我只是學習PowerShell的,雖然如此,我不知道如果這是處理這種情況的最有效的方法。我想知道如果我可以管這第二個ForEach取代第一個結果?什麼是最好的方法?
哦,如果它是相關的,我使用PowerShell 3.0。
任何輸入表示讚賞。 :)
謝謝安斯加爾!我感謝你的迴應。 :) – sqlfool 2013-03-08 23:01:00