我写了以下代码:
cls
function GetFoo() {
function GetBar() {
$bar = "bar"
$bar
}
$foo = "foo"
$bar = GetBar
$foo
$bar
}
$cred = Get-Credential "firmwide\srabhi_adm"
$result = Invoke-Command -Credential $cred -ComputerName localhost
-ScriptBlock ${function:GetFoo}
Write-Host $result[0]
Write-Host $result[1]
它有效,但我不想GetBar
在GetFoo
.
我可以做这样的事情吗?
cls
function GetBar() {
$bar = "bar"
$bar
}
function GetFoo() {
$foo = "foo"
$bar = GetBar
$foo
$bar
}
$cred = Get-Credential "firmwide\srabhi_adm"
$result = Invoke-Command -Credential $cred -ComputerName localhost
-ScriptBlock ${function:GetFoo; function:GetBar; call GetFoo}
Write-Host $result[0]
Write-Host $result[1]
基本上我有选择地将我想要的函数放在 ScriptBlock 中,然后调用其中一个。这样我就不必在函数内部定义函数,我可以通过注入我想成为该 ScriptBlock 一部分的函数来构造 ScriptBlock。