Here's the code
function New-BackupZip
{
[CmdletBinding()]
[Alias()]
Param
(
#File to be compressed
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[String]
$File,
#Password
[String]
$Password
)
Begin
{
[String]${Output-Filename} = "Backups_$((Get-Date).ToShortDateString()).zip"
[String]$7zipParams = "-tzip -p$password ${Output-Filename} $File"
[string]$7zipPath = "${env:ProgramFiles}\7-Zip\7z.exe"
}
Process
{
& $7zipPath "a $7zipParams".ToString()
}
End
{
}
}
My error ALL THE TIME, no matter where I look into, it's that 7z.exe says "unsupported command":
Unsupported command error
Testing the CMDlet
But If I type it manually, of course it works: Manually works
So, what the heck is going on? Is there something going on in the background of PowerShell that the 7zip exe file can't understand?
The problem is something underlying PowerShell and Strings, and how it "sends" the string to a command.
The solution, use an array via the constructor:
$ArrayOfParams = "a","-tzip","-mx=0","-mem=AES256","$passwordparam","$OutputFile","$File"
Then, just use:
& 7z.exe @ArrayOfParams
That's the solution.
To expand a little on the "Underlying":
"Generally speaking, this is how shells behave, not just PowerShell, so there's no documentation that specifically addresses what you've attempted. Command invocations (not just of external programs, though special considerations apply to them) are subject to argument parsing mode, which is described as part of the conceptual about_Parsing help topic" -mkelement0