我有一个zip文件,在提取时要求输入密码。在Powershell脚本中提取密码时如何提供该密码?
我一直在研究Expand-Archive,但未显示如何插入此密码。我已经在文本文件中加密了密码:
"P@ssword1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\Temp 2\Password.txt"
如何将密码从文件输入到Expand-Archive命令?或还有什么其他选择?
答案 0 :(得分:3)
根据内置帮助文件和在线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命名空间,“ virtual
”也不提供它。尽管有一个项目已经存在很长时间了,但是使用.Net可以做到这一点。
旧项目 About DotNetZip - DotNetZip Documentation
但是,这不是特定于PowerShell的。就像7zip,WinZip等一样,它只是一个独立的“ .exe”来使用。但是,如果您愿意挖掘源代码并将其转换为要使用的模块,则这是您的决定。
要点:
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。
NuGet Gallery | DotNetZip 1.13.5
PowerShell: Running Executables
Solve Problems with External Command Lines in PowerShell
Top 5 tips for running external commands in Powershell
Using Windows PowerShell to run old command-line tools (and their weirdest parameters)
这样做时报价也很重要
所以,你最终会得到这个...
# 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。