PowershellMailmodule,发送outbox里的所有邮件(一个.csv文件代表一封邮件)(一)

2015-02-03 04:25:19 · 作者: · 浏览: 66

把creating mail代码写到调用处,往outbox写入 mailxxx.csv文件,入面记录了邮件的主要内容

写入

?

	#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-avmail.ps1发送邮件

?

?

."$($dh)\Mail\Send-AvEmail.ps1" 

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|&$log
try{ 
	#Display
	"order`t type"|&$log
	&$rem|&$log
	1..20|%{
		if($hash["$_"] -ne $null){ 
			"$($_)`t $($hash["$_"])"|&$log
		}
	}
	&$rem|&$log
	"echo The Depoly will start immediately" |&$log
	sleep 1|&$log
	&$rem|&$log
	#hit by order
	1..20|%{
		if($hash["$_"] -ne $null){

			if($dh -ne $pwd.path){
				cd $dh
			}
			$key = $hash["$_"] 
			"........................$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 = "Do you want to Send the logfile to isoftstone?" 
	$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
	"Send the 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`