PowerShell 解压缩访问被拒绝

PowerShell Unzip Access Denied

我有一个 PowerShell 脚本,它应该将一些文件解压缩到一个目录中,但是当我 运行 它抛出这个错误时:

Exception calling "ExtractToDirectory" with "2" argument(s): "Access to the path 
'E:\SubFolder\SubFolder2\SubFolder3' is denied."
At line:7 char:5
+     [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, "E:\SubFolder\Sub ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : UnauthorizedAccessException

我已经完全控制了路径中的每个单独的文件夹,并且 运行 作为管理员(只是为了测试),它仍然会抛出错误。

这是我的代码

Add-Type -AssemblyName System.IO.Compression.FileSystem

function Unzip
{
    param([string]$zipfile)

    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, "E:\SubFolder\SubFolder2\SubFolder3")

}

$Files = get-childitem "E:\SubFolder\SubFolder2\SubFolder3"

foreach ( $i in $files ) 
{
    Unzip "SubFolder\SubFolder2\SubFolder3$i" 
}

有人能给我指出正确的方向来让这个工作吗?

Get-ChildItem

中添加一个Where
Get-ChildItem "E:\SubFolder\SubFolder2\SubFolder3" | Where { $_.Extension -eq ".zip" }

我还建议您将调用 Unzip 函数时的参数更改为

Unzip $i.FullName