Passing local variables to remote (Invoke-Command -FilePath) to script

Welcome Forums General PowerShell Q&A Passing local variables to remote (Invoke-Command -FilePath) to script

Viewing 5 reply threads
  • Author
    Posts
    • #90910
      Participant
      Topics: 8
      Replies: 15
      Points: 76
      Rank: Member

      Hi, Need some help with Invoke-Command and passing variables to a script -Filepath.

      I know we can use invoke-command to run a script located on a remote server, and run it ‘locally’.
      Something like:

      But how do I pass local variables in my script to that remote script?

      I’ve seen plenty of examples using Scriptblock, but I want to pass in parameters to the entire remote script (just like using
      Param (
      $x,
      $y
      )

      I’ve seen examples like:

      But how do I do the same, but not use scriptBlock, but pass local variables as parameters into the remote script with -Filepath?
      Does ArgumentList still work? What is the syntax though?

      Hope that makes sense?
      I will have scripts located on remote servers, but need to pass different variables from time to time.

      Thanks for any advice!

      Alex.

    • #90916
      Participant
      Topics: 12
      Replies: 541
      Points: 1,314
      Helping Hand
      Rank: Community Hero
    • #90919
      Participant
      Topics: 8
      Replies: 15
      Points: 76
      Rank: Member

      Hi Sam
      Thanks, but I don’t follow your answer. Sorry my ignorant power shell brain i guess.

      I don’t see in your example where you pass parameter variables to the script ?? I don’t see the script name via -filepath etc
      I must be missing something.

      Thanks

    • #90938
      Participant
      Topics: 8
      Replies: 15
      Points: 76
      Rank: Member

      Thank you all for your advice and replies. I must be a bit stupid I think, as I can’t for the life of me, follow how $Using works with sending variables to another PS script 🙁
      I’ll keep trying to work it out… cheers.

      • #90958
        Participant
        Topics: 2
        Replies: 1014
        Points: 2,105
        Helping Hand
        Rank: Community Hero

        No, not stupid, just unfamiliar. That link I added was for the Using statement in another context. That’s on me. Actually, that is what I get for multi-tasking and trying to do multiple things at once. I’m am old dude and sometimes, well. you know.

        Here is the set of info I meant to add for you query to Sam’s response.

        About Remote Variables
        You can use variables in commands that you run on remote computers. Simply assign a value to the variable and then use the variable in place of the value.+

        By default, the variables in remote commands are assumed to be defined in the session in which the command runs. You can also use variables that are defined in the local session, but you must identify them as local variables in the command.

        USING LOCAL VARIABLES

        You can also use local variables in remote commands, but you must indicate that the variable is defined in the local session.

        Beginning in Windows PowerShell 3.0, you can use the Using scope modifier to identify a local variable in a remote command.

        The syntax of Using is as follows:

        Copy
        $Using:

        In the following example, the $ps variable is created in the local session, but is used in the session in which the command runs. The Using scope modifier identifies $ps as a local variable.

        $ps = “Windows PowerShell”
        Invoke-Command -ComputerName S1 -ScriptBlock {
        Get-WinEvent -LogName $Using:ps
        }

        You can also use the Using scope modifier in PSSessions.

        $s = New-PSSession -ComputerName S1
        $ps = “Windows PowerShell”
        Invoke-Command -Sessions $s -ScriptBlock {Get-WinEvent -LogName $Using:ps}

        https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-5.1

        Access Local Variables in a Remote Session with PowerShell
        Occasionally I’ll come across a system with PowerShell version 2.0 on it that I need to run a remote command against which needs access to local variables. I can never remember how to accomplish that task so I thought I would write a blog about it as a reminder for myself and others who may find themselves in the same scenario.

        I’ll demonstrate each of the available options and whether or not they work on PowerShell version 5.1
        http://mikefrobbins.com/2017/11/09/access-local-variables-in-a-remote-session-with-powershell/

    • #90941
      Participant
      Topics: 8
      Replies: 15
      Points: 76
      Rank: Member

      I was hoping for some guidance with examples actually, that might be helpful for my objectives.
      Appreciate it.

    • #90947
      Participant
      Topics: 8
      Replies: 15
      Points: 76
      Rank: Member

      Hi all, actually, I got it working without the $using:mypath etc.
      In order to send the variable values gathered in one script, to another script to then be run remotely I used:

      as long as I had the –

      code at the top of the 2nd script matching the same variable names, it simply worked by matching those and passing through those values!
      In the end, the 2nd script to run is stored local to the machine running the scripts – so I can manage all automation scripts in the one area/code repository, but just invoke sessions on remote machines and run them remotely.

      thanks for all your advice and replies!
      Alex.

      • #90959
        Participant
        Topics: 2
        Replies: 1014
        Points: 2,105
        Helping Hand
        Rank: Community Hero

        What you just discovered is exactly what Sam’s sample shows / what he was guiding you to do.
        But good on you for your lightbulb moment.

Viewing 5 reply threads
  • The topic ‘Passing local variables to remote (Invoke-Command -FilePath) to script’ is closed to new replies.