允许参数接受 Null 或空字符串 PowerShell
Allow param to accept a Null or Empty String PowerShell
在这个方法中,我添加了一个参数 $blindcopy ,我希望在调用时能够密件抄送用户。在不添加该参数的情况下测试此脚本后,一切正常。添加此添加后,我收到一条错误消息 "Cannot validate the argument on parameter 'bcc'. The argument is null or empty" 我已尝试将 AllowEmptyString() 属性添加到参数但仍然没有成功。非常感谢任何帮助!
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 = "user@foo.com"
$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 "foo@bar.org" -To "bar@foo.org" -Subject $SubjectString -Bcc $BlindCopy -Body $BodyString -BodyAsHtml -Priority High -dno onSuccess, onFailure -SmtpServer $SMTPServer -Credential $cred
}
else
{
Send-MailMessage -From "foo@bar.org" -To "bar@foo.org" -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 "foo@bar.org" -FileAttachment $null -BlindCopy $BccNull
即使您函数的 -BlindCopy
参数接受空字符串,Send-MailMessage
的 -Bcc
参数仍然不接受。
我认为对您来说最好的做法是构建参数的哈希表,如果它们不为空则添加可选参数,然后 splat 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 = "user@foo.com"
$pass = ConvertTo-SecureString -String "bar" -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential $user, $pass
$params = @{
'From' = 'foo@bar.org'
'To' = 'bar@foo.org'
'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 {
...
}
}
但即使使用 splatting,我可能仍然不允许参数 -BlindCopy
为空字符串。如果不应将消息密件抄送给某个人,则应完全省略该参数。附件也是如此。如果空字符串显示为 BCC 收件人(或附件),函数 应该 抛出错误。恕我直言。 YMMV.
在这个方法中,我添加了一个参数 $blindcopy ,我希望在调用时能够密件抄送用户。在不添加该参数的情况下测试此脚本后,一切正常。添加此添加后,我收到一条错误消息 "Cannot validate the argument on parameter 'bcc'. The argument is null or empty" 我已尝试将 AllowEmptyString() 属性添加到参数但仍然没有成功。非常感谢任何帮助!
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 = "user@foo.com"
$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 "foo@bar.org" -To "bar@foo.org" -Subject $SubjectString -Bcc $BlindCopy -Body $BodyString -BodyAsHtml -Priority High -dno onSuccess, onFailure -SmtpServer $SMTPServer -Credential $cred
}
else
{
Send-MailMessage -From "foo@bar.org" -To "bar@foo.org" -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 "foo@bar.org" -FileAttachment $null -BlindCopy $BccNull
即使您函数的 -BlindCopy
参数接受空字符串,Send-MailMessage
的 -Bcc
参数仍然不接受。
我认为对您来说最好的做法是构建参数的哈希表,如果它们不为空则添加可选参数,然后 splat 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 = "user@foo.com"
$pass = ConvertTo-SecureString -String "bar" -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential $user, $pass
$params = @{
'From' = 'foo@bar.org'
'To' = 'bar@foo.org'
'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 {
...
}
}
但即使使用 splatting,我可能仍然不允许参数 -BlindCopy
为空字符串。如果不应将消息密件抄送给某个人,则应完全省略该参数。附件也是如此。如果空字符串显示为 BCC 收件人(或附件),函数 应该 抛出错误。恕我直言。 YMMV.