powershell - 带有空白行的PowerShell “cat <file>”将空格视为空格

标签 powershell sendmail

我已经看到很多帖子询问如何从Powershell中的文件中删除空白行,但是我试图保留它们,而不在send-mailmessage调用中采用bodyashtml。

我有一个文件mail.txt,它是:

mail file blah blah blah

hshshshshs

当您整理该文件时,看起来还不错;主要问题是,当您将“cat”视为变量时,例如
$BLAH = $(cat ${MAIL_FILE})
echo "blah is $BLAH"

它返回为
blah is mail file blah blah blah  hshshshshs

因此,当它作为正文的一部分传递到发送邮件消息时:
$BODY = @"

this is line two

$(cat ${MAIL_FILE})
"@

send-mailmessage `
  -from ${FROM} `
  -to ${MAIL_RECIPIENT} `
  -subject ${SUBJECT} `
  -body ${BODY} `
  -smtpserver ${RELAYHOST} `
  -priority ${PRIORITY}

它作为电子邮件出现在电子邮件中
line two

mail file blah blah blah  hshshshshs

代替
<blank line here>
line two

mail file blah blah blah

hshshshshshhs

那么,我该如何按原样“维护”文件,传递给邮件正文而不是作为附件?理想情况下,如何在该主体中也获得领先的空白行。

最佳答案

Get-Content(别名cat)实际上将内容读取为字符串数组,每个字符串代表一行。在powershell 3.0中,您可以使用-Raw标志来获取整个文件。

$BLAH = cat ${MAIL_FILE} -Raw

否则,您可以使用join运算符:
$BLAH = (cat ${MAIL_FILE}) -join "`r`n"

关于powershell - 带有空白行的PowerShell “cat <file>”将空格视为空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18925946/

相关文章:

Powershell:并行运行多个作业并查看来自后台作业的流式传输结果

powershell - PowerShell测试连接,如果使用get-service存在服务

powershell - 如何输出到日志文件

python - 如何使用 sendgrid API 在 django 中使用设计的模板?

批量电子邮件期间的 javamail 与 sendmail 性能

c# - SmtpClient 发送原始 Html

PHP邮件()不工作

powershell - Sitecore Powershell : items with multiple fields with the same name

powershell - 我应该使用什么来代替 Select-AzureSubscription?

shell - 将 sendmail 用于 HTML 正文和二进制附件