继续我的评论。选择资源,根据需要进行审查和调整。
'powershell 7ip folders with password'
点击示例
PowerShell function to create a password protected zip file
Download the function script
function Write-ZipUsing7Zip
{
Param
(
[string]$FilesToZip,
[string]$ZipOutputFilePath,
[string]$Password,
[ValidateSet('7z','zip','gzip','bzip2','tar','iso','udf')]
[string]$CompressionType = 'zip',
[switch]$HideWindow
)
# Look for the 7zip executable.
$pathTo32Bit7Zip = "C:\Program Files (x86)\7-Zip\7z.exe"
$pathTo64Bit7Zip = "C:\Program Files\7-Zip\7z.exe"
$THIS_SCRIPTS_DIRECTORY = Split-Path $script:MyInvocation.MyCommand.Path
$pathToStandAloneExe = Join-Path $THIS_SCRIPTS_DIRECTORY "7za.exe"
if (Test-Path $pathTo64Bit7Zip) { $pathTo7ZipExe = $pathTo64Bit7Zip }
elseif (Test-Path $pathTo32Bit7Zip) { $pathTo7ZipExe = $pathTo32Bit7Zip }
elseif (Test-Path $pathToStandAloneExe) { $pathTo7ZipExe = $pathToStandAloneExe }
else { throw "Could not find the 7-zip executable." }
# Delete the destination zip file if it already exists (i.e. overwrite it).
if (Test-Path $ZipOutputFilePath)
{ Remove-Item $ZipOutputFilePath -Force }
$windowStyle = "Normal"
if ($HideWindow) { $windowStyle = "Hidden" }
# Create the arguments to use to zip up the files.
# Command-line argument syntax can be found at: http://www.dotnetperls.com/7-zip-examples
$arguments = "a -t$CompressionType ""$ZipOutputFilePath"" ""$FilesToZip"" -mx9"
if (!([string]::IsNullOrEmpty($Password)))
{ $arguments += " -p$Password" }
# Zip up the files.
$p = Start-Process $pathTo7ZipExe -ArgumentList $arguments -Wait -PassThru -WindowStyle $windowStyle
# If the files were not zipped successfully.
if (!(($p.HasExited -eq $true) -and ($p.ExitCode -eq 0)))
{
throw "There was a problem creating the zip file '$ZipFilePath'."
}
}
顺便说一句,Microsoft powershellgallery.com 中有适用于 7Zip 的 PowerShell 附加模块。
Find-Module -Name '*zip*' | Format-Table -AutoSize
# Results
<#
Version Name Repository Description
------- ---- ---------- -----------
1.13.0 7Zip4Powershell PSGallery Powershell module for creating and extracting 7-Zip archives
0.3.1 Zip PSGallery PowerShell module for creating ZIP archives.
2.2.0 PS7Zip PSGallery Powershell module that allows you to work with compressed archives
1.0.8 x7Zip PSGallery Powershell DSC Configuration Script for installing 7-Zip versions 15.07, 15.06, 15.05, 9.38, and 9.20. This Configuration Script...
1.3.4 7ZipArchiveDsc PSGallery PowerShell DSC Resource to expand an archive file to a specific path.
0.2.2 ziphelper PSGallery Tools to work with zip archives
1.1.0 7zip-Archive PSGallery 7zip utility wrapper....
0.1.1 Get-GzipContent PSGallery Gets the content of the gzip archive at the specified location.
#>
看这篇文章就可以了。
https://www.sans.org/blog/powershell-7-zip-module-versus-compress-archive-with-encryption