Powershell - 如何在PowerShell中完成/关闭zip文件

Eri*_*lik 5 powershell zip

我想在powershell中创建一个zip文件,将项目添加到zip文件,然后在创建zip文件后立即将该zip文件的压缩内容作为字节,在同一个scipt中.问题是,zip应用程序似乎没有将其内容写入文件系统.如何关闭/刷新zip应用程序,以便下一个powershell语句可以访问新创建的zip文件?

例:

new-item -type File test.zip -force
$zip = ( get-item test.zip ).fullname
$app = new-object -com shell.application
$folder = $app.namespace( $zip )
$item = new-item file.txt -itemtype file -value "Mooo" -force
$folder.copyhere( $item.fullname )
dir test.zip # <---- Empty test.zip file
Get-Content -Encoding byte $zip | echo # <-- nothing echoed
Run Code Online (Sandbox Code Playgroud)

"dir test.zip"显示没有内容的zip文件,因此Get-Content不返回任何内容.

请注意,这似乎是copyhere操作的异步行为的问题.如果我在copyhere行之后睡觉,zip文件将被填充.但是,我不知道必须睡多久,也不想延迟处理.

非常感谢提前!

GMa*_*er9 0

这种压缩文件的方法不适合自动化脚本。这在 Windows 2003 和 Windows xp 服务器上有 3 GB 的限制。此外,它不会给出适当的错误。