我有一个 zip 文件,在提取时要求输入密码。在powershell脚本中提取密码时如何提供密码?
我一直在查看 Expand-Archive,但它没有显示如何插入此密码。我已经在文本文件中加密了密码:
"P@ssword1" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\Temp 2\Password.txt"
Run Code Online (Sandbox Code Playgroud)
如何将文件中的密码提供给 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'
Run Code Online (Sandbox Code Playgroud)
即使是原生的 .Net 命名空间,“ System.IO.Compression ”也不提供它。尽管有一个项目已经存在很长时间了,可以用 .Net 来做到这一点。
分叉版 NuGet Gallery | 点网压缩 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...
Run Code Online (Sandbox Code Playgroud)
因此,请使用上面的模块,否则您需要使用 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
Run Code Online (Sandbox Code Playgroud)
有几篇关于如何通过 PowerShell 正确利用外部 cmds 的文章。
这样做时引用也很重要
所以,你最终会得到这个......
# 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
Run Code Online (Sandbox Code Playgroud)
或者使用具有相同字符串的 Start-Process。
| 归档时间: |
|
| 查看次数: |
1791 次 |
| 最近记录: |