问题描述
我有一个* .csv文件,其中有几列,我想使用UsersTo和UsersCc用户列表发送消息。 问题在于$ CcAddress,因为一旦格式化它们,我就有多个电子邮件地址,我得到以下信息: 错误: Send-MailMessage:在邮件标题“'”中发现一个无效字符。 在C:\ discovery Scan Process \ RSU通知\ Notifications.ps1:43 char:2
- 发送MailMessage-编码UTF32-至$ ToAddress -Cc $ CcAddress -from $ FromAddre ...
-
+ CategoryInfo : InvalidType: (:) [Send-MailMessage],FormatException + FullyQualifiedErrorId : FormatException,Microsoft.PowerShell.Commands.SendMailMessage
CSV看起来像这样:
UsersTo,UsersCc,Domain
unkNow[email protected],"[email protected],[email protected]",test.test
unkNow[email protected],[email protected]",new.test
unkNow[email protected],[email protected]",prod.test
unkNow[email protected],[email protected]",uat.test
代码:
$notificaitonList = import-csv .\'listofUsers - copy.csv'
$domains = 'test.test','prod.test'
foreach ($domain in $domains){
$FromAddress = "some@address"
$ToAddress = ($notificaitonList | Where-Object {$_.domain -like $domain}).UsersTo | foreach {"`"$_`""}
$Ccstr = (($notificaitonList | Where-Object {$_.domain -like "$domain"}).UsersCc).split(",")
$Ccstr = $Ccstr | foreach {"`"$_`""}
[string[]]$CcAddress= $Ccstr.Split(",") | foreach {"`"$_`""}
[string] $MessageSubject = "MessageSubject "
[string] $emailbody = "emailbody"
$SendingServer = "server"
Send-MailMessage -Encoding UTF32 -to $ToAddress -Cc $CcAddress -from $FromAddress -subject $MessageSubject -smtpServer $SendingServer -body $emailbody
}
解决方法
您可以简单地在逗号处分割,然后让powershell处理其余部分。这是我的测试,证实了这一点。
$tempfile = New-TemporaryFile
@'
UsersTo,UsersCc,Domain
[email protected],"[email protected],[email protected]",test.test
'@ | Out-File $tempfile -Encoding utf8
Import-CSV $tempfile | foreach {
$mailparams = @{
SMTPServer = 'myexchangeserver'
From = "[email protected]"
To = $_.usersto
CC = $_.userscc -split ','
Subject = 'test email'
Body = 'test body'
}
Send-MailMessage @mailparams
}