2017-01-12 1 views
1

Dans cette méthode, j'ai ajouté un paramètre $ blindcopy que je veux être en mesure de permettre aux utilisateurs bcc lorsqu'ils sont appelés. Après avoir testé ce script sans ce paramètre ajouté, tout va bien. Après avoir ajouté cet ajout, je reçois une erreur disant "Impossible de valider l'argument sur le paramètre 'bcc' L'argument est nul ou vide" J'ai essayé d'ajouter l'attribut AllowEmptyString() au paramètre mais toujours pas de chance. Toute aide est très appréciée!Autoriser param à accepter une chaîne vide ou vide 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 

Répondre

2

Même si le paramètre -BlindCopy de votre fonction accepte une chaîne vide, le paramètre -Bcc de Send-MailMessage ne fonctionne toujours pas.

Je dirais que le meilleur plan d'action pour vous serait de construire une table de hachage de vos paramètres, ajouter des paramètres facultatifs s'ils ne sont pas vides, puis splat la hashtable sur la cmdlet.

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 { 
     ... 
    } 
} 

Mais même avec splatting je ne serais probablement pas encore permettre à des chaînes vides pour le paramètre -BlindCopy. Si le message n'est pas censé être transmis à quelqu'un, ce paramètre doit être entièrement omis. La même chose vaut pour les pièces jointes. Si une chaîne vide apparaît en tant que destinataire BCC (ou pièce jointe), la fonction doit générer une erreur. A MON HUMBLE AVIS. YMMV.

+0

Merci pour votre aide! – johnnyjohnson