我有一系列名称,我正在尝试使用新的行字符加入.我有以下代码
$body = $invalid_hosts -join "`r`n"
$body = "The following files in $Path were found to be invalid and renamed `n`n" + $body
Run Code Online (Sandbox Code Playgroud)
最后,我通过电子邮件发送内容.
$From = "myaddress@domain.com"
$To = "myaddress@domain.com
$subject = "Invalid language files"
Send-MailMessage -SmtpServer "smtp.domain.com" -From $From -To $To -Subject $subject -Body $body
Run Code Online (Sandbox Code Playgroud)
当我收到消息时,该行The following files in <filepath> were found to be invalid and renamed
具有预期的双倍空间,但$ invalid_hosts的内容全部在一行上.我也尝试过
$body = $invalid_hosts -join "`n"
Run Code Online (Sandbox Code Playgroud)
和
$body = [string]::join("`n", $invalid_hosts)
Run Code Online (Sandbox Code Playgroud)
这两种方式都没有效果.我需要做些什么来完成这项工作?
Sha*_*evy 21
将数组传递给Out-String
cmdlet,将它们从字符串对象的集合转换为单个字符串:
PS> $body = $invalid_hosts -join "`r`n" | Out-String
Run Code Online (Sandbox Code Playgroud)
小智 7
今天不得不解决这个问题;以为我会分享我的答案,因为问题和其他答案帮助我找到了解决方案。代替
$body = $invalid_hosts -join "`r`n"
$body = "The following files in $Path were found to be invalid and renamed `n`n" + $body
Run Code Online (Sandbox Code Playgroud)
用
$MessageStr = "The following files in " + $Path + " were found to be invalid and renamed"
$BodyArray = $MessageStr + $Invalid_hosts
$Body = $BodyArray -join "`r`n"
Run Code Online (Sandbox Code Playgroud)
只需输出Out-String即可(参见/sf/answers/1492561801/)
$result = 'This', 'Is', 'a', 'cat'
$strResult = $result | Out-String
Write-Host $strResult
This
Is
a
cat
Run Code Online (Sandbox Code Playgroud)