"Unknown Switch" 在 PowerShell 脚本中使用 7-zip 归档以“-”开头的文件名时出错

"Unknown Switch" error when using 7-zip in PowerShell script to archive file names that start with a `-`

我的文件夹中有一系列文件,其中包含大型日志文件

-20180907 1229 debug.log
-20180907 1229 system.log

我想编写一个 PowerShell 脚本来缩小它们的大小。这是我的脚本:

dir *.log | % {
    $archive = '"' + $_.Name + '.7z"'
    $archive
    $source = '"' + $_.Name + '"'
    $source
    &"C:\Program Files-Zipz.exe" -mx9 a -t7z $archive $source -WhatIf
}

然而,当我 运行 它时,我得到以下输出:

"-20180907 1229 debug.log.7z"
"-20180907 1229 debug.log"
7z.exe : 
At C:\Users\User\Temporary\Test 7z\archive-logs.ps1:6 char:5
+     &"C:\Program Files-Zipz.exe" -mx9 a -t7z $archive $source
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Command Line Error:
Unknown switch:
-20180907 1229 debug.log.7z
"-20180907 1229 system.log.7z"
"-20180907 1229 system.log"
7z.exe : 
At C:\Users\User\Temporary\Test 7z\archive-logs.ps1:6 char:5
+     &"C:\Program Files-Zipz.exe" -mx9 a -t7z $archive $source
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Command Line Error:
Unknown switch:
-20180907 1229 system.log.7z

尽管我用双引号将文件名括起来,但它显示为 7-Zip 忽略了双引号。

有什么想法吗?


我尝试了这个变体(将文件名用单引号引起来):

dir *.log | % {
    $archive = "'" + $_.Name + ".7z'"
    $archive
    $source = "'" + $_.Name + "'"
    $source
    &"C:\Program Files-Zipz.exe" -mx9 a -t7z $archive $source
}

这次我得到了这个输出:

'-20180907 1229 debug.log.7z'
'-20180907 1229 debug.log'

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Open archive: '-20180907 1229 debug.log.7z'
--
Path = '-20180907 1229 debug.log.7z'
Type = 7z
Physical Size = 32
Headers Size = 0
Solid = -
Blocks = 0

Scanning the drive:
7z.exe : 
At C:\Users\User\Temporary\Test 7z\archive-logs-B.ps1:6 char:5
+     &"C:\Program Files-Zipz.exe" -mx9 a -t7z $archive $source
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

WARNING: The system cannot find the file specified.
'-20180907 1229 debug.log'
0 files, 0 bytes

Updating archive: '-20180907 1229 debug.log.7z'

Add new data to archive: 0 files, 0 bytes


Files read from disk: 0
Archive size: 32 bytes (1 KiB)

Scan WARNINGS for files and folders:

'-20180907 1229 debug.log' : The system cannot find the file specified.
----------------
Scan WARNINGS: 1

它创建了类似 '-20180907 1229 debug.log.7z' 的文件,但没有内容。

我认为不需要用引号引起来。
如果我执行以下操作怎么办?

dir *.log | % { &"C:\Program Files-Zipz.exe" -mx9 a -t7z "$_.7z" $_ }

试试这个:

 $executable = "C:\Program Files-Zipz.exe"

 dir *.log | % {
    $archive = "$($_.Name).7z"
    $archive
    $source = $_.Name
    $source 
    $oneliner = "a",".$($archive)",".$($source)", "-mx4"
    & $executable @oneliner
}