我尝试使用Powershell v5.1压缩文件夹,但有些文件被其他进程使用,PS无法强制或忽略它们。
Get-ChildItem "C:\folder" | Compress-Archive -DestinationPath "C:\file.zip"
还使用-Force
和-ErrorAction Ignore,Continue,SilentlyContinue
进行了测试,但每次出现这样的错误时:
ZipArchiveHelper : The process cannot access the file 'C:\folder\filexyz' be cause it is being used by another process. At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:69 6 char:30 + ... sArchived = ZipArchiveHelper $subDirFiles.ToArray() $destinationPath ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (C:\folder\filexyz:String) [Write-Error], IOException + FullyQualifiedErrorId : CompressArchiveUnauthorizedAccessError,ZipArchiveHelper New-Object : Exception calling ".ctor" with "1" argument(s): "Stream was not readable." At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:80 7 char:38 + ... $srcStream = New-Object System.IO.BinaryReader $currentFileStream + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
答案 0 :(得分:0)
由于其他进程使用的文件仍然可以读取,我认为问题是权限不足。
尝试以管理员身份启动PowerShell(搜索PowerShell - >右键单击 - >以管理员身份运行)。
答案 1 :(得分:0)
以下情况例外:当Exception calling ".ctor" with "1" argument(s): "Stream was not readable."
与Compress-Archive一起使用多个文件并且一个或多个打开时发生。
您可以在将文件传送到Compress-Archive之前检查文件是否未锁定。
$items = Get-ChildItem -Path $path
[System.Collections.ArrayList]$itemsToCompress = @()
[System.Collections.ArrayList]$itemsToNotCompress = @()
foreach ($item in $items){
Try {
# Checking File Mode and Access
$FileStream = [System.IO.File]::Open($item.FullName,'Open','Read')
if ($null -ne $FileStream){
$FileStream.Close()
$FileStream.Dispose()
$itemsToCompress += $item
}
}
Catch {
$itemsToNotCompress += $item
}
}
$itemsToCompress | Compress-Archive -DestinationPath $archivefile -ErrorAction SilentlyContinue
答案 2 :(得分:0)
您可以检查$ Error对象。如果在compress调用之后填充了它,则发生错误,您可以采取适当的措施。
$error.clear()
Get-ChildItem "C:\folder" | Compress-Archive -DestinationPath "C:\file.zip"
if ($error[0] -ne $null) {
# Take appropriate action here
}
有关更多信息,请参见此线程。 https://community.spiceworks.com/topic/2026265-checking-the-success-or-failure-of-compress-archive