使用PowerShell的Invoke-Command进行远程处理时,如何包含本地定义的函数?

时间:2012-07-06 17:58:22

标签: powershell powershell-v2.0 powershell-remoting

我觉得我错过了一些显而易见的东西,但我无法弄清楚如何做到这一点。

我有一个ps1脚本,其中定义了一个函数。它调用该函数,然后尝试远程使用它:

function foo
{
    Param([string]$x)

    Write-Output $x
}

foo "Hi!"

Invoke-Command -ScriptBlock { foo "Bye!" } -ComputerName someserver.example.com -Credential someuser@example.com

这个简短的示例脚本打印出“嗨!”然后崩溃说“术语'foo'不被识别为cmdlet,函数,脚本文件或可操作程序的名称。”

据我所知,该功能未在远程服务器上定义,因为它不在ScriptBlock中。我可以在那里重新定义它,但我不愿意。我想定义一次该函数并在本地或远程使用它。有没有办法做到这一点?

4 个答案:

答案 0 :(得分:32)

您需要传递函数本身(而不是调用ScriptBlock中的函数。)

上周我有同样的需求并找到this SO discussion

所以你的代码将成为:

Invoke-Command -ScriptBlock ${function:foo} -argumentlist "Bye!" -ComputerName someserver.example.com -Credential someuser@example.com

请注意,通过使用此方法,您只能在位置上将参数传递到函数中;您无法在本地运行该函数时使用命名参数。

答案 1 :(得分:25)

您可以将函数的定义作为参数传递,然后通过创建脚本块然后对其进行点源来重新定义远程服务器上的函数:

$fooDef = "function foo { ${function:foo} }"

Invoke-Command -ArgumentList $fooDef -ComputerName someserver.example.com -ScriptBlock {
    Param( $fooDef )

    . ([ScriptBlock]::Create($fooDef))

    Write-Host "You can call the function as often as you like:"
    foo "Bye"
    foo "Adieu!"
}

这样就无需复制您的函数副本。如果您愿意,也可以通过这种方式传递多个功能:

$allFunctionDefs = "function foo { ${function:foo} }; function bar { ${function:bar} }"

答案 2 :(得分:5)

您还可以将函数和脚本放在文件(foo.ps1)中,并使用FilePath参数将其传递给Invoke-Command:

Invoke-Command –ComputerName server –FilePath .\foo.ps1

该文件将被复制到远程计算机并执行。

答案 3 :(得分:0)

虽然这是一个老问题,但我想添加我的解决方案。

在函数测试中,脚本块的参数列表很有趣,不会使用[scriptblock]类型的参数,因此需要转换。

Function Write-Log 
{
    param(
        [string]$Message
    )

    Write-Host -ForegroundColor Yellow "$($env:computername): $Message"
}

Function Test
{
    $sb = {
        param(
            [String]$FunctionCall
        )

        [Scriptblock]$WriteLog = [Scriptblock]::Create($FunctionCall) 
        $WriteLog.Invoke("There goes my message...")               
    }

    # Get function stack and convert to type scriptblock 
    [scriptblock]$writelog = (Get-Item "Function:Write-Log").ScriptBlock 

    # Invoke command and pass function in scriptblock form as argument 
    Invoke-Command -ComputerName SomeHost -ScriptBlock $sb -ArgumentList $writelog
}

Test

另一个可能性是将哈希表传递给我们的scriptblock,其中包含您希望在远程会话中可用的所有方法:

Function Build-FunctionStack 
{
    param([ref]$dict, [string]$FunctionName)

    ($dict.Value).Add((Get-Item "Function:${FunctionName}").Name, (Get-Item "Function:${FunctionName}").Scriptblock)
}

Function MyFunctionA 
{
    param([string]$SomeValue)

    Write-Host $SomeValue
}

Function MyFunctionB
{
    param([int]$Foo)

    Write-Host $Foo
}

$functionStack = @{}

Build-FunctionStack -dict ([ref]$functionStack) -FunctionName "MyFunctionA"
Build-FunctionStack -dict ([ref]$functionStack) -FunctionName "MyFunctionB" 

Function ExecuteSomethingRemote
{
    $sb = {
        param([Hashtable]$FunctionStack)

        ([Scriptblock]::Create($functionStack["MyFunctionA"])).Invoke("Here goes my message");
        ([Scriptblock]::Create($functionStack["MyFunctionB"])).Invoke(1234);

    }

    Invoke-Command -ComputerName SomeHost -ScriptBlock $sb -ArgumentList $functionStack
}

ExecuteSomethingRemote