Geo*_*uer 4 powershell powershell-remoting
我的电脑上有一个简单的功能
MYPC> $txt = "Testy McTesterson"
MYPC> function Do-Stuff($file) {
cd c:\temp;
$txt > $file;
}
Run Code Online (Sandbox Code Playgroud)
我想在远程计算机上运行它
MYPC> Invoke-Command -ComputerName OTHERPC { Do-Stuff "test.txt" }
Run Code Online (Sandbox Code Playgroud)
可以理解的是,Do-Stuff在OTHERPC上不存在,这不起作用.我怎么能让它工作呢?该Do-Stuff函数抽象了一些基本代码,并在其他几个地方调用,所以我不想复制它.
请注意,在我的示例中,值通过参数和范围闭包传递给函数.那可能吗?
我不知道你如何使用闭包值,但你可以像这样传递两个参数:
$txt = "Testy McTesterson"
$file = "SomeFile.txt"
function Do-Stuff { param($txt,$file) cd c:\temp; $txt > $file }
Invoke-Command -ComputerName SomeComputer -ScriptBlock ${function:Do-Stuff} -ArgumentList $txt, $file
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10627 次 |
| 最近记录: |