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:


    Invoke-Command -ComputerName $remotehostname -ScriptBlock {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


  • 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