I have written a script that gathers information from public folders and outputs it to a .csv. All is well, except for the fact that the pf.FolderSize attribute is displayed (exported) in both MB and KB with the bytes in parenthesis e.g: 1.925 MB (2,018,497 bytes). I would like to sum the whole thing so it would be nice to format the results in MB only without the parenthesis, so I can easily get the overall size of all folders.
Here is the working
$publicFolders = Get-PublicFolder "\SomePublicFolderName" -Recurse -resultsize unlimited |
Select-Object *
### Array to contain results
$results = @()
### Loop through and do the work
$publicFolders |ForEach-Object {
$pf = $_
if ($pf.MailEnabled){
$pfstat = Get-PublicFolderStatistics -Identity $pf.Identity | Where-Object {$_.User -notmatch "Default|Anonymous|S-X-X-XX"}
$perms = Get-PublicFolderClientPermission -Identity $pf.Identity | Where-Object {$_.User -notmatch "Default|Anonymous|S-X-X-XX"}
Foreach($perm in $perms){
[PSCustomObject]@{
PFName = $pf.Name
PFIdentity = $pf.Identity
MailEnabled = $pf.MailEnabled
FolderSize = $pf.FolderSize
TotalItemSize = $pfstat.TotalItemSize
ItemCount = $pfstat.ItemCount
LastModificationTime = $pfstat.LastModificationTime
UserWithPerms = $perm.User
AccessRights = $perm | Select-Object -ExpandProperty AccessRights
PrimaryAddy = $mepf.PrimarySmtpAddress
ForwardingAddy = $mepf.ForwardingAddress
}
}
}}| Export-Csv $path -NoTypeInformation -append
I have tried the below code, but the output was the same value for everything, so that can't be correct...
$pfstat = Get-PublicFolderStatistics -Identity $pf.Identity | Where-Object {$_.User -notmatch "Default|Anonymous|S-X-X-XX"} | Select-Object FolderPath,ItemCount,LastModificationTime @{
N = "TotalItemSize (MB)";
E = {
"{0:N2}" -f ((($_.TotalItemSize -replace "[0-9\.] [A-Z]* \(([0-9,] ) bytes\)","`$1") -replace ",","") / 1MB)
}
}
As always, Thanks in advance. I am eager to see where I went amiss with it.
CodePudding user response:
There's a difference if you run your code on the server itself or from your pc.
When running on (an older Exchange) server, TotalItemSize
may not be a [string]
, but a [Microsoft.Exchange.Data.Unlimited]
object instead which has a .Value
property of type [ByteQuantifiedSize]
.
To also handle that, use a helper function
function Format-MailboxStatisticsSize($itemSize) {
if ($itemSize -is [string]) {
'{0:F2}' -f (($itemSize -replace '^.*\(([\d,] ) bytes\)', '$1' -replace '\D') / 1MB)
}
else { # Microsoft.Exchange.Data.Unlimited
'{0:F2}' -f ($itemSize.Value.ToBytes() / 1Mb)
}
}
Format-MailboxStatisticsSize $pfstat.TotalItemSize # "1.925 MB (2,018,497 bytes)" --> 1.92