-1

我需要检查磁盘并发送一封包含状态的电子邮件。

但我得到的只是一个 Line 而不是'r'n. 怎么了?

$smtpserver = "server"
$from = "from"
$to = "to"
$subject = "subject"

$servernames = Get-Content "C:\computer.txt"

$Diskreport = $Servernames | % {
    Get-WmiObject Win32-LogicalDisk -Computername $_ -Filter "Drivetype=3" -ErrorAction SilentContinue |
        ? { ($_.freespace/$size) -le '0.7' }
}

[String]$body = $null
$DiskReport | % {
    $body += (Servername: "+$_.SystemName) + "'r'n"
    $body += (Drive Letter: "+$_.DeviceID.ToString()) + "'r'n"
    $body += (TotalCapacity (GB): "+((($_.size /1024) /1000) /1000).ToString()) + "'r'n"
    $body += (TotalCapacity (GB): "+((($_.Freespace /1024) /1000) /1000).ToString()) + "'r'n"
    $body += (TotalCapacity (GB): "+($Freespace /$_.size).ToString()) 
}

Send-MailMessage -Subject $Subject -Body $Body -From $from -To $to -SmtpServer $smtpserver -Port 587
4

1 回答 1

1

该字符串"'r'n"只有四个普通字符 - 一个单引号、一个r、一个单引号和一个n.

如果您想在字符串中使用回车和换行符,请使用反引号( `) 转义rand n如注释中所指出的

$body += "(Servername: " + $_.SystemName + ")`r`n" # and so on...

另一种方法是仅使用-join运算符通过以下方式连接所有行[Environment]::NewLine

$bodyLines = @(
    "(Servername: $($_.SystemName))"
    "(Drive Letter: $($_.DeviceID.ToString()))"
    # etc...
)
$body += $bodyLines -join [Environment]::NewLine
于 2019-01-15T09:36:56.190 回答