首页 > 解决方案 > 远程调用命令 - 未找到函数/cmdlet

问题描述

我有以下脚本:

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函数可以正常工作,但我猜它与不在远程服务器上的函数有关。

请问我该如何解决?

标签: powershell

解决方案


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

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'
}

推荐阅读