piped value in Invoke-Command

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 3 months, 2 weeks ago.

  • Author
    Posts
  • #73990

    Lucas
    Participant

    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

    Don Jones
    Keymaster

    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.

You must be logged in to reply to this topic.