简体   繁体   English

PowerShell解压缩访问被拒绝

[英]PowerShell Unzip Access Denied

I have a PowerShell script that is supposed to unzip some files in a directory, but when I run it it throws this error: 我有一个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

I have given myself full control to each individual folder in the path and run as administrator (just to test) and it still throws the error. 我已经完全控制了路径中的每个文件夹并以管理员身份运行(仅用于测试),但仍然会引发错误。

Here is my code 这是我的代码

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" 
}

Could someone point me in the right direction to get this working? 有人可以指出正确的方向来使它正常工作吗?

Add a Where in the Get-ChildItem 添加WhereGet-ChildItem

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

I would also suggest you change the argument when calling Unzip function to 我还建议您在调用Unzip函数时更改参数

Unzip $i.FullName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM