Please feel free to provide feedback or file bugs here.

Remote Invoke-Command with -ScriptBlock changes types of arguments if scripblock contains variables

What's going on here???

PS C:\> Invoke-Command -ComputerName somemachine -Credential $cred -ArgumentList {Write-Host hi} -ScriptBlock {param($sb); Write-Host $sb.GetType().Name}
ScriptBlock
PS C:\> Invoke-Command -ComputerName somemachine -Credential $cred -ArgumentList {Write-Host hi} -ScriptBlock {param($sb); Write-Host $sb.GetType().Name; $a=3}
String
PS C:\> Invoke-Command -ComputerName somemachine -Credential $cred -ArgumentList {Get-ChildItem} -ScriptBlock {param($sb); Write-Host $sb.GetType().Name}
ScriptBlock
PS C:\> Invoke-Command -ComputerName somemachine -Credential $cred -ArgumentList {Get-ChildItem} -ScriptBlock {param($sb); Write-Host $sb.GetType().Name; $a=3}
String

1 vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)

    We’ll send you updates on this idea

    Tom Bowles shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    5 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...
      • Tom Bowles commented  ·   ·  Flag as inappropriate

        Asked on the scripting guys forum. However, since this forum clearly _is_ for feedback, I'll say this: If the above is the intended behaviour it is exceptionally confusing.

      • Tom Bowles commented  ·   ·  Flag as inappropriate

        If you know why this happens, Colyn1337, perhaps you could enlighten me? The post is a question, not a bug report.

      • Tom Bowles commented  ·   ·  Flag as inappropriate

        Oops, 3rd and 4th examples were supposed to be:

        PS C:\> Invoke-Command -ComputerName somemachine -Credential $cred -ArgumentList $(Get-ChildItem) -ScriptBlock {param($sb); Write-Host $sb.GetType().Name}
        DirectoryInfo
        PS C:\> Invoke-Command -ComputerName somemachine -Credential $cred -ArgumentList $(Get-ChildItem) -ScriptBlock {param($sb); Write-Host $sb.GetType().Name; $a=3}
        PSObject

      Feedback and Knowledge Base