簡體   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