Monday, February 04, 2013

Creating a Report of the WhiteSpace in all the PST's and OST in the default Outlook profile

One of my favorite development utilities is mfcMapi which along with OutlookSpy are invaluable utilities to use when doing any Exchange development. Along with mfcMapi Stephan also has a utility called mrMapi which provides console access to a number of useful MAPI functions . Last week he added a new feature to report on the files sizes and white-space inside a PST or OST based on the Header information in the file http://blogs.msdn.com/b/stephen_griffin/archive/2013/01/28/january-2013-release-of-mfcmapi-and-mrmapi.aspx.

I thought this was a really useful thing as it can be a little bit confounding sometimes looking at PST files that have been produced by exporting a Mailbox and comparing that against the actual mailbox size. So I decided to put a script together that would get all the PST and OST file paths for default Outlook profile. Run those filepaths through mrMapi and parse console Output and the put together a HTML report of the information that ended up looking something like this (all sizes are in MB)


To use this script you need the latest copy of mrMapi from http://mfcmapi.codeplex.com/ and you need to adjust the following varible in the script to the path where you downloaded it to

$mrMapiPath = "c:\mfcmapi\mrmapi.exe"

I've put a download of the script here the code itself looks like

  1. $mrMapiPath = "c:\mfcmapi\mrmapi.exe"  
  2. $rptCollection = @()  
  3.   
  4. function ParseBitValue($String){  
  5.     $numItempattern = '(?=\().*(?=bytes)'  
  6.     $matchedItemsNumber = [regex]::matches($String, $numItempattern)   
  7.     $bytes = [INT64]$matchedItemsNumber[0].Value.Replace("(","").Replace(",","") /1MB  
  8.     return [System.Math]::Round($bytes,2)  
  9. }  
  10.   
  11. $Encode = new-object System.Text.UnicodeEncoding  
  12. ##Check for Office 2013  
  13. $RootKey = "Software\Microsoft\Office\15.0\Outlook\Profiles\" 
  14. $pkProfileskey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($RootKey, $true) 
  15.  
  16. if($pkProfileskey -eq $null){ 
  17.     $RootKey = "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles" 
  18.     $pkProfileskey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($RootKey, $true) 
  19.     $defProf = $pkProfileskey.GetValue("DefaultProfile") 
  20. } 
  21. else{ 
  22.     $OutDefault = "Software\Microsoft\Office\15.0\Outlook\" 
  23.     $pkProfileskey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($OutDefault, $true) 
  24.     $defProf = $pkProfileskey.GetValue("DefaultProfile") 
  25. } 
  26. $defProf 
  27. $pkSubProfilekey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey(($RootKey + "\\" + $defProf), $true) 
  28. foreach($Valuekey in $pkSubProfilekey.getSubKeyNames()){ 
  29.     $pkSubValueKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey(($RootKey + "\\" + $defProf + "\\" + $Valuekey ), $true) 
  30.     $pstVal = $pkSubValueKey.GetValue("001f6700") 
  31.     if($pstVal -ne $null) 
  32.     { 
  33.         $pstPath = $Encode.GetString($pstVal)  
  34.         $fnFileName = $pstPath.Replace([System.Convert]::ToChar(0x0).ToString().Trim(), "")      
  35.         if(Test-Path $fnFileName){ 
  36.             $rptObj = "" | Select Name,Type,FilePath,FileSize,FreeSpace,PrecentFree 
  37.             $fiInfo = ([System.IO.FileInfo]$fnFileName)  
  38.             $rptObj.Name = $fiInfo.Name 
  39.             $rptObj.FilePath = $fiInfo.DirectoryName 
  40.             $rptObj.Type = "PST"             
  41.             iex ($mrMapiPath + " -pst -i '$pstPath'") | foreach-object{ 
  42.                 if($_.Contains("=")){ 
  43.                     $splitArray = $_.Split("=")              
  44.                     switch ($splitArray[0].Trim()){                  
  45.                         "File Size" { 
  46.                                 $rptObj.FileSize =  ParseBitValue($splitArray[1].Trim())  
  47.                             } 
  48.                         "Free Space" { 
  49.                                 $rptObj.FreeSpace = ParseBitValue($splitArray[1].Trim())  
  50.                             } 
  51.                         "Percent free" { 
  52.                                 $rptObj.PrecentFree = $splitArray[1].Trim() 
  53.                             } 
  54.                     } 
  55.                      
  56.                 } 
  57.             } 
  58.             $rptCollection += $rptObj 
  59.         } 
  60.     } 
  61.     ## Check OSTs 
  62.     $ostVal = $pkSubValueKey.GetValue("001f6610") 
  63.     if($ostVal -ne $null) 
  64.     { 
  65.         $ostPath = $Encode.GetString($ostVal)  
  66.         $fnFileName = $ostPath.Replace([System.Convert]::ToChar(0x0).ToString().Trim(), "")      
  67.         if(Test-Path $fnFileName){ 
  68.             $rptObj = "" | Select Name,Type,FilePath,FileSize,FreeSpace,PrecentFree 
  69.             $fiInfo = ([System.IO.FileInfo]$fnFileName)  
  70.             $rptObj.Name = $fiInfo.Name 
  71.             $rptObj.FilePath = $fiInfo.DirectoryName 
  72.             $rptObj.Type = "OST" 
  73.             iex ($mrMapiPath + " -pst -i `"$fnFileName`"") | foreach-object{ 
  74.                 if($_.Contains("=")){ 
  75.                     $splitArray = $_.Split("=")              
  76.                      
  77.                     switch ($splitArray[0].Trim()){                  
  78.                         "File Size" { 
  79.                                 $rptObj.FileSize =  ParseBitValue($splitArray[1].Trim())  
  80.                             } 
  81.                         "Free Space" { 
  82.                                 $rptObj.FreeSpace = ParseBitValue($splitArray[1].Trim())  
  83.                             } 
  84.                         "Percent free" { 
  85.                                 $rptObj.PrecentFree = $splitArray[1].Trim() 
  86.                             } 
  87.                     } 
  88.                      
  89.                 } 
  90.             } 
  91.             $rptCollection += $rptObj 
  92.         } 
  93.     } 
  94. } 
  95.  
  96.  
  97. $tableStyle = @"  
  98. <style>  
  99. BODY{background-color:white;}  
  100. TABLE{border-width: 1px;  
  101. border-style: solid;  
  102. border-color: black;  
  103. border-collapse: collapse;  
  104. }  
  105. TH{border-width: 1px;  
  106. padding: 10px;  
  107. border-style: solid;  
  108. border-color: black;  
  109. background-color:thistle  
  110. }  
  111. TD{border-width: 1px;  
  112. padding: 2px;  
  113. border-style: solid;  
  114. border-color: black;  
  115. background-color:white  
  116. }  
  117. </style>  
  118. "@ 
  119.  
  120. $body = @"  
  121. <p style="font-size:25px;family:calibri;color:#ff9100">  
  122. $TableHeader  
  123. </p>  
  124. "@  
  125.   
  126. $rptCollection | ConvertTo-HTML -head $tableStyle –body $body |Out-File c:\temp\ProfileReport-$defProf.htm  

1 comment:

John Carter said...

Great information for all Exchange server users.. Good Job!!