【发布时间】:2022-01-04 16:26:31
【问题描述】:
我有一个 PowerShell 远程处理脚本,它的测试路径始终为 false。
$pathname = "C:\temp"
$hostremote = "computer122" #example
示例 - 不起作用:
Invoke-Command -ComputerName $hostremote {Test-Path -Path '$pathname' }
示例 - 工作正常:
Invoke-Command -ComputerName $hostremote {Test-Path -Path 'C:\temp' }
我尝试使用 " 并导致错误,因此将 ' 或 " 排除在外。必须是一个简单的 powershell 技巧,我因为这么简单的事情而错过了。
【问题讨论】:
-
试试
Invoke-Command -ComputerName $hostremote {Test-Path -Path $using:pathname }。Invoke-Command在远程范围内运行。局部变量在该范围内不可用。是的,不要在要扩展的变量周围使用单引号,除非那些单引号被外部双引号包围。 -
使用命令
help Invoke-Command -Full其中Example 9解释了using:的使用。
标签: powershell