PowerShell to calclualte the size of files and total file size in a directory not working ?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

PowerShell to calclualte the size of files and total file size in a directory not working ?

Post by ITEngineer »

Hi People,

I need someone to fix the below script which calculates the Mailbox.EDB file size in GB as well as Log files size which doesn't seem to be working?

Code: Select all

Get-MailboxDatabase | 
    Select Server, 
		Name, 
        @{Name="Number of Mbx";expression={(Get-Mailbox -Database $_.Identity | Measure-Object).Count}},
		@{Name="DB Size (GB)";Expression={$databasepath = "\\" + $_.Server + "\" + $_.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $_.EdbFilePath.PathName.Remove(0,2); $databasesize = ((Get-ChildItem $databasepath).length)/1048576KB; [math]::round($databasesize, 2)}}, 
		EdbFilePath,
		LogFolderPath,
		@{Name="Log Folder Size (GB)";Expression={$objitem = (Get-MailboxDatabase $_.Identity); $logpath = "`\`\" + $objitem.server + "`\" + $objItem.LogFolderPath.DriveName.Remove(1).ToString() + "$"+ $objItem.LogFolderPath.PathName.Remove(0,2) + "\*";$logfiles = (Get-ChildItem $logpath -include "*log") ; $logsize = (($logfiles | Measure-Object -Sum Length).sum)/1048576KB; [math]::round($logsize, 2)}}, 		
		CircularLoggingEnabled,
        IsPublicFolderDatabase | ft -AutoSize
The rest of the column has been working great, but not sure how to get the file size to work correctly.

Thanks in advance.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell to calclualte the size of files and total file size in a directory not working ?

Post by jvierra »

This will get you further.

Code: Select all

Get-MailboxDatabase | 
    Select Server, 
		Name, 
        @{n="Number of Mbx";e={(Get-Mailbox -Database $_.Identity | Measure-Object).Count}},
		@{
            n='DB Size (GB)';
            e={
                $drive = $_.EdbFilePath.DriveName -replace ':';
                $pathname = $_.EdbFilePath.PathName -replace $_.EdbFilePath.DriveName;
                $databasepath = '\{0}\{1}${2}' -f $_.Server, $drive, $pathname;
                Write-Host $databasepatn -fore Green;
                $databasesize = (Get-ChildItem $databasepath).length/1Gb; 
                [math]::round($databasesize, 2);
            }
        }, 
		EdbFilePath,
		LogFolderPath,
		@{
            n='Log Folder Size (GB)';
            e={
        # ======>>>> fix this too!!!
                $objitem = (Get-MailboxDatabase $_.Identity);
                $logpath = "`\`\" + $objitem.server + "`\" + $objItem.LogFolderPath.DriveName.Remove(1).ToString() + "$"+ $objItem.LogFolderPath.PathName.Remove(0,2) + "\*";
                $logfiles = (Get-ChildItem $logpath -include "*log") ; $logsize = (($logfiles | Measure-Object -Sum Length).sum)/1Gb; 
                [math]::round($logsize, 2)
            }
        }, 		
		CircularLoggingEnabled,
        IsPublicFolderDatabase | 
        Format-Table -AutoSize
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: PowerShell to calclualte the size of files and total file size in a directory not working ?

Post by ITEngineer »

I got this error on the column for the DB & log size:

ERROR: You cannot call a method on a null-valued expression.

is it because I'm executing it from my laptop PowerShell ISE as Administrator:

Code: Select all

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://PRDEXC01-VM/PowerShell/ -Authentication Kerberos
Import-PSSession $s
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell to calclualte the size of files and total file size in a directory not working ?

Post by jvierra »

The code I posted is a suggestion as to how to make this work so that it can be debugged. I cannot access an Exchange server right now so I cannot test it. You will have to look at the whole error message and identify the offending object.

When using remote Exchange the returned objects will not be integers. Sizes will be special large byte structures that have been converted to strings. The code you started with assumes it is running in the Exchange Management Shell (EMS). Remotely it will not work without addressing the martialing differences.

Build the information up a piece at a time until you understand the returned objects. You will need to abandon the pipeline to do this exploration.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: PowerShell to calclualte the size of files and total file size in a directory not working ?

Post by ITEngineer »

jvierra wrote: Wed Jun 27, 2018 5:49 am The code I posted is a suggestion as to how to make this work so that it can be debugged. I cannot access an Exchange server right now so I cannot test it. You will have to look at the whole error message and identify the offending object.

When using remote Exchange the returned objects will not be integers. Sizes will be special large byte structures that have been converted to strings. The code you started with assumes it is running in the Exchange Management Shell (EMS). Remotely it will not work without addressing the martialing differences.

Build the information up a piece at a time until you understand the returned objects. You will need to abandon the pipeline to do this exploration.
Hm, yes,

For example:

Code: Select all

Get-MailboxDatabase -Server PRDEXC01-VM | Select Identity, EdbFilePath, @{n='EdpType'; e={$_.EdbFilePath.GetType().FullName}}, LogFolderPath, @{n='LogType'; e={$_.LogFolderPath.GetType().FullName}}
Returns like these:

Code: Select all

Identity      : SITE3-DB03
EdbFilePath   : G:\Mailbox\SITE3-DB03\SITE3-DB03.edb
EdpType       : System.String
LogFolderPath : G:\Mailbox\SITE3-DB03
LogType       : System.String
/* IT Engineer */
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked