【问题标题】:Unzip Password Protected ZIP File And Send It's Content By Email Using Powershell解压缩受密码保护的 ZIP 文件并使用 Powershell 通过电子邮件发送其内容
【发布时间】:2018-02-19 16:04:14
【问题描述】:

那里!! 这是我的目标,我正在尝试使用 Powershell 脚本来完成。 我有需要能够从受密码保护的 zip 文件中提取 csv 文件并通过电子邮件发送此 csv 文件的管理用户。 给定目录有许多以用户名命名的 zip 文件(例如 dana.zip)并使用相同的密码(123456)保护。管理用户(知道 zip 文件的密码)需要运行 powershell 脚本,该脚本要求输入所需的用户名,然后执行它的工作人员 - 将文件提取到同一目录并通过电子邮件发送。 到目前为止,我发现并采用了以下 powershell 脚本来满足上述需求。

解压受密码保护的文件:

$7ZipPath = '"C:\Program Files\7-Zip\7z.exe"'
$User = Read-Host -Prompt 'Please Input Desired User Name'
write-host ""
write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
write-host ""
write-host " Desired file will be extracted to W:\ADMINISTRATION folder " -foregroundcolor Cyan
write-host ""
write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
write-host ""
$zipFile = '"W:\ADMINISTRATION\$User.zip"'
$zipFilePassword = "123456"
$command = "& $7ZipPath e -oW:\ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile"
iex $command

这个脚本在做它的工作,但我试图避免在脚本中使用密码作为纯文本。由于此脚本将在同一管理用户帐户下运行,因此我尝试在脚本中使用加密的密码文件。

首先,我运行以下命令来创建加密密码文件:

"123456" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "W:\Admin\ZipPassword.txt"

后来我采用了我的脚本来使用加密的密码文件:

$7ZipPath = '"C:\Program Files\7-Zip\7z.exe"'
$User = Read-Host -Prompt 'Please Input Desired User Name'
write-host ""
write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
write-host ""
write-host " Desired file will be extracted to W:\\ADMINISTRATION folder " -foregroundcolor Cyan
write-host ""
write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
write-host ""
$zipFile = '"W:\ADMINISTRATION\$User.zip"'
$cred = Get-Content "W:\Admin\ZipPassword.txt" | ConvertTo-SecureString
$zipFilePassword = new-object -typename System.Management.Automation.PSCredential -argumentlist ($cred)
$command = "& $7ZipPath e -oW:\ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile"
iex $command

运行此脚本时出现以下错误:

如果可以让这个脚本使用加密的密码文件,那将是非常有益的...

第二个脚本 - 通过电子邮件发送提取的文件。

首先,我创建了加密的密码文件(在这个脚本中它运行良好):

"myPassword" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "W:\Admin\EmailPassword.txt"

这是脚本本身:

$User = "me.me@gmail.com"
$File = "W:\Admin\EmailPassword.txt"
$cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
$EmailTo = "me.me@gmail.com"
$EmailFrom = "me.me@gmail.com"
$Subject = "Some text here"
$Body = "Some text here"
$SMTPServer = "smtp.gmail.com"
$filenameAndPath = "W:\ADMINISTRATION\dana.csv"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
$SMTPClient.Send($SMTPMessage)
write-host "Mail Sent Successfully !!"  -foregroundcolor Green

此脚本按预期工作...唯一的问题是管理用户每次都需要对其进行编辑并输入正确的文件名(dana.csv、david.csc...等)。当然我也可以在这个脚本中使用用户输入法,但是我想将两个脚本合并为一个......到目前为止我尝试了这个:

$7ZipPath = '"C:\Program Files\7-Zip\7z.exe"'
$User = Read-Host -Prompt 'Please Input Desired User Name'
write-host ""
write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
write-host ""
write-host " Desired file will be extracted to W:\\ADMINISTRATION folder " -foregroundcolor Cyan
write-host ""
write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
write-host ""
$zipFile = '"W:\ADMINISTRATION\$User.zip"'
$zipFilePassword = "123456"
$command = "& $7ZipPath e -oW:\ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile"
iex $command

$User = "me.me@gmail.com"
$File = "W:\Admin\EmailPassword.txt"
$cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
$EmailTo = "me.me@gmail.com"
$EmailFrom = "me.me@gmail.com"
$Subject = "Some text here"
$Body = "Some text here"
$SMTPServer = "smtp.gmail.com"
$filenameAndPath = "W:\ADMINISTRATION\$User.csv"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
$SMTPClient.Send($SMTPMessage)
write-host "Mail Sent Successfully !!"  -foregroundcolor Green

但无法将文件附加到电子邮件。我想我这里有问题(语法错误):

$filenameAndPath = "W:\ADMINISTRATION\$User.csv"

所以,如果有人可以帮助我解决以下问题,将不胜感激:

  1. 在脚本的第一部分,使用加密的密码文件而不是纯文本
  2. 在第二部分,采用来自脚本第一部分的用户输入 ($User) 作为文件名(例如,如果用户输入是 "dana" ,则 $User.csv 将等于 dana。 csv)
  3. 发送邮件后删除 *.csv 文件。

提前谢谢你,

伊戈尔。

【问题讨论】:

  • 您在用户说明中说:Desired file will be extracted to W:\\ADMINISTRATION folder,然后您的代码显示为:$zipFile = '"W:\ADMINISTRATION\$User.zip"' 您是否缺少“\”?
  • 不,只是拼写错误...应该是:W:\ADMINISTRATION...
  • 我已经弄清楚问题 #2 出了什么问题, - 我在脚本的第一部分和第二部分中使用了相同的变量 $User ... 所以第二个 $User 变量覆盖已经存在,第一个。这就是文件未附加到电子邮件的原因。我已在脚本的第一部分将 $User 变量更改为 $Username,现在一切正常: $Username = Read-Host -Prompt 'Please Input Desired User Name' 所以,需要帮助解决问题 1 和 3...
  • 在使用像7z 这样的外部命令时,您无法完全避免在脚本中使用可逆密码,除非它还支持将加密密码传递给它。 SecurePassword 是特定于 Windows 的构造。

标签: powershell csv email encryption


【解决方案1】:

我在“谷歌搜索”后解决了所有问题,所以我可以分享我的经验...

  1. 在脚本的第一部分,使用加密的密码文件而不是纯文本

就像 TheIncorrigible1 在他对我的问题的回答中提到的那样:

您无法完全避免在您的 使用 7z 等外部命令时编写脚本,除非它也支持 传递给它的加密密码。 SecurePassword 是一个 特定于 Windows 的构造。

看起来 7zip 不支持 Windows 特定的加密密码,所以我需要在使用前将其转换回纯文本。 我在这些网站上找到了如何做到这一点: https://blogs.msdn.microsoft.com/timid/2009/09/10/powershell-one-liner-decrypt-securestring/#comment-6495http://www.travisgan.com/2015/06/powershell-password-encryption.html

因此,这种和平的代码帮助我解决了加密密码问题:

$Password = Get-Content 'W:\Administration\EncryptedPasswords\ZipPassword.txt'| ConvertTo-SecureString 
$Marshal = [System.Runtime.InteropServices.Marshal]
$Bstr = $Marshal::SecureStringToBSTR($Password)
$ZipPassword = $Marshal::PtrToStringAuto($Bstr)
$Marshal::ZeroFreeBSTR($Bstr)
  1. 在第二部分,采用来自脚本第一部分 ($User) 的用户输入作为文件名(例如,如果用户输入 是 "dana" ,$User.csv 将等于 dana.csv)

这是我的错误,我在脚本的第一部分和第二部分使用了相同的变量 $User,因此脚本第二部分中的 $User 变量覆盖了现有变量...因此文件未附加到电子邮件.在脚本的第一部分更改变量后,问题就消失了:

$Username = Read-Host -Prompt 'Please Input Desired User Name'....
$zipFile = "W:\ADMINISTRATION\$UserName.zip"...
$filenameAndPath = "W:\ADMINISTRATION\$UserName.csv"
  1. 在发送邮件后删除 *.csv 文件。 我已经使用单独的 PowerShell 脚本实现了这个目标:
Get-ChildItem W:\Administration\*.csv | Where { ! $_.PSIsContainer } | remove-item

write-host " ---------------------------------------------- " -foregroundcolor DarkCyan 
write-host "" 
write-host "     All Operations Completed Successfully !!"     -foregroundcolor Green                    
write-host ""
write-host " ---------------------------------------------- " -foregroundcolor DarkCyan

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    相关资源
    最近更新 更多