piped value in Invoke-Command

Welcome Forums General PowerShell Q&A piped value in Invoke-Command

This topic contains 1 reply, has 2 voices, and was last updated by

1 year, 6 months ago.

  • Author
  • #73990

    Points: 0
    Rank: Member

    Hi All, I have this small beginners problem with piped value into Invoke-Command scriptblock.

    What I have:

    $ser | % {Invoke-Command -ComputerName $_ -ScriptBlock {c:\temp\app.exe -outputFile c:\temp\$_.mf3}}

    and I get nothing for $_, all files are named ".mf3"

    my workaround is renaming them after, which works, but doesn't look nice on code level

    $ser | % {Rename-Item "\\$_\c$\temp\.mf3" \\$_\c$\temp\$_.mf3}
  • #74072

    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    That's not quite how Invoke-Command works.

    $_ only has meaning on your local computer. When the Script Block gets to the remote machine, $_ has no meaning. Look at the "Remote Variables" topic in The Big Book of PowerShell Gotchas, linked from our eBooks menu, for alternate approaches. The examples in the help for Invoke-Command also have examples, albeit with a bit less explanation than the book.

The topic ‘piped value in Invoke-Command’ is closed to new replies.