繁体   English   中英

解压缩密码保护的ZIP文件并使用Powershell通过电子邮件发送其内容

[英]Unzip Password Protected ZIP File And Send It's Content By Email Using Powershell

那里!! 这是我的目标,我正在尝试使用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

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

Powershell错误消息

如果可以使该脚本使用加密的密码文件,那对我非常有益。

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

首先,我创建了加密的密码文件(在此脚本中,它可以正常工作):

"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文件。

先感谢您,

伊戈尔

在“搜索”它们之后,我已修复所有问题,因此我可以分享我的经验...

  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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM