2017-09-11 19 views
0

そこに!! 私の目標は、Powershellスクリプトで達成しようとしています。 パスワードで保護されたzipファイルからcsvファイルを抽出し、このcsvファイルを電子メールで送信する必要がある管理者がいます。 指定されたディレクトリには、username(たとえば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 

このスクリプトはそれが仕事だことが、私は、スクリプトにプレーンテキストとしてパスワードの使用を避けるためにしようとしています。このスクリプトは同じ管理者ユーザーアカウントで実行されるため、スクリプトで暗号化されたパスワードファイルを使用しようとしました。私はこのスクリプトを実行すると

$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 

:その後、私は暗号化されたパスワードファイルを使用するように私のスクリプトを採用しました

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

まず、私は暗号化されたパスワード・ファイルを作成するには、次のコマンドを実行しました次のエラーが発生する:

Powershell Error Message

このスクリプトで暗号化されたパスワードファイルを使用することができれば、それは私に非常に有益です...

2番目のスクリプト - メールで抽出ファイルを送信します。

まず、私は(これは完全に働いて、このスクリプトで)暗号化されたパスワードファイルを作成しました:

$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 

このスクリプトは、期待通りに動作:ここ

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

、スクリプト自体はあります...唯一の問題は、管理者が毎回それを編集し、適切なファイル名(dana.csv、david.csc ...など)を入れる必要があることです。もちろん、私もこのスクリプトでは、ユーザの入力方法を使用することができますが、私は単一のものに両方のスクリプトを組み合わせたい...これまでのところ私はこの1つ試してみました:

$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. 2番目の部分では、スクリプトの最初の部分($ User)からのユーザー入力をファイル名として使用します(たとえば、ユーザー入力が "dana"、$ Userの場合など)。 csvはdana.csvと等しくなります)
  3. メールが送信された後に* .csvファイルを削除します。

、事前に

イゴール、ありがとうございました。

+0

あなたは言う: '目的のファイルがWに抽出されます:\\ユーザーの指示で管理folder'、その後、 '$ zipFile = '" W:\ ADMINISTRATION \ $ User.zip "''そこに「\」がありませんか? – rossum

+0

いいえ、それはちょうどタイプミスです:W:\ ADMINISTRATION ... –

+0

私は問題#2で何が間違っているかを理解しました - 私はスクリプトの最初の部分で同じ変数$ Userを使用していますsecond ... so second $ User変数はすでに存在しています。それがファイルが電子メールに添付されなかった理由です。 $ Username = Read-Host -Prompt '希望するユーザー名を入力してください' 問題1と3を修正するには、ヘルプが必要です。スクリプトの最初の部分で$ User変数を$ Usernameに変更しました。 ... –

答えて

0

TheIncorrigible1は私の質問に彼の答えで述べたように私はそれらを「グーグル」の後にすべての問題を修正した、だから私は、私の経験を共有することができます...

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

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.

Windows固有の暗号化されたパスワードをサポートしていない7zipのように見えるので、使用前にプレーンテキストに変換する必要があります。 私はこれらのサイトでそれを行う方法を発見しました: 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)

これは私の間違いでした、 - スクリプトの第1部分と第2部分で同じ変数$ Userを使用したので、スクリプトの2番目の部分の$ 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 
関連する問題