使用扩展变量创建脚本块
Create scriptblocks with expanded variables
我知道 and here 之前有人问过这个问题。但出于某种原因,它对我们来说并不适用。
想要的结果
2 个可与 Pester 参数 ParameterFilter
一起使用的脚本块,其中仅扩展了变量 $testTable
:
{
($Query -like "*SELECT * FROM tableOne*
WHERE TicketNr IS NOT NULL AND TicketCloseDate IS NULL*")
}
{
($Query -like "*SELECT * FROM tableWto*
WHERE TicketNr IS NOT NULL AND TicketCloseDate IS NULL*")
}
下面的代码不起作用,因为我们需要转义 $testQuery
中的所有内容。有没有办法只扩展 $testTable
而不是其他部分?
foreach ($testTable in @('tableOne', 'tableTwo')) {
$testQuery = "*SELECT * FROM $testTable*
WHERE TicketNr IS NOT NULL AND TicketCloseDate IS NULL*"
[ScriptBlock]::create("`$Query `-like $testQuery")
}
我想我明白了:
foreach ($testTable in @('tableOne', 'tableTwo')) {
[ScriptBlock]::create(@"
(`$Query -like "*SELECT * FROM $testTable*
WHERE TicketNr IS NOT NULL AND TicketCloseDate IS NULL*" )
"@)
}
我知道
想要的结果
2 个可与 Pester 参数 ParameterFilter
一起使用的脚本块,其中仅扩展了变量 $testTable
:
{
($Query -like "*SELECT * FROM tableOne*
WHERE TicketNr IS NOT NULL AND TicketCloseDate IS NULL*")
}
{
($Query -like "*SELECT * FROM tableWto*
WHERE TicketNr IS NOT NULL AND TicketCloseDate IS NULL*")
}
下面的代码不起作用,因为我们需要转义 $testQuery
中的所有内容。有没有办法只扩展 $testTable
而不是其他部分?
foreach ($testTable in @('tableOne', 'tableTwo')) {
$testQuery = "*SELECT * FROM $testTable*
WHERE TicketNr IS NOT NULL AND TicketCloseDate IS NULL*"
[ScriptBlock]::create("`$Query `-like $testQuery")
}
我想我明白了:
foreach ($testTable in @('tableOne', 'tableTwo')) {
[ScriptBlock]::create(@"
(`$Query -like "*SELECT * FROM $testTable*
WHERE TicketNr IS NOT NULL AND TicketCloseDate IS NULL*" )
"@)
}