Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell System.IO.Compression.ZipFileExtensions-CreateEntryFromFile-giving“;因为它正被另一个进程使用”;拉链_Powershell_Zip_Powershell 3.0 - Fatal编程技术网

Powershell System.IO.Compression.ZipFileExtensions-CreateEntryFromFile-giving“;因为它正被另一个进程使用”;拉链

Powershell System.IO.Compression.ZipFileExtensions-CreateEntryFromFile-giving“;因为它正被另一个进程使用”;拉链,powershell,zip,powershell-3.0,Powershell,Zip,Powershell 3.0,我正在尝试将1个文件压缩到一个新的存档中。下面的代码可以工作。。。。大多数时候。然而,偶尔(网络问题?)我会更改代码(见下文)以创建一个唯一的名称,但偶尔还是会出现此错误 有没有办法解决这个问题?我在powershell 3上,所以压缩存档不是一个选项,尽管我可能可以安装pscx Exception calling "Open" with "2" argument(s): "The process cannot access the file '\\path\share$\folder\file

我正在尝试将1个文件压缩到一个新的存档中。下面的代码可以工作。。。。大多数时候。然而,偶尔(网络问题?)我会更改代码(见下文)以创建一个唯一的名称,但偶尔还是会出现此错误

有没有办法解决这个问题?我在powershell 3上,所以压缩存档不是一个选项,尽管我可能可以安装pscx

Exception calling "Open" with "2" argument(s): "The process cannot access the file '\\path\share$\folder\filename_20200221_14__20200224_102143.zip' because it is being used by another process."
At line:3 char:5
+     [System.IO.Compression.ZipArchive]$ZipFile = [System.IO.Compression.ZipFile] ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : IOException
这是我正在使用的代码

我正在执行一个“foreach($files\u to\u process中的file)”,但是通过使用foreach{}设置变量,然后运行下面的代码,可以复制错误。自然,后续运行正常

$timestamp = (get-date).ToString("yyyyMMdd_HHmmss")

# Now create the zip file and remove the old one 
[string]$zipFN = "$OnPremDirectoryDone$($file.basename)__$($timestamp).zip"
[string]$fileToZip = "$OnPremDirectory$($file.name)"
[System.IO.Compression.ZipArchive]$ZipFile = [System.IO.Compression.ZipFile]::Open($zipFN, ([System.IO.Compression.ZipArchiveMode]::Update))
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($ZipFile, $fileToZip, (Split-Path $fileToZip -Leaf))
$ZipFile.Dispose()
remove-item "$fileToZip"

我猜该文件可能位于共享文件服务器或多个用户可以访问的系统上?这似乎不是PowerShell问题,更重要的是该文件已在使用中?转到compmgmt-shares,查找可能正在使用文件本身的打开会话。@matthew我希望你是对的。是的,它是,但文件夹本身位于一个足够专门的子文件夹中,我是唯一一个在那里查看的人。创建文件(Open$zipFN)和添加文件(CreateEntryFromFile)之间的时间足够短,我无法想象任何东西会击中它。当它工作时,大约需要一秒钟的时间才能到达Dispose,这似乎就是它实际写入磁盘的原因。不过,谢谢!抛出异常的是.Open()方法。如果在循环中使用此代码,则您将遇到一个竞争条件,即文件在再次访问之前尚未完成添加到zip。如果这不在循环中,那么其他进程会打开您的文件。您需要查看访问该文件的句柄,以确定打开该文件的进程(例如AV、备份软件、powershell.exe等)