How can I pass a local variable to a script block executed on a remote machine with Invoke-Command?











up vote
7
down vote

favorite
2












I'm trying to retrieve the Filehash of a file, located in remote server using Invoke-Command. I'm running my scripts on powershell version 4. It works fine, when i give the full path as below:



Invoke-Command -ComputerName winserver -ScriptBlock { 
Get-FileHash E:testtestfile.zip -Algorithm SHA1
}


The above command works. But I need to pass the file name via a variable as below:



Invoke-Command -ComputerName winserver -ScriptBlock { 
Get-FileHash E:test$dest.zip -Algorithm SHA1
}


I'm new to scripting and powershell. Please, help me with resolving this!










share|improve this question




























    up vote
    7
    down vote

    favorite
    2












    I'm trying to retrieve the Filehash of a file, located in remote server using Invoke-Command. I'm running my scripts on powershell version 4. It works fine, when i give the full path as below:



    Invoke-Command -ComputerName winserver -ScriptBlock { 
    Get-FileHash E:testtestfile.zip -Algorithm SHA1
    }


    The above command works. But I need to pass the file name via a variable as below:



    Invoke-Command -ComputerName winserver -ScriptBlock { 
    Get-FileHash E:test$dest.zip -Algorithm SHA1
    }


    I'm new to scripting and powershell. Please, help me with resolving this!










    share|improve this question


























      up vote
      7
      down vote

      favorite
      2









      up vote
      7
      down vote

      favorite
      2






      2





      I'm trying to retrieve the Filehash of a file, located in remote server using Invoke-Command. I'm running my scripts on powershell version 4. It works fine, when i give the full path as below:



      Invoke-Command -ComputerName winserver -ScriptBlock { 
      Get-FileHash E:testtestfile.zip -Algorithm SHA1
      }


      The above command works. But I need to pass the file name via a variable as below:



      Invoke-Command -ComputerName winserver -ScriptBlock { 
      Get-FileHash E:test$dest.zip -Algorithm SHA1
      }


      I'm new to scripting and powershell. Please, help me with resolving this!










      share|improve this question















      I'm trying to retrieve the Filehash of a file, located in remote server using Invoke-Command. I'm running my scripts on powershell version 4. It works fine, when i give the full path as below:



      Invoke-Command -ComputerName winserver -ScriptBlock { 
      Get-FileHash E:testtestfile.zip -Algorithm SHA1
      }


      The above command works. But I need to pass the file name via a variable as below:



      Invoke-Command -ComputerName winserver -ScriptBlock { 
      Get-FileHash E:test$dest.zip -Algorithm SHA1
      }


      I'm new to scripting and powershell. Please, help me with resolving this!







      powershell powershell-remoting powershell-v4.0






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 15 '16 at 15:45









      mklement0

      122k20234265




      122k20234265










      asked Feb 18 '16 at 21:08









      Bose

      4817




      4817
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          13
          down vote



          accepted










          In PowerShell 4 (3+ actually) the easiest way is to use the Using scope modifier:



          Invoke-Command -ComputerName winserver -ScriptBlock { 
          Get-FileHash E:test$Using:dest.zip -Algorithm SHA1
          }


          For a solution that works with all versions:



          Invoke-Command -ComputerName winserver -ScriptBlock { param($myDest) 
          Get-FileHash E:test$myDest.zip -Algorithm SHA1
          } -ArgumentList $dest





          share|improve this answer




























            up vote
            6
            down vote













            To complement briantist's helpful answer:



            The script block passed to Invoke-Command is (as intended) executed on the remote machine, using the remote machine's variables by default.



            Thus, in order to use a local variable (value), extra steps are needed (to put it differently: inside a script block executed remotely, you cannot just refer to local variables as you normally would, such as with $dest):





            • PS v3+ offers the using: scope modifier for direct use of a local variable inside the script block - see briantist's first command.




              • Note that using: only works when Invoke-Command actually targets a remote machine.



            • The only option that also works in earlier versions is to pass the local variable as a parameter to the script block. - see briantist's second command.



            For more information, refer to Get-Help about_Remote_Variables or the docs online.






            share|improve this answer



















            • 1




              Thanks, I would usually add this stuff and didn't have time; +1
              – briantist
              Feb 18 '16 at 21:35











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35492437%2fhow-can-i-pass-a-local-variable-to-a-script-block-executed-on-a-remote-machine-w%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            13
            down vote



            accepted










            In PowerShell 4 (3+ actually) the easiest way is to use the Using scope modifier:



            Invoke-Command -ComputerName winserver -ScriptBlock { 
            Get-FileHash E:test$Using:dest.zip -Algorithm SHA1
            }


            For a solution that works with all versions:



            Invoke-Command -ComputerName winserver -ScriptBlock { param($myDest) 
            Get-FileHash E:test$myDest.zip -Algorithm SHA1
            } -ArgumentList $dest





            share|improve this answer

























              up vote
              13
              down vote



              accepted










              In PowerShell 4 (3+ actually) the easiest way is to use the Using scope modifier:



              Invoke-Command -ComputerName winserver -ScriptBlock { 
              Get-FileHash E:test$Using:dest.zip -Algorithm SHA1
              }


              For a solution that works with all versions:



              Invoke-Command -ComputerName winserver -ScriptBlock { param($myDest) 
              Get-FileHash E:test$myDest.zip -Algorithm SHA1
              } -ArgumentList $dest





              share|improve this answer























                up vote
                13
                down vote



                accepted







                up vote
                13
                down vote



                accepted






                In PowerShell 4 (3+ actually) the easiest way is to use the Using scope modifier:



                Invoke-Command -ComputerName winserver -ScriptBlock { 
                Get-FileHash E:test$Using:dest.zip -Algorithm SHA1
                }


                For a solution that works with all versions:



                Invoke-Command -ComputerName winserver -ScriptBlock { param($myDest) 
                Get-FileHash E:test$myDest.zip -Algorithm SHA1
                } -ArgumentList $dest





                share|improve this answer












                In PowerShell 4 (3+ actually) the easiest way is to use the Using scope modifier:



                Invoke-Command -ComputerName winserver -ScriptBlock { 
                Get-FileHash E:test$Using:dest.zip -Algorithm SHA1
                }


                For a solution that works with all versions:



                Invoke-Command -ComputerName winserver -ScriptBlock { param($myDest) 
                Get-FileHash E:test$myDest.zip -Algorithm SHA1
                } -ArgumentList $dest






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 18 '16 at 21:11









                briantist

                30.3k34271




                30.3k34271
























                    up vote
                    6
                    down vote













                    To complement briantist's helpful answer:



                    The script block passed to Invoke-Command is (as intended) executed on the remote machine, using the remote machine's variables by default.



                    Thus, in order to use a local variable (value), extra steps are needed (to put it differently: inside a script block executed remotely, you cannot just refer to local variables as you normally would, such as with $dest):





                    • PS v3+ offers the using: scope modifier for direct use of a local variable inside the script block - see briantist's first command.




                      • Note that using: only works when Invoke-Command actually targets a remote machine.



                    • The only option that also works in earlier versions is to pass the local variable as a parameter to the script block. - see briantist's second command.



                    For more information, refer to Get-Help about_Remote_Variables or the docs online.






                    share|improve this answer



















                    • 1




                      Thanks, I would usually add this stuff and didn't have time; +1
                      – briantist
                      Feb 18 '16 at 21:35















                    up vote
                    6
                    down vote













                    To complement briantist's helpful answer:



                    The script block passed to Invoke-Command is (as intended) executed on the remote machine, using the remote machine's variables by default.



                    Thus, in order to use a local variable (value), extra steps are needed (to put it differently: inside a script block executed remotely, you cannot just refer to local variables as you normally would, such as with $dest):





                    • PS v3+ offers the using: scope modifier for direct use of a local variable inside the script block - see briantist's first command.




                      • Note that using: only works when Invoke-Command actually targets a remote machine.



                    • The only option that also works in earlier versions is to pass the local variable as a parameter to the script block. - see briantist's second command.



                    For more information, refer to Get-Help about_Remote_Variables or the docs online.






                    share|improve this answer



















                    • 1




                      Thanks, I would usually add this stuff and didn't have time; +1
                      – briantist
                      Feb 18 '16 at 21:35













                    up vote
                    6
                    down vote










                    up vote
                    6
                    down vote









                    To complement briantist's helpful answer:



                    The script block passed to Invoke-Command is (as intended) executed on the remote machine, using the remote machine's variables by default.



                    Thus, in order to use a local variable (value), extra steps are needed (to put it differently: inside a script block executed remotely, you cannot just refer to local variables as you normally would, such as with $dest):





                    • PS v3+ offers the using: scope modifier for direct use of a local variable inside the script block - see briantist's first command.




                      • Note that using: only works when Invoke-Command actually targets a remote machine.



                    • The only option that also works in earlier versions is to pass the local variable as a parameter to the script block. - see briantist's second command.



                    For more information, refer to Get-Help about_Remote_Variables or the docs online.






                    share|improve this answer














                    To complement briantist's helpful answer:



                    The script block passed to Invoke-Command is (as intended) executed on the remote machine, using the remote machine's variables by default.



                    Thus, in order to use a local variable (value), extra steps are needed (to put it differently: inside a script block executed remotely, you cannot just refer to local variables as you normally would, such as with $dest):





                    • PS v3+ offers the using: scope modifier for direct use of a local variable inside the script block - see briantist's first command.




                      • Note that using: only works when Invoke-Command actually targets a remote machine.



                    • The only option that also works in earlier versions is to pass the local variable as a parameter to the script block. - see briantist's second command.



                    For more information, refer to Get-Help about_Remote_Variables or the docs online.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jul 11 '17 at 17:57

























                    answered Feb 18 '16 at 21:19









                    mklement0

                    122k20234265




                    122k20234265








                    • 1




                      Thanks, I would usually add this stuff and didn't have time; +1
                      – briantist
                      Feb 18 '16 at 21:35














                    • 1




                      Thanks, I would usually add this stuff and didn't have time; +1
                      – briantist
                      Feb 18 '16 at 21:35








                    1




                    1




                    Thanks, I would usually add this stuff and didn't have time; +1
                    – briantist
                    Feb 18 '16 at 21:35




                    Thanks, I would usually add this stuff and didn't have time; +1
                    – briantist
                    Feb 18 '16 at 21:35


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35492437%2fhow-can-i-pass-a-local-variable-to-a-script-block-executed-on-a-remote-machine-w%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Schultheiß

                    Verwaltungsgliederung Dänemarks

                    Liste der Kulturdenkmale in Wilsdruff