且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Try Catch 命令远程执行 Powershell 命令,然后在失败时使用本机命令回退?

更新时间:2023-08-20 21:14:40

运行时 Invoke-Command 针对远程会话,局部变量 未在远程会话中定义.您要么需要将这些变量作为参数传递给脚本块,要么使用 using: 范围 修饰符.语法是$using:variable.

When running Invoke-Command against remote sessions, local variables from the calling session are not defined in the remote sessions. You either need to pass in those variables as arguments to a script block or use the using: scope modifier. The syntax is $using:variable.

在您的情况下,局部变量是 $_.所以你需要使用 $using:_.

In your case, the local variable is $_. So you would need to use $using:_.

Write-Host "Processing Server ... $($using:_) using DFSR Powershell Module" -ForegroundColor Yellow

请记住,using: 作用域修饰符只读取变量,不允许写入.

Keep in mind that the using: scope modifier only reads variables and does not allow writes.