BlackHatSamurai
BlackHatSamurai

Reputation: 23483

Pass Argument to -FilePath in Powershell

It seem like everyone who answers this question skirts the issue by giving example that either A. Aren't relevant; or, B. Use the -scriptBlock version.

Here is what I want: I want to run a PowerShell script from my local machine, on a remote machine. I need to pass in an argument or parameter.

Here is what I'm doing:

$responseObject = Invoke-Command -ComputerName MININT-OU9K10R -FilePath C:\Users\Documents\RemoteProofOfConcept\validatePath.ps1 -ArgumentList $filename  -AsJob

I want to pass $filename into the validatePath.ps1 script, and I can't figure out how to do this.

Can someone please show me how to do this, or tell me what I'm doing wrong?

None of these links have helped:

Get script directory in PowerShell when script is invoked with Invoke-Command

Powershell Invoke-Command with-FilePath Gives ItemNotFoundException

How do I pass named parameters with Invoke-Command?

Upvotes: 0

Views: 4137

Answers (1)

Keith Hill
Keith Hill

Reputation: 201642

This works on PowerShell v3. Are you on V2?

9# gc .\foo.ps1
param($myarg)

hostname
"parameter value of myarg is $myarg"
10# $filename = "c:\foo.txt"
11# Invoke-Command Beagle2 -FilePath .\foo.ps1 -ArgumentList $filename
BEAGLE2
parameter value of myarg is c:\foo.txt

BTW, foo.ps1 doesn't exist on the remote computer. It is being transported by PowerShell to the remote computer for execution. Now if $filename contains a local computer path, I'm not sure how that would work on the remote computer unless it points to a file that is common to both computers.

Upvotes: 1

Related Questions