powershell - 解压缩 .zip 文件时提供密码
问题描述
我有一个 zip 文件,在提取时要求输入密码。如何在 powershell 脚本中提取此密码时提供此密码?
我一直在查看 Expand-Archive,但它没有显示如何插入此密码。我已经在文本文件中加密了密码:
"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 命名空间,“ System.IO.Compression ”也没有提供它。尽管有一个项目已经存在了很长一段时间,但可以使用 .Net 来实现这一点。
分叉版 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 的文章。
执行此操作时引用也很重要
所以,你最终会得到这个......
# 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。
推荐阅读
- php - Redis 中的 FLUSHALL ASYNC 线程是否安全?
- android - android phone linux deploy docker systemctl 在 chroot 中运行,忽略请求。问题
- php - 如何删除数组前后的“”
- ios - 如何在 Swift 4 中显示没有 UIViewController 的新屏幕?
- encryption - 是否所有功能都属于 FAST API 中可用的 Django Rest Framework 来开发 REST APIS
- cron - 在 12:00 AM 到 12:59 AM 之间每 20 分钟执行一次 Cron 表达式,仅在星期六
- backend - 在没有浏览器的情况下运行 DHTMLX API
- c++ - 单击一个按钮后创建一个按钮
- vue.js - 如果在所选语言文件中没有找到翻译,如何显示英文翻译?
- css - 如何使用 CSS 重现设备下的阴影