Using Try Catch command to execute Powershell command remotely and then fallback with native command when failed?

后端 未结 1 830
花落未央
花落未央 2021-01-28 05:11

I\'m trying to compile the script to manually connect to each server and then perform the Powershell command remotely, and if the Powershell module is not found, then failback t

相关标签:
1条回答
  • 2021-01-28 05:38

    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.

    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
    

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

    0 讨论(0)
提交回复
热议问题