首页 > 解决方案 > 解压缩 .zip 文件时提供密码

问题描述

我有一个 zip 文件,在提取时要求输入密码。如何在 powershell 脚本中提取此密码时提供此密码?

我一直在查看 Expand-Archive,但它没有显示如何插入此密码。我已经在文本文件中加密了密码:

"P@ssword1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\Temp 2\Password.txt"

如何将文件中的密码提供给 Expand-Archive 命令?或者还有什么其他选择?

标签: powershell

解决方案


根据内置帮助文件和在线 PowerShell 文档,Expand-Archive cmdlet 没有为此用例提供任何方法,也没有添加密码的选项。

所有提供这种机制的 cmdlet 都有一个“-Credential”开关。

# Find all cmdlets / functions with a target parameter
Get-Command -CommandType Function | 
Where-Object { $_.parameters.keys -match 'credential'} | 
Out-GridView -PassThru -Title 'Available functions which has a specific parameter'

Get-Command -CommandType Cmdlet | 
Where-Object { $_.parameters.keys -match 'credential'} | 
Out-GridView -PassThru -Title 'Results for cmdlets which has a specific parameter'

即使是本机 .Net 命名空间,“ System.IO.Compression ”也没有提供它。尽管有一个项目已经存在了很长一段时间,但可以使用 .Net 来实现这一点。

关于 DotNetZip - DotNetZip 文档

旧项目 DotNetZip - CodeProject

分叉版 NuGet 库 | DotNetZip 1.13.5

然而,这并不是特定于 PowerShell 的。它只是一个独立的“.exe”来使用,就像 7zip、WinZip 等一样。但是,如果您愿意挖掘源代码并将其转换为要使用的模块,那是您自己的决定。

注意事项:

MS powershellgallery.com 中已经有模块已经做到了这一点。

Find-Package -Name '*DotNetZip*'

Name                           Version          Source           Summary                                                                                            
----                           -------          ------           -------                                                                                            
DotNetZip                      1.13.5           nuget.org        A library for dealing with zip, bzip and zlib from .Net                                            
DotNetZip.Reduced              1.9.1.8          nuget.org        DotNetZip is an easy-to-use, FAST, FREE class library and toolset for manipulating zip files or ...
XAct.IO.Compression.DotNetZip  1.1.17040.3290   nuget.org        An XActLib assembly: a library to work with the essentials of DotNetZip.                           
VirtualPath.DotNetZip          0.3.9            nuget.org        DotNetZip library wrapper for VirtualPath                                                          
DotNetZip.NetStandard          1.12.0           nuget.org        A library for dealing with zip, bzip and zlib from .Net                                            
DotNetZip.Zlib.vNextUnofficial 1.9.8.3          nuget.org        DotNetZip vNext port                                                                               
DotNetZip.Zip.vNextUnofficial  1.9.8.3          nuget.org        DotNetZip vNext port                                                                               
DotNetZip.Android              1.13.5           nuget.org        A library for dealing with zip, bzip and zlib from .Net                                            
DotNetZip.iOS                  1.13.5           nuget.org        A library for dealing with zip, bzip and zlib from .Net                                            
Bardock.Utils.Web.Mvc.DotNe... 1.0.0            nuget.org        MVC and DotNetZip utilities
...
RI.Framework.Extensions.Dot... 0.7.0            nuget.org        Decoupling & Utilities Framework                                                                   
DotNetZipforWindows8.1         1.0.0            nuget.org        i make same changes to make this is a package for zip file with password for windows 8.1 and win...

因此,请使用上面的模块,或者您需要使用 3rdP 工具,例如 7zip 等...

C:\>"C:\Program Files\7-Zip\7z.exe" a D:\Temp\mytest.zip D:\Temp\mytest.txt -pSecret

7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21

Scanning the drive:
1 file, 64 bytes (1 KiB)

Creating archive: D:\Temp\mytest.zip

Add new data to archive: 1 file, 64 bytes (1 KiB)


Files read from disk: 1
Archive size: 230 bytes (1 KiB)
Everything is Ok

C:\>"C:\Program Files\7-Zip\7z.exe" x D:\Temp\mytest.zip D:\Temp\mytest.txt -pSecret

7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21

Scanning the drive for archives:
1 file, 230 bytes (1 KiB)

Extracting archive: D:\Temp\mytest.zip
--
Path = D:\Temp\mytest.zip
Type = zip
Physical Size = 230


No files to process
Everything is Ok

Files: 0
Size:       0
Compressed: 230

有几篇关于如何通过 PowerShell 正确利用外部 cmd 的文章。

PowerShell:运行可执行文件

解决 PowerShell 中的外部命令行问题

在 Powershell 中运行外部命令的 5 大技巧

使用 Windows PowerShell 运行旧的命令行工具(以及它们最奇怪的参数)

在 PowerShell 中正确执行外部命令

https://mnaoumov.wordpress.com/2015/03/31/execution-of-external-commands-native-applications-in-powershell-done-right-part-2

https://mnaoumov.wordpress.com/2015/04/05/execution-of-external-commands-native-applications-in-powershell-done-right-part-3

执行此操作时引用也很重要

引用细节

所以,你最终会得到这个......

# Using the call operator
& 'C:\Program Files\7-Zip\7z.exe' a 'D:\Temp\mytest.zip' 'D:\Temp\mytest.txt' -pSecret
& 'C:\Program Files\7-Zip\7z.exe' x 'D:\Temp\mytest.zip' 'D:\Temp\mytest.txt' -pSecret

或者使用具有相同字符串的 Start-Process。


推荐阅读