How to pass a variable as an argument to a quoted command in powershell
My powershell script takes the following parameter:
Pairs ($ BackedUpFilePath)
The value that is passed to my script is:
"\ 123.123.123.123 \ Backups \ Website.7z"
I have another variable that is the location where I want to extract the file:
$ WebsiteDeploymentFolder = "C: \ example"
I am trying to extract an archive using the following command:
`7z x $ BackedUpFilePath -o $ WebsiteDeploymentFolder -aoa
I keep getting the following error:
Error: Cannot find archive
The following works, but I need the $ BackedUpFilePath to be dynamic:
`7z x '\ 123.123.123.123 \ Backups \ Website.7z' -o $ WebsiteDeploymentFolder -aoa
I think I need to pass the $ BackedUpFilePath in 7z with quotes, but they seem to be removed no matter what I try. I'll quote hell.
Thanks.
EDIT: Turns out the problem was that I was navigating to "\ 123.123.123.123 \ Backups \ Website.7z". (extra single quotes)
a source to share
The easiest way to work with external command line applications in PowerShell (in my opinion) is to use aliases. For example, the following works fine for me.
Set-Alias Szip C:\Utilities\7zip\7za.exe
$Archive = 'C:\Temp\New Folder\archive.7z'
$Filename = 'C:\Temp\New Folder\file.txt'
SZip a $Archive $Filename
PowerShell takes care of delimiting parameters correctly.
a source to share