powershell - 远程调用命令 - 未找到函数/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
函数可以正常工作,但我猜它与不在远程服务器上的函数有关。
请问我该如何解决?
解决方案
设法通过在工作中嵌入“缺失”功能来解决这个问题:
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'
}
推荐阅读
- xquery - 如何在单行输出中获取 MarkLogic 中 XQuery 的输出?
- numpy - 如何摆脱contourplot contourf(平滑矩阵/二维数组)中的伪影?
- pandas - pandas groupby 并填补缺失的频率
- django - Azure 容器实例:从 Django+Nginx+Postgres 创建多容器组
- r - 通过循环从数据框中填充矩阵
- java - 有没有办法用另一种方法用一种方法对数组中的所有数字求和?
- php - Codeigniter 从控制器发送数据到视图
- python - 构建 TensorFlow 错误“找不到 bazel。请安装 bazel”
- php - 在php中查找两个时隙之间的记录
- ios - 使用 SinchRTC - iOS 13 向 CallKit 报告新来电