none
Powershell scriptblock with variable RRS feed

  • Question

  • Hi all, I had wrote a script that allow me to execute a script remotely which work successfully. However if i try to change the script to using a variable it will fail. Below are the example:

    Success:

    Invoke-Command -ComputerName $remotehostname -ScriptBlock {D:\Script\remotescript.ps1}

    Failed:

    $remotescript=D:\Script\remotescript.ps1

    Invoke-Command -ComputerName $remotehostname -ScriptBlock {$remotescript}

    How can i make the failed script to be success?


    • Edited by Jimson Huang Monday, December 18, 2017 10:29 AM add file type
    Monday, December 18, 2017 10:28 AM

Answers

  • You have to assign the variable and reference it as an external variable.

    $remotescript = D:\Script\remotescript.ps1
    Invoke-Command -ComputerName $remotehostname -ScriptBlock { & $using:remotescript }


    \_(ツ)_/

    Tuesday, December 19, 2017 3:09 AM

All replies