[英]Remote Invoke-Command - function/cmdlet not found
I've got the following script: 我有以下脚本:
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'
}
That I'm trying to invoke on a remote server like so (from powershell): 我试图在远程服务器上这样调用(从powershell):
.\myscript.ps1 server1,server2,server3
The script is executing, but the issue is I get an error relating to the Write-Info
function like so: 脚本正在执行,但是问题是我收到与Write-Info
函数有关的错误,如下所示:
The term 'Write-Info' is not recognised as the name of a cmdlet, function, script file, or operable program
The Introspect
function works fine if I embed the function inside but I guess it relates to the function not being on the remote server. 如果我将Introspect
函数嵌入其中,则该函数正常工作,但我想它与该函数不在远程服务器上有关。
How can I solve this please? 请问该如何解决?
Managed to solve this by embedding the 'missing' function inside the working one: 通过将'missing'函数嵌入到工作中来解决此问题:
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'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.