2017-04-26 59 views
1

如何配置此脚本,以便我可以多次重复此多个地址的邮件过程,例如欧洲,亚洲而不复制代码?如何优化此电子邮件列表powershell脚本

例如,我可以做这样的事情?:

IF $keyword "Europe"发现THEN $europe

$europe =请问欧洲的东西

THEN

IF $keyword "Asia"发现THEN $asia

这里是我的代码:

# Email Automation 

    #Defines Directory 
    $dir = "C:\Users\user\Desktop\myfolder" 
    #Sets STMP server 
    $SMTPServer = "10.0.0.1" 
    #Declares todays time and formats 
    $Time = (Get-Date).ToString('MM/dd/yyyy hh:mm tt') 

    # Europe # 
    #Declares the keyword used to find List 
    $keywordEur = "Europe" 

    #Searches dir for list , formats 
    $AttachmentEur = Get-ChildItem -Path $dir -Filter "*$keywordEur*" -Recurse 
    $AttachmentNameEur = $AttachmentEur.BaseName 

    #Defines mailing list 
    $FromEur = "[email protected]" 
    $ToEur = "[email protected]" 
    $CcEur = "[email protected]", "[email protected]" 
    $SubjectEur = "$AttachmentName @ $Time" 
    $BodyEur = "Please find attached the file needed for Europe. 

    Regards, 
    Me 
    " 

    #Actions Email 
    Send-MailMessage -From $FromEur -To $ToEur -CC $CcEur -Subject   $SubjectEur -Body $BodyEur -SmtpServer $SMTPServerEur -Attachments   $AttachmentEur.FullName 

    # Asia # 
    #Declares the keyword used to find List 
    $keywordAs = "Asia" 

    #Searches dir for list , formats 
    $AttachmentAs = Get-ChildItem -Path $dir -Filter "*$keywordAs*" -Recurse 
    $AttachmentNameAs = $AttachmentAs.BaseName 

    #Defines mailing list 
    $FromAs = "[email protected]" 
    $ToAs = "[email protected]" 
    $CcAs = "[email protected]", "[email protected]" 
    $SubjectAs = "$AttachmentNameAs @ $Time" 
    $BodyAs = "Please find attached the file needed for Asia. 

    Regards, 
    Me 
    " 

    #Actions Email 
    Send-MailMessage -From $FromAs -To $ToAs -CC $CcAs -Subject $SubjectAs   -Body $BodyAs -SmtpServer $SMTPServerAs -Attachments $AttachmentAs.FullName 

回答

0

这里是你如何能与ForEach环和两个哈希表建成为区域对象重复一遍:

# Email Automation 

#Defines Directory 
$dir = "C:\Users\user\Desktop\myfolder" 
#Sets STMP server 
$SMTPServer = "10.0.0.1" 
#Declares todays time and formats 
$Time = (Get-Date).ToString('MM/dd/yyyy hh:mm tt') 

$Europe = @{    
    Name = 'Asia' 
    From = "[email protected]" 
    To = "[email protected]" 
    Cc = "[email protected]", "[email protected]" 
} 

$Asia = @{ 
    Name = 'Asia'   
    From = "[email protected]" 
    To = "[email protected]" 
    Cc = "[email protected]", "[email protected]" 
} 

$Regions = @() 
$Regions += New-Object PSObject -Property $Asia 
$Regions += New-Object PSObject -Property $Europe 

ForEach ($Region in $Regions) { 

    #Searches dir for list , formats 
    $Attachment = Get-ChildItem -Path $dir -Filter "*$($Region.name)*" -Recurse 
    $AttachmentName = $Attachment.BaseName 

    $Subject = "$AttachmentName @ $Time" 
    $Body = "Please find attached the file needed for $($Region.name). 

    Regards, 
    Me 
    " 
    #Actions Email 
    Send-MailMessage -From $Region.From -To $Region.To -CC $Region.Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment.FullName 
} 
+0

谢谢您的帮助迄今为止,但我也需要发送电子邮件至基于关键字 – oisinmc

+0

的不同电子邮件地址它是否总是与每个关键字相同的一组地址? –

+0

对于每个关键字,没有其不同的To和Cc地址,这是否意味着我应该使用基于每个关键字的IF语句? – oisinmc

0

是的,有。由于关键字本身是似乎改变,把你的关键字的数组,并包裹在一个循环整个事情的唯一的事:

# Email Automation 

#Define Directory 
$dir = "C:\Users\user\Desktop\myfolder" 
#Set STMP server 
$SMTPServer = "10.0.0.1" 
#Declare todays time and formats 
$Time = (Get-Date).ToString('MM/dd/yyyy hh:mm tt') 

#Define mailing list 
$From = "[email protected]" 
$To = "[email protected]" 
$Cc = "[email protected]", "[email protected]" 

#Declares the keywords used to find attachments 
$keywords = 'Europe','Asia' 

foreach($keyword in $keywords){ 
    #Searches dir for list , formats 
    $Attachment = Get-ChildItem -Path $dir -Filter "*$keyword*" -Recurse 
    $AttachmentName = $Attachment.BaseName 

    $Subject = "$AttachmentName @ $Time" 
    $Body = "Please find attached the file needed for $keyword. 

    Regards, 
    Me 
    " 

    #Actions Email 
    Send-MailMessage -From $From -To $To -CC $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment.FullName 
} 
+0

感谢您的帮助到目前为止,但我也需要发送电子邮件到不同的电子邮件地址根据关键字 – oisinmc