那裏! 這是我的目標,我試圖用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
運行此腳本我會出現以下錯誤:
如果可以使這個腳本使用加密的密碼文件,這將是非常有益的...
第二個腳本 - 通過電子郵件發送提取的文件。
首先,我創建加密密碼文件(在這個腳本它的完美的工作):
"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"
所以,如果有人能幫助我解決以下問題,這將非常感激:
- 在腳本的第一部分,使用加密的密碼文件而不是純文本
- 在第二部分中,從腳本的第一部分($ User)採用用戶輸入作爲文件名(例如,如果用戶輸入是「dana」,$ User)。 csv將等於dana.csv)
- 發送郵件後刪除* .csv文件。
謝謝你在前進,
伊戈爾。
你說:'所需的文件將被提取至W:在你的用戶指令\\管理folder',然後你的代碼顯示爲:'$ zipFile ='「W:\ ADMINISTRATION \ $ User.zip」''你在那裏是否缺少一個「\」? – rossum
不,這只是錯誤的錯誤...應該是:W:\ ADMINISTRATION ... –
我已經找出問題#2有什麼問題, - 我已經在腳本的第一部分中使用了相同的變量$ User第二...所以第二個$用戶變量覆蓋已經存在,第一個。這就是爲什麼文件沒有附加到電子郵件。我已經將$ User變量更改爲$用戶名在腳本的第一部分,現在都很好: $ Username = Read-Host -Prompt'請輸入所需用戶名' 因此,需要幫助來解決問題1和3 ... –