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

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:

Invoke-Command -ComputerName $Server -FilePath C:\myFolder\myScript.ps1

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:

$Name = "svchost"
$CPU = 99
Invoke-Command –ComputerName $RemoteComputer –ScriptBlock { param($RemoteName,$RemoteCPU) Get-Process -Name $RemoteName | Where -Property CPU -gt $RemoteCPU } -ArgumentList $Name,$CPU

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?

Invoke-Command - ComputerName $RemoteComputer -Filepath ???? 

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.

$myPath = $env:windir

# Passing script variable to script block:
Invoke-Command -ComputerName myComputer -ScriptBlock { Get-ChildItem $Using:myPath }

# Passing script block data to main script

$Proc = Invoke-Command -ComputerName myComputer -ScriptBlock { Get-Process }

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

“$Using:myPath”

$Using is what you want.

Take a look at the MS video regarding the $Using statement Sam is sharing with you relative to your goal.
Learn About the PowerShell 5.0 Using Statement
https://channel9.msdn.com/Shows/MsftPowerShell/powershell-using-statement

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 :frowning:
I’ll keep trying to work it out… cheers.

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

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:

Invoke-Command -ComputerName $targethost -FilePath $2ndscriptToRun -ArgumentList $a,$b,$c

as long as I had the -

param ( 
      [string]$a, 
      [string]$b,
      [string]$c
)

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.

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}

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/

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.