powershell - Powershell在ScriptBlock中传递参数

标签 powershell invoke-command scriptblock

我正在尝试从远程服务器获取文件的最后写入时间。

这不起作用:

$server = "MyServerName"

$lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\$args[0]\hot.war" } -argumentlist $server | select -Property LastWriteTime

这确实有效:
 $lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\MyServerName\hot.war" } -argumentlist $server | select -Property LastWriteTime

任何人都可以帮助使第一套作品生效吗?

最佳答案

换句话说,如果您使用的是PowerShell 3,则可以执行以下操作:

$lastWrite = Invoke-Command -Computername $server -ScriptBlock {
               Get-ChildItem "\\$using:server\hot.war"
             } | select -Property LastWriteTime

关于powershell - Powershell在ScriptBlock中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30537230/

相关文章:

powershell - 在脚本 block powershell 中调用函数

regex - 使用从正则表达式捕获的字符串来格式化日期时间

powershell - 如何在 powershell 中删除整行?

c# - 为什么从 C# 调用我的 powershell 函数失败?

powershell - 防止集成终端自动打开

powershell - 在后台作业中调用命令

powershell - 使用 PowerShell Invoke 命令安装软件

function - 如何在调用者的上下文中调用脚本 block ?