我使用以下脚本从所有邮箱中获取有关配额详细信息的信息,但是在report.csv文件中,我没有可用空间详细信息,如您在以下示例中看到的那样:
$results=ForEach($mb in $mailboxes){
$stats=get-mailboxstatistics $mb
$props=@{
alias=$mb.alias
DisplayName=$mb.displayname
#StorageLimitStatus=$stats.StorageLimitStatus
TotalItemSize=$stats.totalitemsize
#DatabaseName=$stats.databasename
ProhibitSendQuota=$mb.ProhibitSendQuota
ProhibitsendReceiveQuota=$mb.ProhibitsendReceiveQuota
IssueWarningQuota=$mb.IssueWarningQuota
}
New-Object PsObject -Property $props
}
$results | Sort-Object TotalItemSize -descending | export-csv c:\script\report.csv -NoTypeInformation -Encoding UTF8
输出:
Alias , Display Name ,TotalItemSize, IssueWarningQuota ,ProhibitsendReceiveQuota , ProhibitSendQuota
User01, User01 , 46.51 GB (49,935,441,080 bytes) , unlimited , unlimited , unlimited
User02, User02 , 4.887 GB (5,247,750,394 bytes) 5.86 GB (6,292,127,744 bytes) , 5.95 GB (6,388,764,672 bytes) , 5.91 GB (6,345,815,040 bytes)
我想要的输出是:
Alias , Display Name ,TotalItemSize, IssueWarningQuota ,ProhibitsendReceiveQuota , ProhibitSendQuota , Free Space
User01, User01 , 46.51 GB (49,935,441,080 bytes) , unlimited , unlimited , unlimited , unlimited
User02, User02 , 4.887 GB (5,247,750,394 bytes) 5.86 GB (6,292,127,744 bytes) , 5.95 GB (6,388,764,672 bytes) , 5.91 GB (6,345,815,040 bytes) , 1.023 GB
可用空间计算公式为:(可用空间= ProhibitSendQuota-TotalItemSize)
如果ProhibitSendQuota没有限制,那么可用空间就没有限制。
如果ProhibitSendQuota是自定义大小,则可用空间为Free Space = ProhibitSendQuota-TotalItemSize。
我还需要获取具有可用空间的用户帐户名。
可以帮忙吗?
答案 0 :(得分:1)
您可以执行以下操作:
$results = ForEach($mb in $mailboxes){
$stats=get-mailboxstatistics $mb
if ($mb.ProhibitSendQuota -eq 'Unlimited') {
$freespace = 'Unlimited'
}
else {
$totalBytes = [double]($stats.totalitemsize -replace '.*?\((.*?) bytes.*','$1')
$prohibitBytes = [double]($mb.ProhibitSendQuota -replace '.*?\((.*?) bytes.*','$1')
$freespace = [Math]::Round(($prohibitBytes - $totalbytes)/1GB,2)
}
$props=@{
alias=$mb.alias
DisplayName=$mb.displayname
#StorageLimitStatus=$stats.StorageLimitStatus
TotalItemSize=$stats.totalitemsize
#DatabaseName=$stats.databasename
ProhibitSendQuota=$mb.ProhibitSendQuota
ProhibitsendReceiveQuota=$mb.ProhibitsendReceiveQuota
IssueWarningQuota=$mb.IssueWarningQuota
FreeSpace=$freespace
}
[pscustomobject]$props
}
$results | Sort-Object TotalItemSize -descending | export-csv c:\script\report.csv -NoTypeInformation -Encoding UTF8
说明:
添加了 if
和else
语句来处理Unlimited
配额与数值的条件。 $totalBytes
和$prohibitBytes
选择## GB (#,###,###,### Bytes)
格式的字节值并将其转换为类型[double]
。一旦这些值是数字值类型,我们就可以执行减法。 /1GB
将结果转换为GB。由于Round()
参数值,,2
方法四舍五入到最接近的百分之一。
可能值得研究使用ByteQuantifiedSize结构的更优雅的解决方案。我只是发现这种方法更容易实现。