邮件正文中的PowerShell Send-MailMessage格式列

时间:2019-11-28 19:02:44

标签: powershell email format

警告-我是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 

邮件正文: Message Body

2 个答案:

答案 0 :(得分:1)

列排序

在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或等效的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

答案 1 :(得分:0)

[订购]效果很好。

我修改了消息正文参数:  正文=($ emailBody |字符串)

由于此错误:

Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.

您对设置小数位数有什么建议吗? ... 124.994548797607421875