[英]PowerShell Send-MailMessage format column in message body
警告 - 我是 PowerShell 的新手。 我想用這個腳本實現兩個結果。 第一種是將輸出包含在電子郵件中,並格式化郵件正文中的列,使它們與類似於 Out-Host 的標題對齊。 其次是當 out-csv、out-gridview 或 export-excel 時,我如何對列進行排序?
$VolArray = @();
$Volumes = Get-Ncvol | Where-Object {$_.VolumeMirrorAttributes.IsDataProtectionMirror -match 'False' -and $_.VolumeStateAttributes.IsVserverRoot -match 'False' -and -not $_.VolumeCloneAttributes.VolumeCloneParentAttributes}
ForEach ($Volume in $Volumes){
#get properties
$vol = Get-Ncvol $Volume
#create object with values
$volobj = New-Object -TypeName PSObject -Property @{
'Controller' = $vol.NcController
'Vserver' = $vol.Vserver
'Aggregate' = $vol.VolumeIdAttributes.ContainingAggregateName
'Name' = $vol.VolumeIdAttributes.Name
'Type' = $vol.VolumeIdAttributes.Type
'TotSizeGB'= $vol.VolumeSpaceAttributes.Size/1gb
'Used' = $vol.VolumeSpaceAttributes.SizeUsed/1gb
'%Used' = $vol.VolumeSpaceAttributes.PercentageSizeUsed
'AvailableGB' = $vol.VolumeSpaceAttributes.SizeAvailable/1gb
'SSResSizeGB' = $vol.VolumeSpaceAttributes.SnapshotReserveSize/1GB
'IsDPMirror' = $vol.VolumeMirrorAttributes.IsDataProtectionMirror
'IsReplicaVol' = $vol.VolumeMirrorAttributes.IsReplicaVolume
'IsDPSource' = $vol.VolumeMirrorAttributes.IsSnapmirrorSource
'DPInProgress' = $vol.VolumeMirrorAttributes.MirrorTransferInProgress
'SSPolicy' = $vol.VolumeSnapshotAttributes.SnapshotPolicy
'AutoSSEnabled' = $vol.VolumeSnapshotAttributes.AutoSnapshotsEnabled
'SSCount' = $vol.VolumeSnapshotAttributes.SnapshotCount
'%SSReserve' = $vol.VolumeSpaceAttributes.PercentageSnapshotReserve
'%SSResUsed' = $vol.VolumeSpaceAttributes.PercentageSnapshotReserveUsed
'SSSpaceUsed' = $vol.VolumeSpaceAttributes.SizeUsedBySnapshots/1GB;
}
#add to array outside opf for-loop
$VolArray += $volobj
}
#$VolArray | Export-Csv -Path c:\temp\file.csv
#$VolArray | Export-Excel -Path c:\temp\exceldump.xlsx
$VolArray | Out-String
#Send-MailMessage -To $mailto -Subject $subject -Body (-join $message) -Port $port -SmtpServer $smtp -from $emailfrom
Send-MailMessage -To $mailto -Subject $subject -Port $port -SmtpServer $smtp -from $emailfrom -Attachments c:\temp\file.csv
在 PowerShell 中,出於性能原因,無法保證常見哈希表的屬性順序。 幸運的是,從版本 3 開始,您可以使用[ordered]
關鍵字來創建有序字典(哈希表是字典的一種形式)。
[PSCustomObject][ordered]@{
FirstColumn = 1
SecondColumn = 2
ThirdColumn = 3
}
這將確保后續操作(如 Export-Csv)中的屬性順序。 請注意,我還使用了[PSCustomObject]
加速器,它比New-Object -TypeName PSObject
更高效。
在您的代碼中,在 foreach 循環中有不必要的Get-Ncvol
調用。 您已經獲得了您之前需要的數據:
$Volumes = Get-Ncvol |
Where-Object {
$_.VolumeMirrorAttributes.IsDataProtectionMirror -match 'False' -and
$_.VolumeStateAttributes.IsVserverRoot -match 'False' -and
-not $_.VolumeCloneAttributes.VolumeCloneParentAttributes
}
# Store results in a variable to use later
$reportData = foreach ($Volume in $Volumes) {
# Create object with values
[PSCustomObject][ordered]@{
'Controller' = $Volume.NcController
'Vserver' = $Volume.Vserver
'Aggregate' = $Volume.VolumeIdAttributes.ContainingAggregateName
'Name' = $Volume.VolumeIdAttributes.Name
'Type' = $Volume.VolumeIdAttributes.Type
'TotSizeGB' = $Volume.VolumeSpaceAttributes.Size / 1gb
'Used' = $Volume.VolumeSpaceAttributes.SizeUsed / 1gb
'%Used' = $Volume.VolumeSpaceAttributes.PercentageSizeUsed
'AvailableGB' = $Volume.VolumeSpaceAttributes.SizeAvailable / 1gb
'SSResSizeGB' = $Volume.VolumeSpaceAttributes.SnapshotReserveSize / 1GB
'IsDPMirror' = $Volume.VolumeMirrorAttributes.IsDataProtectionMirror
'IsReplicaVol' = $Volume.VolumeMirrorAttributes.IsReplicaVolume
'IsDPSource' = $Volume.VolumeMirrorAttributes.IsSnapmirrorSource
'DPInProgress' = $Volume.VolumeMirrorAttributes.MirrorTransferInProgress
'SSPolicy' = $Volume.VolumeSnapshotAttributes.SnapshotPolicy
'AutoSSEnabled' = $Volume.VolumeSnapshotAttributes.AutoSnapshotsEnabled
'SSCount' = $Volume.VolumeSnapshotAttributes.SnapshotCount
'%SSReserve' = $Volume.VolumeSpaceAttributes.PercentageSnapshotReserve
'%SSResUsed' = $Volume.VolumeSpaceAttributes.PercentageSnapshotReserveUsed
'SSSpaceUsed' = $Volume.VolumeSpaceAttributes.SizeUsedBySnapshots / 1GB;
}
}
由於我們已經處理了列排序,因此您只需要使用$reportData | Export-Csv c:\\temp\\file.csv -NoTypeInformation
$reportData | Export-Csv c:\\temp\\file.csv -NoTypeInformation
或Export-Excel
等效項。
發送電子郵件將變得更加困難。 最好的辦法是將數據轉換為 HTML 表格並將其包含在電子郵件正文中。
# The CSS is neccesary to make the table look nicer, adjust as needed
$css = @'
<style>
body {background-color: powderblue;}
h1 {color: red;}
p {color: blue;}
th, td {
padding: 15px;
text-align: left;
}
</style>
'@
$emailBody = $reportData | ConvertTo-Html -Head $css
# Use parameter splatting for redability
$emailParameters = @{
To = "jdoe@company.com"
Subject = "NetApp report for $(Get-Date -Format 'd')"
Body = $emailBody
BodyAsHtml = $true
SmtpServer = "smtp.company.com"
Credential = Get-Credential
}
Send-MailMessage @emailParameters
[已訂購] 完美運行。
我修改了郵件正文參數:Body = ($emailBody | Out-String)
因為這個錯誤:
Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
您對設置小數位數有什么建議嗎? ...124.994548797607421875
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.