我想删除输出的最后一个字符
$random = Get-Random -Minimum 1 -Maximum 9
[string]$mails = ""
$recipient = Get-Content F:\maillist.txt | Get-Random -Count $random
$mails = $recipient + $mails
return $mails
这给出了:
name2@domain.com,
name6@domain.com,
name8@domain.com,
这很好,但我需要摆脱最后的","因为那是我想用来通过Send-MailMessage
发送邮件的变量
我尝试了一些事情:
$recipient = Get-Content F:\mailliste.txt | Get-Random -Count $random
$recipient_conv = $recipient.Remove($recipient.Length-1)
完全切断:nam @do ...
$recipient_conv = $recipient.TrimEnd(",")
这有效,但没有","完全像,name1@domain.com name3@domain
但我需要","告诉Send-Mailmessage
还有一个以上的收件人。
与.TrimEnd()
相同的结果是:
$recipient_conv = $recipient -replace ".$"
邮件列表如下所示:
name1@domain.com,
name2@domain.com,
name3@domain.com,
...
name9@domain.com,
编辑 编辑 编辑 编辑
@Manu @AnsgarWiechers
代码看起来像这样
$random = Get-Random -Minimum 1 -Maximum 9
[string]$mails = ""
$recipient = Get-Content F:\mailliste.txt | Get-Random -Count $random
$recipient_conv = $recipient.TrimEnd(",") -join ', '
$mails = $recipient_conv + $mails
Send-MailMessage -To $mails -From sender@domain.com -Subject "This is a subject" -Body "This is the bodytext"
这将发送邮件,但只发送一个而不是所有随机抓取的mailadresses。如果我得到mysef的输出,如Write-Host "send to: $mails"
它会说:send to: name3@domain.com, name6@domain.com, name1@domain.com
所以一切似乎都很好,所以问题是什么