Writes the creating mail code to the call, writes the Mailxxx.csv file to the Outbox, and records the main contents of the message in the face.
Write
#template $TMP = import-csv "$ ($DH) \mail\templates\mail. CSV "#customization $TMP. CC = "" # $TMP. Subject = "from Server $ ($ (get-wmiobject Win32_ComputerSystem). Name) by Apoms_tsql_deployment_package $ (get-date). "# test1 $TMP. Body = "$msg ' nplease Check the attachement for details. ' Nthanks ' Nbest regards. "# test# #clear if (test-path" $logfile. zip ") {remove-item-path" $logfile. zip "-force|& $log} #create Zip "zipping log ..." Export-zip-sourcefiles "$logfile"-zipfilename "$logfile. Zip" |& $log #add attachements$tmp. Attachments = "$logfile. zip,$ ($DH) \mail\msg.txt" # #wirte-outbox,output mailxxx.csv convertto-mail-path "$ ($DH) \ Mail\outbox\mail$t.csv "-mail $tmp
Send, call send-avmail.ps1 send mail
Sample
CLS; $dh = $pwd. path# $PSScriptRoot =split-path-parent $MyInvocation. MYCOMMAND.DEFINITION$PV = $ Psversiontable.psversion.major$strdate = Get-date-format "yyyyMMdd" $logfile = "$ ($DH) \log\log$ ($strDate). txt" $log = { $input | Tee-object-filepath $logfile-append} $filename = "$rem = {$i ="; 1..75|%{$i + = '-'}; $i} #getting order "start deploying ' Nat $ (get-date) ' |& $log "Reading configuration file ..." |& $log $content = (get-content "$ ($DH) \module\config_ Order "| out-string) $hash = (invoke-expression $content) # $hash #sql parameters$content = (get-content "$ ($DH) \module\config_sys "| out-string) $hashsys = (invoke-expression $content) & $rem |& $logtry {#Display #sorting hashtable $hash. Gete Numerator () |sort name|& $log & $rem |& $log "echo the Depoly would start immediately" |& $logsleep 1|& $log & $rem |& $log #hit by order foreach ($e in $hash. GetEnumerator () |sort name) {if ($dh-ne $pwd. Path) {Set-locat Ion $dh} $key = $e. Value ".......... $key ................. "| & $log $cmd = "$ ($DH) \module\runsqlcmdbykey.ps1" $dh |& $log invoke-expression-command $cmd |& $log }}catch {"$ (get-date) error:$_. Exception "|& $log}finally{" finish deploying at $ (get-date) "|& $log $title =" Send mail "$message =" does you want to Sen D The logfile to iSoftStone? "$yes = New-object System.Management.Automation.Host.ChoiceDescription" &yes ", '" Send th e logfile to iSoftStone. "$no = New-object System.Management.Automation.Host.ChoiceDescription" &no "," Continue. "$ options = [system.management.automation.host.choicedescription[]] ($yes, $no) $result = $host. Ui.promptforchoice ($ Title, $message, $options, 1) switch ($result) {0 {#import mail moudle "Creating Attachement" Import-module "$ ($DH) \mail\ Mailmodule.psm1 "-force $t = Get-date-format ' Yyyymmdd_hh_mm_ss '." $ ($DH) \mail\msg.ps1 "Creating Mail" $msg = "FYI." #template $TMP = import-csv "$ ($DH) \mail\templates\mail. CSV "#customization $TMP. CC = "" # $TMP. Subject = "from Server $ ($ (get-wmiobject Win32_ComputerSystem). Name) by Apoms_tsql_deployment_package $ (get-date). "# Test1 $TMP. Body = "$msg ' nplease Check the attachement for details. ' Nthanks ' Nbest regards. "# test# #clear if (test-path" $logfile. zip ") {remove-item-path" $logfile. zip "-force|& $log} #create zip "zipping log ..." Export-zip-sourcefiles "$logfile"-zipfilename "$logfile. Zip" |& $log #add attachements$tmp. Attachments = "$logfile. zip,$ ($DH) \mail\msg.txt" # #wirte-outbox,output mailxxx.csv convertto-mail-path "$ ($DH) \mail\ Outbox\mail$t.csv "-mail $tmp #start sending" praparing to send "sleep" sending email ... "." $ ($DH) \mail\send-avemail.ps1 "|& $log}1 {}}}
Send-avmail.ps1
#cls #paths $DH = $pwd. PATH$PV = $psversiontable. psversion.major$strdate = Get-date-format "YyyyMMdd" #log $log _file = "$ ( $DH) \mail\log\log$ ($strDate). txt "$log = {$input | Tee-object-filepath $log _file-append} $filename = ' $rem = {$i = '; 1..75|%{$i + = '-'}; $i} ############################## # #START ###################################### #remember $curentFile = "$pop = $pwd $path =" $ ($DH) \mail "CD $path # Bodytry {if (Test-path-path $path) {import-module "$path \mailmodule.psm1"-force $t = Get-date-format ' Yyyymmdd_hh_mm_ss ' $SMTP = Get-smtp-path $path get-date|& $log #max 10foreach ($f in $ (ls-path "$path \outbox\*.csv" |sort-object-propert Y lastwritetime-descending| Select-object-first) {& $rem |& $log $curentfile = "$ ($f. FullName)" $curentFile |& $log #one CSV FILE PRESENT On MAIL MESSAGE. $email = Convertfrom-mailcsv-path $curentFile "$ ($email. Subject) is sending.." #SEND it$smtp.send ($email) "$ ($email. Subject) is sent" #MOVE IT-to-FOLDER sent when Successmove-item-path $cureNtfile-destination "$path \sent\$ ($f. Name)" #Tabke a Breaksleep 1}}}catch {#log error message$_|& $log "error occur whe N Sending mail: $curentFile "|& $log #move IT to FOLDER error when Failmove-item-path $curentFile-destination" $path \err or\$ ($f. Name) "}finally {#mark with dateget-date|& $log #popcd $pop}
Mailmodule.psm1
function import-zip{param ([string] $zipfilename, [string] $destination) if (Test-path ($zipfilename)) {$ Shellapplication = new-object-com Shell.application$zippackage = $shellApplication. NameSpace ($zipfilename) $ Destinationfolder = $shellApplication. NameSpace ($destination) $destinationFolder. CopyHere ($zipPackage. Items ())}} function export-zip{param ([string] $sourcefiles, [string] $zipfilename) dir $sourcefiles | Foreach-object {if (!$ Zipfilename) {$zipfile = $_. FullName + ". zip";} else {$zipfile = $zipfilename;} if (! ( Test-path ($zipfile))) {Set-content $zipfile ("PK" + [char]5 + [Char]6 + ("$ ([char]0)]);(d ir $zipfile). IsReadOnly = $false;} $shellApplication = new-object-com shell.application; $zipPackage = $shellApplication. NameSpace ($zipfile); $ Zippackage.copyhere ($_. FullName));}} function convertfrom-mailcsv{[Cmdletbinding ()]param ([System.String] $Path) $Path = "$Path" if (Test-path-path $Path) {$ TMP = Import-csv-path $Path $mail = New-object System.net.mail.mailmessage#set the From addresses$MailAddress = $tmp. From$mail. from = New-object System.Net.Mail.MailAddress ($MailAddress) #set the destination addresses$mailtoaddress = $tmp. To$mail. To.add ($MailtoAddress) #set the CC addresses foreach ($cca in $tmp. Cc-split ', ') {if ($cca-ne ""-and $cca-ne $null) {$copy =new-object System.Net.Mail.MailAddress ("$cca"); $mail. Cc. ADD ($copy); }} #set the Content$mail. Subject = $tmp. Subject$mail. Priority = $tmp. Priority$mail. Body = $tmp. Body#set the Attachmentsforeach ($filename in $tmp. Attachments-split ', ') {if (Test-path-path $filename) {$attachment = New-object System.Net.Mail.Attachment ($filename) $mail. Attachments.Add ($attachment)}} #add to Arrayreturn $mail;}} function convertfrom-mail{[Cmdletbinding ()]param ([System.String] $Path) begin {$pop = $pwd cd $Path}process {[ System.net.mail.mailmessage[]] $mails = @ () foreach ($f in $ (ls-path $Path-name "*.csv")) {$tmp = Import-csv-path $f $mail = New-object System.net.mail.mailmessage#set the From addresses$mailaddress = $tmp. From$mail. From = New-object System.Net.Mail.MailAddress ($MailAddress) #set the destination addresses$mailtoaddress = $tmp. To$mail. To.add ($MailtoAddress) #set the CC addresses foreach ($cca in $tmp. Cc-split ', ') {if ($cca-ne ""-and $cca-ne $null) {$copy =new-object System.Net.Mail.MailAddress ("$cca"); $mail. Cc. ADD ($copy); }} #set the Content$mail. Subject = $tmp. Subject$mail. Priority = $tmp. Priority$mail. Body = $tmp. Body#set the Attachmentsforeach ($filename in $tmp. Attachments-split ', ') {if (Test-path-path $filename) {$attachment = New-object System.Net.Mail.Attachment ($filename) $mail. Attachments.Add ($attachment)}} #add to Array$mails + = $mail;} return $mails;} End {CD $pop}}function get-smtp{param ([System.String] $Path) begin {$pop = $PWDCD $Path}process {$param = import-csv ". \sm Tp.csv "$smtpServer = $param. Server$smtpuser = $param. User$smtppassword = $ ($param. password| convertto-securestring) $smtp = New-object system.net.mail.smtpclient-argumentlist $smtpServer # $smtp. Credentials = New-object System.Net.NetworkCrEdential-argumentlist $smtpUser, "Isoft1410" # $smtpPassword $SMTP. Credentials = New-object system.net.networkcredential-argumentlist $smtpUser, $smtpPassword return $SMTP;} End {CD $pop}}function convertto-mail{[Cmdletbinding ()]param ($mail, [System.String] $Path) $mail | Export-csv-path $Path}export-modulemember-function convertfrom-mail, CONVERTTO-MAIL,GET-SMTP, CONVERTFROM-MAILCSV, Import-zip,export-zip
Download
http://download.csdn.net/detail/wangzhpwang/8268477
http://download.csdn.net/detail/wangzhpwang/8268511
Powershell Mail module, send all messages in Outbox (A. csv file represents an email)