PowerShell Remoting

Experts,

I'm using PowerShell remoting to extract a zip file on my target server. The code I have is as follows

	$targetServer =  "someServer"
	$deployPath = "D:/Assets/Web/12/"
	$credentials = "someCredentials"
	
	invoke-command -computername $targetServer  {param($arg1) c:\7zip\7z.exe x -o$arg1  -y C:\ftproot\packages\webassets.zip} -Credential $credentials -ArgumentList $deployPath

Open in new window


This doesn't actually work.

However, if I ignore the $arg1, and hardcode the path in place of $arg1 within the script block like this...

	$targetServer =  "someServer"
	$deployPath = "D:/Assets/Web/12/"
	$credentials = "someCredentials"
	
	invoke-command -computername $targetServer  {param($arg1) c:\7zip\7z.exe x -oD:/Assets/Web/12/  -y C:\ftproot\packages\webassets.zip} -Credential $credentials -ArgumentList $deployPath

Open in new window



It works without any issues. So my issue is really that I cannot pass this a file path to work with. I've tried clearing the code and using write-host to see if it is correctly passed in and thats fine. I'm not sure where to go now.

Any help will as ever be appreciated.
S-UnitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jamie McKillopIT ManagerCommented:
Hello,

Use -args  instead of -argumentlist

Try this:

$targetServer =  "someServer"
$deployPath = "D:/Assets/Web/12/"
$credentials = "someCredentials"
	
invoke-command -computername $targetServer  {param($arg1) c:\7zip\7z.exe x -o $arg1  -y C:\ftproot\packages\webassets.zip} -Credential $credentials -Args $deployPath
                                  

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
jjmck, -Args is just an alias for -ArgumentList, and hence doesn't change anything.

S-Unit, the issue is that the var begins in-midst of an option string. That is not supported, and PowerShell does not expand the var. So, 7z will get a string of -o$arg1.
You can force PS to expand by enclosing the option string into double quotes:
$targetServer =  "someServer"
$deployPath = "D:/Assets/Web/12/"
$credentials = "someCredentials"
	
invoke-command -computername $targetServer  {
  param($arg1)
  c:\7zip\7z.exe x "-o$arg1" -y C:\ftproot\packages\webassets.zip
} -Credential $credentials -ArgumentList $deployPath

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.