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

Ask your Windows 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.
Locked
User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Wed Jun 27, 2018 4:24 am

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 */

User avatar
jvierra
Posts: 13479
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Jun 27, 2018 4:43 am

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: 138
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Wed Jun 27, 2018 5:22 am

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 */

User avatar
jvierra
Posts: 13479
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » 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.

User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Wed Jun 27, 2018 6:21 am

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 */

Locked