将大小值转换为TeraBytes(TB)

时间:2014-08-08 11:36:02

标签: powershell powershell-v3.0

您好有人可以查看我的代码并建议如何将以下行转换为TeraBytes。

$totalmbdata = ($allstats | Select-Object 'Archive.Mailbox.Total.GB' | Measure-Object 'Archive.Mailbox.Total.GB' -Sum).Sum

最诚挚的问候 Mike.W

#=====================================
#Get system date and store to variable
#=====================================
$Date = Get-Date -format D 

#====================
#Setting up variables
#====================
$allstats = @()
$archivedbstats = @()

#==========================================================
#Set the archive database statistcs variables and grab data
#==========================================================
$arcservers = Get-MailboxServer | Where-Object {$_.Name -like 'Server*' -or $_.Name -like 'Server*'}
$databases = Get-MailboxDatabase UK-ARC-0*

ForEach ($db in $databases){
    $numarcs= (Get-MailboxStatistics -Database $db).count
    $dbstats = Get-MailboxDatabase -Identity $db -status | Select-Object Name,@{l="dbfilesize";e={"$([math]::round($_.DatabaseSize.Tobytes() /1Gb, 0))"}}`
    ,@{l="availablespace";E={"$([math]::Round($_.AvailableNewMailboxSpace.ToBytes() /1GB,2))"}}
    $archMBsizes = Get-MailboxStatistics -database $db | Select @{l="Size";e={$([math]::Round($_.TotalItemSize.Value.ToBytes() /1GB,0))}} | Measure-Object Size -Sum
    [int]$totalbocks = $archMBsizes.Sum /2
    #$archMBsizesTB = Get-MailboxStatistics -database $db | Select @{l="SizeTB";e={$([math]::Round($_.TotalItemSize.Value.ToBytes() /1TB,1))}}

#==========================================================================================
#Greate new Object and populate information from variables, add table column names and data
#==========================================================================================
    $archivedbstats = New-Object PSObject
            $archivedbstats | Add-Member -MemberType 'NoteProperty' -Name 'Database.Name' -Value $db.Name
            $archivedbstats | Add-Member -MemberType 'NoteProperty' -Name 'Database.Size.GB' -Value $dbstats.dbfilesize
            $archivedbstats | Add-Member -MemberType 'NoteProperty' -Name 'Archive.Mailbox.Total.GB' -Value $archMBsizes.Sum
            $archivedbstats | Add-Member -MemberType 'NoteProperty' -Name 'Archive.Count' -Value $archMBsizes.Count
            $archivedbstats | Add-Member -MemberType 'NoteProperty' -Name 'Usable.Database.Space.GB' -Value $dbstats.availablespace
            $archivedbstats | Add-Member -MemberType 'NoteProperty' -Name 'DB.in.Blocks' -Value $totalbocks
    $allstats += $archivedbstats
    }

#====================================================
#Sum the total archive data size and save to variable
#====================================================
$totalmbdata = ($allstats | Select-Object 'Archive.Mailbox.Total.GB' | Measure-Object 'Archive.Mailbox.Total.GB' -Sum).Sum
#===============================================================================================================
#Colourize the table cells by passing the HTML file through the function located in the Set-CellColor.ps1 script 
#===============================================================================================================

$dbstatshtml = $allstats | ConvertTo-Html -Fragment | Set-CellColor Database.Size.GB -Color red -Filter "Database.Size.GB -eq 550 -or Database.Size.GB -gt 550" -Row
$dbstatshtml = $dbstatshtml | Set-CellColor Database.Size.GB -Color yellow -Filter "Database.Size.GB -eq 400 -or Database.Size.GB -lt 550" -Row
$dbstatshtml = $dbstatshtml | Set-CellColor Database.Size.GB -Color green -Filter "Database.Size.GB -lt 400" -Row

#=============================================
#Formatting HTML page imcluding table and text
#=============================================
$htmlbody = @"
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<style>
TABLE   {
    border: 0px solid black;
    border-collapse: collapse;
    font-family: helvetica;
    font-size: 10.5pt;
    }
TH  {
    border: 0px solid black;
    background: #dddddd; padding: 5px;
    }
TD  {
    border: 0px solid black;
    padding: 5px;
    }
 /* List Definitions */
 ol
    {margin-bottom:0cm;}
ul
    {margin-bottom:0cm;}
-->
</style>
</head>
<body>

<!-- This is the Company Header image -->
<p class=MsoNormal style='margin-left:4.5pt'>
    <a href="https://www.domain.com/">
    <span style='color:windowtext'>
        <img border=0px width=640 height=69 id="Picture 1" src="cid:HeaderImage" alt="header_white">
    </span>
    </a>
</br>
<p><b>Total Email data is $totalmbdata GB on the $Date</br>
</br>
<p><b>Legend</br>
<SPAN style="BACKGROUND-COLOR: #FF0000">Do Not create Archive Mailboxes on these databases.</SPAN></br>
<SPAN style="BACKGROUND-COLOR: #FFFF00">Create Achive Mailboxes for users with a total pst size of 3GB or less.</SPAN></br>
<SPAN style="BACKGROUND-COLOR: #008000">Create Archive Mailboxes for users with a total pst size greater than 3GB and up to 5GB.</SPAN></br>
For PST totals over 5GB please contact the IT Team</br>
</br>
</p>
$dbstatshtml

</body>
</html>
"@

#===============================================
#Configure email settings build and send message
#===============================================
    $smtpServer = "smtpfor.domain.local.com"
    $recipients = "user@domain.local.com, suser@domain.local.com"
    $msg = New-Object Net.Mail.MailMessage
    $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
    $msg.From = "user@domain.local.com"
    $msg.IsBodyHTML = $true
    $msg.Subject = $Subject
    $msg.Body = $htmlbody
    $msg.to.add($recipients)

    $HeaderImage = "C:\Scripts\Images\Header.png"
            $att1 = New-Object Net.Mail.Attachment($HeaderImage)
            $att1.ContentType.MediaType = "image/png"
            $att1.ContentId = "HeaderImage"
            $msg.Attachments.Add($att1)


$smtp.Send($msg)

#====================End=========================

1 个答案:

答案 0 :(得分:0)

由于您已经在以GB收集它(提示:不要转换为GB,TB等,直到您要向用户显示的位置),将GB的数量除以1024以获得TB的数量。

$totalmbdata = (($allstats | Select-Object 'Archive.Mailbox.Total.GB' | Measure-Object 'Archive.Mailbox.Total.GB' -Sum).Sum) / 1024;

但是,总结脚本中较早的原始字节数(不要转换为GB)会更好更多,然后只需要除1TB到了这一点。每次在流中进行此转换时,都会失去一些保真度。

此外(这是未经请求的建议),在您使用Net.Mail.SmtpClient的最后 - 请改为使用Send-Mailmessage。你有PowerShell v3,它从v2开始就可以使用。