Powershell 上的 7zip 命令无法解析?

7zip commands on Powershell can't be parsed?

这是代码

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]zipParams = "-tzip -p$password ${Output-Filename} $File"
        [string]zipPath = "${env:ProgramFiles}-Zipz.exe"
    }
    Process
    {
        & zipPath "a zipParams".ToString()
    }
    End
    {
    }
}

我的错误一直以来,无论我在哪里查看,都是 7z.exe 说“不受支持的命令”:
Unsupported command error
Testing the CMDlet

但是如果我手动输入,当然可以: Manually works

所以,这到底是怎么回事? 7zip exe文件无法理解的PowerShell后台是否发生了什么?

问题出在 PowerShell 和字符串的底层,以及它如何将字符串“发送”到命令。
解决方案,通过构造函数使用数组:
$ArrayOfParams = "a","-tzip","-mx=0","-mem=AES256","$passwordparam","$OutputFile","$File"

然后,只需使用:
& 7z.exe @ArrayOfParams

这就是解决方案。

稍微扩展一下“底层”:

"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