I'm attempting to use PsExec to run Expand-Archive on a remote computer, extracting to a folder inside Program Files, but Expand-Archive keeps returning the error:
Expand-Archive : A positional parameter cannot be found that accepts argument 'Files\Folder'
I take this to mean that Expand-Archive gets to the space in "Program Files" and thinks one argument is done and starts trying to interpret the next.
I have used the following variants of the command, all with the same outcome:
Double-quotes around entire Powershell portion, using %programfiles% environment variable:
.\PsExec.exe \\computername /s cmd /c "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -command expand-archive c:\temp\bc.zip -destinationpath %programfiles%\Folder"
Single-quotes just around the destination folder path, using %programfiles% environment variable:
.\PsExec.exe \\computername /s cmd /c C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -command expand-archive c:\temp\bc.zip -destinationpath '%programfiles%\Folder'
Double-quotes just around the destination folder path, using %programfiles% environment variable:
.\PsExec.exe \\computername /s cmd /c C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -command expand-archive c:\temp\bc.zip -destinationpath "%programfiles%\Folder"
No quotes, using %programfiles% environment variable:
.\PsExec.exe \\computername /s cmd /c C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -command expand-archive c:\temp\bc.zip -destinationpath %programfiles%\Folder
Double-quotes around fully typed folder path:
.\PsExec.exe \\computername /s cmd /c C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -command expand-archive c:\temp\bc.zip -destinationpath "C:\Program Files\Folder"
I have tried doing the same with psexec, and i'm facing all kinds of errors.
I would personally use powershell's invoke command. Would this also work for you? If not i will try to fiddle with psexec some more if you would like.
Example :
$PATH = "EnterPath"
$COMPUTERNAME = "EnterComputerName"
Invoke-Command -ComputerName $COMPUTERNAME -ScriptBlock {
}
The script will run on given computer, $env:ProgramFiles translates to the variable on the remote computer, equivalent of %programfiles% in cmd.
I wouldn't say that the accepted answer is not correct, but rather it's not targeting the actual issue in my opinion.
Here there's not any issue with
PSExec
, but issue with how thePowerShell
command is called.When calling Powershell command many a times we cannot just pass commands without quotes, especially those commands that need quoted arguments like spaced-paths of OS directory structure. So we have to apply ampersand
&
before command when calling the command and escape the command's inner quotes for arguments, which pretty much looks like this:OR
You can also ignore the
-command
switch in the above one-liner as long as you provide&
properly before actual command :)