我有一个脚本正在检查数据库的镜像状态。 Powershell中的输出很好,但是当我尝试通过邮件发送时,得到的是“ Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData”,而不是数据本身。我尝试将其更改为Out-String,但随后将所有结果都显示在一行中。如何将格式化的输出与直接在PowerShell中格式化的方式格式化?
# rozszerzenie do obslugi
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null;
$mail_from = "xxx";
$mail_to = "xxx";
$mail_subject = "Status mirrorowanych baz";
$mail_encoding = "UTF8";
$mail_smtp = "xxx";
# lista serwerow
$list = @("SERVER01V",
"SERVER02V"
);
$output = foreach($server in $list)
{
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $server;
# pokaz tylko mirrorowane
$databases = $srv.Databases | Where-Object {$_.IsMirroringEnabled -eq $true};
Write-Output "<br>==================================<br>";
Write-Output $server;
Write-Output "<br>==================================<br>";
$databases | Select-Object -Property Name, MirroringStatus | Format-Table -AutoSize | Out-String;
Write-Output "<br>";
}
$mail_body = $output;
Send-MailMessage -To $mail_to -From $mail_from -Subject $mail_subject -SmtpServer $mail_smtp -Encoding $mail_encoding -Body $mail_body -BodyAsHtml
答案 0 :(得分:0)
您当前正在发送HTML邮件。因为这样的换行符没有关系。如果要在邮件中使用换行符,则需要使用文本格式,或者将换行符替换为<br />
或类似名称。手动添加外观上的<br />
以保持一致的样式并进行替换可能是明智的。
答案 1 :(得分:0)
尝试将所有数据收集到一个数组中,然后使用ConvertTo-HTML cmdlet和Send-MailMessage中的“ BodyAsHTML”开关
$DatabaseArray=@()
ForEach ($server in $list) {
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $server
$DatabaseArray += $srv.Databases | Where-Object {$_.IsMirroringEnabled -eq $true} | Select-Object Name,MirroringStatus
}
$HTMLBody = $DatabaseArray | ConvertTo-HTML
Send-MailMessage -subject x -body $HTMLBody -BodyAsHTML