我正在遍历计算机列表并检查磁盘空间。我想将此信息格式化为一个数组,我可以将其添加为电子邮件正文。我想将整个输出整理成一封电子邮件,我知道如何发送每封电子邮件的个人电子邮件。
以下是代码:
$ComputerList = IMPORT-CSV listofcomputers.txt
$tableFragment = ForEach ($Computer in $ComputerList){
$Name = $Computer.Name
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName $Name -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
$b = ($disk.Size / 1073741824)
$a = ($disk.FreeSpace / 1073741824)
$a = "{0:N1}" -f $a
$b = "{0:N1}" -f $b
if($disk.FreeSpace -lt 10737418240){Write-Host Disk space on $Name is LOW. Only $a GB remaining. -foregroundcolor "magenta"}
else{Write-Host Free disc space on $Name is $a GB, out of $b GB.}
}
答案 0 :(得分:2)
改变这一位:
if($disk.FreeSpace -lt 10737418240){Write-Host Disk space on $Name is LOW. Only $a GB remaining. -foregroundcolor "magenta"}
else{Write-Host Free disc space on $Name is $a GB, out of $b GB.}
到此:
if($disk.FreeSpace -lt 10737418240){ $body += "Disk space on $Name is LOW. Only $a GB remaining.`r`n"}
else{ $body += "Free disc space on $Name is $a GB, out of $b GB.`r`n"}
您不需要字符串数组,因为Send-MailMessage命令的Body参数需要一个字符串。
如果你想要一些标题信息,那么在你的脚本顶部声明$ body:
$body = "Some header info.`r`n"