2017-09-11 52 views
0

那裏! 這是我的目標,我試圖用Powershell腳本來完成。 我有管理用戶需要能夠從密碼保護的zip文件中提取csv文件並通過電子郵件發送此csv文件。 鑑於目錄中有許多用戶名(例如dana.zip)命名的zip文件,並使用相同的密碼(123456)進行保護。管理用戶(知道zip文件的密碼)需要運行powershell腳本,該腳本要求輸入所需的用戶名,然後執行它的工作人員 - 將文件解壓縮到同一目錄並通過電子郵件發送。 到目前爲止,我發現並採用了以下需要以下PowerShell腳本。解壓密碼保護的ZIP文件並通過電子郵件發送它的內容使用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 Error Message

如果可以使這個腳本使用加密的密碼文件,這將是非常有益的...

第二個腳本 - 通過電子郵件發送提取的文件。

首先,我創建加密密碼文件(在這個腳本它的完美的工作):

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

這裏是腳本本身:

$User = "[email protected]" 
$File = "W:\Admin\EmailPassword.txt" 
$cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString) 
$EmailTo = "[email protected]" 
$EmailFrom = "[email protected]" 
$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 = "[email protected]" 
$File = "W:\Admin\EmailPassword.txt" 
$cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString) 
$EmailTo = "[email protected]" 
$EmailFrom = "[email protected]" 
$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文件。

謝謝你在前進,

伊戈爾。

+0

你說:'所需的文件將被提取至W:在你的用戶指令\\管理folder',然後你的代碼顯示爲:'$ zipFile ='「W:\ ADMINISTRATION \ $ User.zip」''你在那裏是否缺少一個「\」? – rossum

+0

不,這只是錯誤的錯誤...應該是:W:\ ADMINISTRATION ... –

+0

我已經找出問題#2有什麼問題, - 我已經在腳本的第一部分中使用了相同的變量$ User第二...所以第二個$用戶變量覆蓋已經存在,第一個。這就是爲什麼文件沒有附加到電子郵件。我已經將$ User變量更改爲$用戶名在腳本的第一部分,現在都很好: $ Username = Read-Host -Prompt'請輸入所需用戶名' 因此,需要幫助來解決問題1和3 ... –

回答

0

,我已經修復所有問題「谷歌搜索」他們之後,所以我可以分享我的經驗...

  1. In the first portion of script, use encrypted password file instead of plain text

像TheIncorrigible1在他的回答中提到我的問題:

You cannot completely avoid having a reversible password in your script while using an external command like 7z unless it also supports encrypted passwords being passed to it. SecurePassword is a Windows-specific construct.

它看起來像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. In the second portion, adopt user input from the first part of the script ($User) to be used as file name (for instance, if user input was "dana" , $User.csv will be equal to dana.csv)

這是我的錯, - 我在腳本的第一部分和第二部分中使用了相同的變量$ User,因此腳本的第二部分中的$ User變量覆蓋現有的一個...因此文件未附加到電子郵件。在腳本的第一部分改變變量後,問題消失:

$Username = Read-Host -Prompt 'Please Input Desired User Name'.... 
$zipFile = "W:\ADMINISTRATION\$UserName.zip"... 
$filenameAndPath = "W:\ADMINISTRATION\$UserName.csv" 
  1. Remove *.csv file after mail was sent. I've accomplished this goal using separate PowerShell script:
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 
相關問題