远程调用命令-找不到功能/ cmdlet

时间:2018-11-03 17:33:35

标签: powershell

我有以下脚本:

param(
    [string[]]$servers
)

function Write-Info($message) {
}

function Introspect($server) {
    Write-Info "about to do something on server"    
    // other powershell stuff that works
}

Foreach ($server in $servers) {
    Invoke-Command -ComputerName $server -ScriptBlock ${function:Introspect} -ArgumentList $server -credential 'MyUser'
}

我正在尝试在远程服务器上这样调用(从powershell):

.\myscript.ps1 server1,server2,server3

脚本正在执行,但问题是我遇到与Write-Info函数有关的错误,如下所示:

The term 'Write-Info' is not recognised as the name of a cmdlet, function, script file, or operable program

如果我将Introspect函数嵌入其中,但我猜想它与不在远程服务器上的函数有关。

请问该如何解决?

1 个答案:

答案 0 :(得分:0)

设法通过在工作中嵌入“缺失”功能来解决此问题:

param(
    [string[]]$servers
)

function Introspect($server) {

    function Write-Info($message) {
    }
    Write-Info "about to do something on server"    
    // other powershell stuff that works

}

Foreach ($server in $servers) {
    Invoke-Command -ComputerName $server -ScriptBlock ${function:Introspect} -ArgumentList $server -credential 'MyUser'
}