What is difference for Invoke-Command between ScriptBlock and FilePath?

This topic contains 7 replies, has 2 voices, and was last updated by Profile photo of Adam Bertram Adam Bertram 2 years, 1 month ago.

  • Author
    Posts
  • #19979
    Profile photo of GS
    GS
    Participant

    I have very simple script located on UNC share which executes fine remotely when executed with -FilePath parameter of Invoke-Command but refusing to do the same thing when executed with ScriptBlock instead. You see results below. I assume it's issue with doublehop but why it works with -FilePath then?

    PS C:\Users\g> Invoke-Command -ComputerName SJVAPPSVC3 -FilePath \\prod\serverops\BuildStandards\scripts\cleanup
    \test.ps1
    here
    PS C:\Users\g>Invoke-Command -ComputerName SJVAPPSVC3 {\\prod\serverops\BuildStandards\scripts\cleanup\test.ps1
     }
    The term '\\prod\serverops\BuildStandards\scripts\cleanup\test.ps1' is not recognized as the name of a cmdlet,
    function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
    path is correct and try again.
        + CategoryInfo          : ObjectNotFound: (\\prod\serverop...leanup\test.ps1:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
        + PSComputerName        : SJVAPPSVC3
    
  • #19980
    Profile photo of Adam Bertram
    Adam Bertram
    Participant

    In scenario #1 Powershell is reading the contents of \\prod\serverops\BuildStandards\scripts\cleanup\test.ps1 locally into a string, converting it to a script block and then executing the contents of that scriptblock on SJAPPSVC3. As far as SJAPPSVC3 is concerned it's just executing some code in RAM and doesn't have any idea about test.ps1.

    In your second scenario, you're forcing SJAPPSVC3 to read test.ps1, which, in your case you probably are hitting the double hop problem.

    If you want scenario #2 to work I created a script to it here http://www.adamtheautomator.com/powershell-remote-script-execution/. It lets you copy a file or folder from a UNC path to a remote computer and execute it. This gets around the double hop problem by temporarily copying the script down to the remote machine, executing it and then cleaning up the files after it's done.

  • #19993
    Profile photo of GS
    GS
    Participant

    So in essense difference between ScriptBlock and FilePath is that ScriptBlock is evaluated and furnished by the remote host on remotehost while FilePath is evaluated and fetched directly from local host from remote host?
    My dilemma is passing named parameters to Invoke-Command which is more or less easy to do with ScriptBlock but that does not work.

  • #20011
    Profile photo of Adam Bertram
    Adam Bertram
    Participant

    ScriptBlock is passed to the remote host and it's executed from there. FilePath is retrieved on the localhost and passed to the remote host as a ScriptBlock. To use parameters in Invoke-Command use $using. You can do pass local variables to the remote session by doing:

    $Var = 'abc'
    Invoke-Commmand -Computername COMPUTER -ScriptBlock {$using:abc}

  • #20013
    Profile photo of GS
    GS
    Participant

    How do I pass variable if I use -FilePath? ScriptBlock is not working due to double hop issue

  • #20014
    Profile photo of Adam Bertram
    Adam Bertram
    Participant

    Hmm...I'd either use my remote file copy script I pointed to earlier or follow this guide to allow the double-hop http://www.travisgan.com/2014/03/enable-powershell-double-hop-remoting.html.

  • #20032
    Profile photo of GS
    GS
    Participant

    To use -CredSSP I would have to supply manually username/password to the script which will not be possible in scenario where I'm running script from (Powershell web access). Am I misunderstanding how credSSP is used or exactly this is usefull for automation scenario if it's relies on user entering username/password manually?

  • #20035
    Profile photo of Adam Bertram
    Adam Bertram
    Participant

    You don't to interactively give a username and password. You can store credentials in a file and use it whenever you like. Here's a good reference. http://www.leeholmes.com/blog/2008/06/04/importing-and-exporting-credentials-in-powershell/

You must be logged in to reply to this topic.