Powershell 解压缩.zip文件时提供密码

Powershell 解压缩.zip文件时提供密码,powershell,Powershell,我有一个zip文件,在解压缩时要求输入密码。如何在powershell脚本中提取此密码时提供此密码 我一直在查看扩展存档,但它没有显示如何插入此密码。我已经在文本文件中对密码进行了加密: "P@ssword1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\Temp 2\Password.txt" 如何将密码从文件馈送到Expand Archive命令?或者还有什么其

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

我一直在查看扩展存档,但它没有显示如何插入此密码。我已经在文本文件中对密码进行了加密:

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

如何将密码从文件馈送到Expand Archive命令?或者还有什么其他选项?

根据内置的帮助文件和联机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命名空间也不提供它。尽管有一个项目已经存在很长一段时间了,可以用.Net来实现这一点

老项目

分叉版本

然而,这并不是特定于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

进行此操作时,引用也很重要

所以,你最终会得到这个

# 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

或者使用同一字符串启动进程。

根据内置帮助文件和联机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命名空间也不提供它。尽管有一个项目已经存在很长一段时间了,可以用.Net来实现这一点

老项目

分叉版本

然而,这并不是特定于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

进行此操作时,引用也很重要

所以,你最终会得到这个

# 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

或者使用相同字符串的启动进程。

我安装了一个名为7Zip4Powershell的模块,并提供了以下命令来提取一个受密码保护的.zip文件

Expand-7Zip -ArchiveFileName "$($downloadedPath)\$($library.Name)" -TargetPath $extractedPath -SecurePassword (Get-Content $unpackingPassword | ConvertTo-SecureString) 

我安装了一个名为7Zip4Powershell的模块,并提供了以下命令来提取一个受密码保护的.zip文件

Expand-7Zip -ArchiveFileName "$($downloadedPath)\$($library.Name)" -TargetPath $extractedPath -SecurePassword (Get-Content $unpackingPassword | ConvertTo-SecureString) 
据我所知,Expand Archive cmdlet不支持处理受密码保护的存档。您将不得不使用另一个util—可能是7zip?的命令行版本—从我所知,Expand Archive cmdlet不支持处理受密码保护的存档。您将不得不使用另一个util-可能是7zip的命令行版本?