2017-01-12 265 views
1

このメソッドでは、呼び出されたときにbccユーザーにできるようにする$ blindcopyパラメータを追加しました。このパラメータを追加せずにこのスクリプトをテストした後、すべて正常です。この追加を追加した後、 "パラメータ 'bcc'の引数を検証できません。引数がnullまたは空です。" AllowEmptyString()属性をパラメータに追加しようとしましたが、まだ運がありません。どんな助けでも大歓迎です!あなたの関数の-BlindCopyパラメータが空の文字列を受け入れた場合でもparamにnullまたは空の文字列を受け入れるPowerShell

cls 

$BccNull = "" 

function SendEmail([string]$BodyString,[string]$SubjectString,[string[]]$EmailRecipientsArray,[string]$FileAttachment=$null,[AllowEmptyString()][string]$BlindCopy) 
{ 
    $MethodName = "Send Email" 

    # Send the HTML Based Email using the information from the tables I have gathered information in 
    try 
    {  
     $user = "[email protected]" 
     $pass = ConvertTo-SecureString -String "bar" -AsPlainText -Force 
     $cred = New-Object System.Management.Automation.PSCredential $user, $pass 

     $SMTPServer = "some.mail.server" 
     if([string]::IsNullOrEmpty($BodyString)) 
     { 
      $BodyString = " Body text was empty for user: $ErrorMessageUserName" 
     } 


     if([string]::IsNullOrEmpty($FileAttachment)) 
     { 
      Send-MailMessage -From "[email protected]" -To "[email protected]" -Subject $SubjectString -Bcc $BlindCopy -Body $BodyString -BodyAsHtml -Priority High -dno onSuccess, onFailure -SmtpServer $SMTPServer -Credential $cred 
     } 
     else 
     { 
      Send-MailMessage -From "[email protected]" -To "[email protected]" -Subject $SubjectString -Body $BodyString -BodyAsHtml -Attachments $FileAttachment -Priority High -dno onSuccess, onFailure -SmtpServer $SMTPServer -Credential $cred 
     } 
    } 
    catch 
    { 
     Write-Host "An Exception has occurred:" -ForegroundColor Red 
     Write-Host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red 
     Write-Host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red 

     #$ErrorMessage = "Script Error: "+ $_.Exception.Message + "`n" + "Exception Type: $($_.Exception.GetType().FullName)" 
     #$SubjectLine = "Script Error: " + $MethodName + " " + $ErrorMessageUserName 

     #SendEmail -BodyString $ErrorMessage -SubjectString $SubjectLine -EmailRecipientsArray $EmailErrorAddress -FileAttachment $null 

     $SuccessfulRun = $false 
     #ReturnStatusError -CurrentStatus "Error" -ErrorMessage $_.Exception.Message 
    } 
} 

SendEmail -BodyString "Test" -SubjectString "Test" -EmailRecipientArray "[email protected]" -FileAttachment $null -BlindCopy $BccNull 

答えて

2

Send-MailMessage-Bccパラメータはまだいません。

パラメータのハッシュテーブルを作成し、空でない場合はオプションのパラメータを追加し、コマンドレットにハッシュテーブルsplatを追加することをお勧めします。

function Send-Email { 
    Param(
     [Parameter(Mandatory=$true)] 
     [string]$BodyString, 

     [Parameter(Mandatory=$true)] 
     [string]$SubjectString, 

     [Parameter(Mandatory=$true)] 
     [string[]]$EmailRecipientsArray, 

     [Parameter(Mandatory=$false)] 
     [string]$FileAttachment = '', 

     [Parameter(Mandatory=$false)] 
     [AllowEmptyString()] 
     [string]$BlindCopy = '' 
    ) 

    try { 
     $user = "[email protected]" 
     $pass = ConvertTo-SecureString -String "bar" -AsPlainText -Force 
     $cred = New-Object Management.Automation.PSCredential $user, $pass 

     $params = @{ 
      'From'  = '[email protected]' 
      'To'   = '[email protected]' 
      'Subject' = $SubjectString 
      'Body'  = $BodyString 
      'Priority' = 'High' 
      'dno'  = 'onSuccess', 'onFailure' 
      'SmtpServer' = 'some.mail.server' 
      'Credential' = $cred 
     } 

     if ($BlindCopy)  { $params['Bcc'] = $BlindCopy } 
     if($FileAttachment) { $params['Attachments'] = $FileAttachment } 

     Send-MailMessage @params -BodyAsHtml 
    } catch { 
     ... 
    } 
} 

しかし、スプラットを使用しても、おそらくパラメータ-BlindCopyの空の文字列は許可されません。メッセージが誰かにBCCされていないと思われる場合、そのパラメータは完全に省略されるべきです。同じことが添付ファイルにも適用されます。空文字列がBCC受信者(または添付ファイル)として表示される場合、関数にエラーが発生するはずです。私見では。 YMMV。

+0

ありがとうございました! – johnnyjohnson