如何将局部变量传递给远程“调用命令"? [英] How do I pass a local variable to a remote `Invoke-Command`?
本文介绍了如何将局部变量传递给远程“调用命令"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 Invoke-Command
检索位于远程服务器上的文件的哈希值.当我提供如下完整路径时,它工作正常:
I'm trying to retrieve the hash of a file located on remote server using Invoke-Command
. It works fine when I give the full path as below:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash -Path E:\test\testfile.zip -Algorithm SHA1
}
但我需要通过如下变量传递文件名:
But I need to pass the file name via a variable as below:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash -Path "E:\test\$dest.zip" -Algorithm SHA1
}
如何在远程会话的 scriptblock
中访问该变量?
How do I access this variable in the scriptblock
of a remote session?
推荐答案
在 PowerShell 4(实际上 3+)中,最简单的方法是使用 使用
范围修饰符:
In PowerShell 4 (3+ actually) the easiest way is to use the Using
scope modifier:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:\test\$Using:dest.zip -Algorithm SHA1
}
对于适用于所有版本的解决方案:
For a solution that works with all versions:
Invoke-Command -ComputerName winserver -ScriptBlock {
param($myDest)
Get-FileHash E:\test\$myDest.zip -Algorithm SHA1
} -ArgumentList $dest
这篇关于如何将局部变量传递给远程“调用命令"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文