locked
PowerShell Scoping and ScriptBlocks RRS feed

  • Question

  • PowerShell scoping has defeated me again.  Why does the code below return a null value instead of "blah"?  How can I make it return "blah"?

    $string = "blah"
    $Session = New-PSSession -ComputerName "RandomRemoteComputer"
    Invoke-Command -Session $Session -ScriptBlock{$string}

    I have tried it with the global scope specified on all variables.  Same exact result.

    • Edited by J-LA Friday, September 5, 2014 6:43 PM
    Friday, September 5, 2014 6:37 PM

Answers

  • You didn't read the whole thing.

    $string = "blah"
    $Session = New-PSSession -ComputerName "RandomRemoteComputer"
    Invoke-Command -Session $Session -ScriptBlock {$args} -ArgumentList $string

    OR

    $string = "blah"
    $Session = New-PSSession -ComputerName "RandomRemoteComputer"
    Invoke-Command -Session $Session -ScriptBlock {Param($p1);$p1} -ArgumentList $string


    ¯\_(ツ)_/¯

    • Proposed as answer by Mike Laughlin Friday, September 5, 2014 7:51 PM
    • Marked as answer by J-LA Friday, September 5, 2014 11:18 PM
    Friday, September 5, 2014 7:04 PM
  • If both sides of the connection are running PowerShell 3.0 or later, you can also use the "using" scope modifier:

    $string = "blah"
    $Session = New-PSSession -ComputerName "RandomRemoteComputer"
    Invoke-Command -Session $Session -ScriptBlock { $using:string }

    • Proposed as answer by Mike Laughlin Friday, September 5, 2014 7:51 PM
    • Marked as answer by J-LA Friday, September 5, 2014 11:18 PM
    Friday, September 5, 2014 7:47 PM

All replies

  • Based on that, I tried this, which still yielded the same result:

    $string = "blah"
    $Session = New-PSSession -ComputerName "RandomRemoteComputer"
    Invoke-Command -Session $Session -ScriptBlock{$string} -ArgumentList $string


    • Edited by J-LA Friday, September 5, 2014 6:50 PM
    Friday, September 5, 2014 6:50 PM
  • You didn't read the whole thing.

    $string = "blah"
    $Session = New-PSSession -ComputerName "RandomRemoteComputer"
    Invoke-Command -Session $Session -ScriptBlock {$args} -ArgumentList $string

    OR

    $string = "blah"
    $Session = New-PSSession -ComputerName "RandomRemoteComputer"
    Invoke-Command -Session $Session -ScriptBlock {Param($p1);$p1} -ArgumentList $string


    ¯\_(ツ)_/¯

    • Proposed as answer by Mike Laughlin Friday, September 5, 2014 7:51 PM
    • Marked as answer by J-LA Friday, September 5, 2014 11:18 PM
    Friday, September 5, 2014 7:04 PM
  • If both sides of the connection are running PowerShell 3.0 or later, you can also use the "using" scope modifier:

    $string = "blah"
    $Session = New-PSSession -ComputerName "RandomRemoteComputer"
    Invoke-Command -Session $Session -ScriptBlock { $using:string }

    • Proposed as answer by Mike Laughlin Friday, September 5, 2014 7:51 PM
    • Marked as answer by J-LA Friday, September 5, 2014 11:18 PM
    Friday, September 5, 2014 7:47 PM
  • Now I see what I missed.  I did not notice him declare the new parameter in the ScriptBlock; my tunnel vision was focused on Invoke-Command.  Thanks for stepping me through it!  This is now working the way I want it.
    Friday, September 5, 2014 11:42 PM
  • Thanks!  I did see this mentioned in as an option elsewhere so I've marked it as an answer, but in my specific circumstance I cannot ensure PS 3.0 compliance at this time.
    Friday, September 5, 2014 11:44 PM