none
Script to find active PST files

    Question

  • I need a logon vb script that will check if the current user has a PST file attached to their outlook profile, the location and size of the pst file.  I need the script to find pst files that are in use by the user and check the size and if they exceed a size I set in the script, an email sent to me.  I have some scripts that find the pst files but can't get the size of them or to send me a warning email.

    Most of my users have their PST files on a network drive even though not recommended by Microsoft.

    Any expert help from you expert script writers is greatly appreciated.

    John

    Friday, September 17, 2010 3:03 PM

Answers

  • I found code to find the PST files and determine the size. However, emailing from a logon script may not work. Some methods of sending an email require that something be installed. Since in this case all users have Outlook, we can use that, but when I test the script below I get several security alerts asking if it is OK to access Outlook. Also, I tested with Outlook running. I assume in a logon script you would need to possibly provide credentials, or otherwise logon to Outlook. It might make more sense to simply write the information to a shared file on a server. In any case, the example below might get you started:

    Option Explicit
    
    Dim objOutlook, k, strPSTFile, objFSO, objPSTFile
    Dim objNetwork, objMessage
    
    Const olNotExchange = 3
    Const olMailItem = 0
    Const SIZE_LIMIT = 500000000
    
    Set objOutlook = CreateObject("Outlook.Application")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objNetwork = CreateObject("Wscript.Network")
    
    For k = 1 To objOutlook.Session.Stores.Count
      If (objOutlook.Session.Stores.Item(k).ExchangeStoreType = olNotExchange) Then
        strPSTFile = objOutlook.Session.Stores.Item(k).FilePath
        Set objPSTFile = objFSO.GetFile(strPSTFile)
        If (objPSTFile.Size > SIZE_LIMIT) Then
          Set objMessage = objOutlook.CreateItem(olMailItem)
          objMessage.Subject = "PST file too large" 
          objMessage.Recipients.Add "MyAccount@myisp.com"
          objMessage.Body = "Username: " & objNetwork.UserName _
            & vbCrLf & "Computer: " & objNetwork.ComputerName _
            & vbCrLf & "PST file: " & strPSTFile _
            & vbCrLf & "File size: " & FormatNumber(objPSTFile.Size, 0)
          Wscript.Echo objMessage.Body
          objMessage.Recipients.ResolveAll
          objMessage.Send
        End If
      End If
    Next
    
    Richard Mueller
    MVP ADSI
    • Marked as answer by jdsam Monday, September 20, 2010 1:35 PM
    Friday, September 17, 2010 6:34 PM

All replies

  • I found code to find the PST files and determine the size. However, emailing from a logon script may not work. Some methods of sending an email require that something be installed. Since in this case all users have Outlook, we can use that, but when I test the script below I get several security alerts asking if it is OK to access Outlook. Also, I tested with Outlook running. I assume in a logon script you would need to possibly provide credentials, or otherwise logon to Outlook. It might make more sense to simply write the information to a shared file on a server. In any case, the example below might get you started:

    Option Explicit
    
    Dim objOutlook, k, strPSTFile, objFSO, objPSTFile
    Dim objNetwork, objMessage
    
    Const olNotExchange = 3
    Const olMailItem = 0
    Const SIZE_LIMIT = 500000000
    
    Set objOutlook = CreateObject("Outlook.Application")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objNetwork = CreateObject("Wscript.Network")
    
    For k = 1 To objOutlook.Session.Stores.Count
      If (objOutlook.Session.Stores.Item(k).ExchangeStoreType = olNotExchange) Then
        strPSTFile = objOutlook.Session.Stores.Item(k).FilePath
        Set objPSTFile = objFSO.GetFile(strPSTFile)
        If (objPSTFile.Size > SIZE_LIMIT) Then
          Set objMessage = objOutlook.CreateItem(olMailItem)
          objMessage.Subject = "PST file too large" 
          objMessage.Recipients.Add "MyAccount@myisp.com"
          objMessage.Body = "Username: " & objNetwork.UserName _
            & vbCrLf & "Computer: " & objNetwork.ComputerName _
            & vbCrLf & "PST file: " & strPSTFile _
            & vbCrLf & "File size: " & FormatNumber(objPSTFile.Size, 0)
          Wscript.Echo objMessage.Body
          objMessage.Recipients.ResolveAll
          objMessage.Send
        End If
      End If
    Next
    
    Richard Mueller
    MVP ADSI
    • Marked as answer by jdsam Monday, September 20, 2010 1:35 PM
    Friday, September 17, 2010 6:34 PM
  • I know this doesn't meet your requirements, but you could get the mail send functionality via logon script with Powershell 2.0

    $foundPST = $false
    $body = "Username: $env:USERNAME`n"
    $body += "Computer: $env:COMPUTERNAME`n"
    $app.Session.Stores |where {$_.ExchangeStoreType -eq 3} |foreach {
      Get-ChildItem $_.FilePath
    } |where {$_.Length -gt 171359} |foreach {
      $foundPST = $true
      $body += "PST file: {0}`n" -f $_.fullname
      $body += "File Size: {0}`n`n" -f $_.Length
    }
    if ($foundPST) {
      Send-MailMessage -From "me@me.com" -To "you@you.com" -Body $body -Subject "PST file too large" -SmtpServer "smtp.server.com"
    }
    I chose that length to test it, but you get the idea.
    write-host ((0..56)|%{if (($_+1)%3 -eq 0){[char][int]("116111101110117102102064103109097105108046099111109"[($_-2)..$_] -join "")}}) -separator ""
    Friday, September 17, 2010 8:41 PM
  • Richard,

    Thanks for the code, I did test it and it also asked me to allow it to run.  I think I can work with this and will try modifying it some.

    Thanks for your help.

    John

    Monday, September 20, 2010 11:58 AM
  • Tom,

    Thanks for the code.  I'm not sure I have the knowledge to incorporate a powershell script with a VB script.

    I'll keep you posted on my progress.

    Thanks,

    John

    Monday, September 20, 2010 12:02 PM
  • Richard,

    Modified the code to send an email.  It no longer asks for access or warns about secuirty.  I want it to send an email rather than write a file so that I get warned when someone has a large PST file and I don't have to remember to look at a file.

    Here is the modified code that worked for me:

     

     

    Option Explicit

    Dim objOutlook, k, strPSTFile, objFSO, objPSTFile Dim objNetwork, objMessage

    Const olNotExchange = 3
    Const olMailItem = 0
    Const SIZE_LIMIT = 500000000

    Set objOutlook = CreateObject("Outlook.Application")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objNetwork = CreateObject("Wscript.Network")

    For k = 1 To objOutlook.Session.Stores.Count
      If (objOutlook.Session.Stores.Item(k).ExchangeStoreType =
    olNotExchange) Then
        strPSTFile = objOutlook.Session.Stores.Item(k).FilePath
        Set objPSTFile = objFSO.GetFile(strPSTFile)
       
          If (objPSTFile.Size > SIZE_LIMIT) Then
          Set objMessage = CreateObject("CDO.Message")
          objMessage.From = "user1@mydomain.com"
          objMessage.To = "user2@mydomain.com"
          objMessage.Subject = "PST file too large"
          objMessage.TextBody = "Username: " & objNetwork.UserName _
            & vbCrLf & "Computer: " & objNetwork.ComputerName _
            & vbCrLf & "PST file: " & strPSTFile _
            & vbCrLf & "File size: " & FormatNumber(objPSTFile.Size, 0)
          objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
          objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mailservername_goes_here"
          objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
          objMessage.Configuration.Fields.Update
          objMessage.Send
          End If
      End If
    Next

    Monday, September 20, 2010 1:01 PM
  • Hello everybody,

    How can i search default pst name where current outlook email delivery has set. because i want to run above code only for current PST where new emails are coming, not for all PST's which are added into Outlook.

     

    Dhiraj

    Sunday, May 15, 2011 9:29 AM
  • Hello,

    Good day, I do have a similar coding for checking the outlook PST file size. But its not working in outlook 2003, but works fine in 2007 and 2010. objOutlook.Session.Stores gives an error message...Can you please guide me?

    Friday, June 08, 2012 8:47 AM
  • 'Option Explicit
    'Dim objOutlook, k, strPSTFile, objFSO, objPSTFile
    'Dim objNetwork, objMessage
     
    Const olNotExchange = 3
    Const olMailItem = 0
    Const SIZE_LIMIT = 2147483648
    Largepast = ""
    s=0
    c=0
    oc=0

    '----------------------------------------
    'To find outlook.exe service is running
    '----------------------------------------

    set service = GetObject ("winmgmts:")
     
    for each Process in Service.InstancesOf ("Win32_Process")
                    If Process.Name = "OUTLOOK.EXE" then

        oc = oc + 1

    '-------------------------------------------------------------
    'If outlook.exe running, then check the active outlook account
    '-------------------------------------------------------------

    Set objOutlook = CreateObject("Outlook.Application")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objNetwork = CreateObject("Wscript.Network")
    For k = 1 To objOutlook.Session.Stores.Count
      If (objOutlook.Session.Stores.Item(k).ExchangeStoreType =olNotExchange) Then
        strPSTFile = objOutlook.Session.Stores.Item(k).FilePath
        Set objPSTFile = objFSO.GetFile(strPSTFile)

    '------------------------------------------------------
    'Appending the result if more than one PST exists
    '------------------------------------------------------   
            If (objPSTFile.Size < SIZE_LIMIT) Then

     c = c + 1            

     Else               

    Largepast = Largepast +  strPSTFile  & ".........." & "File Size: " & FormatNumber((objPSTFile.Size/1048576), 0) & "MB" & vbCrLf         

    s = S + 1              

     End If 
      End If

    Next

      End If

    Next

     if (s > 0) then

      x= msgbox ("ATTENTION" & vbCrLf & "---------------" & vbCrLf &"Below mentioned Outlook PST file(s) are more than recommended Size (2GB)." & vbCrLf & vbCrLf &"Kindly create a New PST file to move the emails and prevent corruption of PST data file" & vbCrLf & vbCrLf & "Location: " & vbCrLf &  Largepast,48,"Microsoft Outlook")

     End If

     if (c > 0) And (S = 0) then

      x= msgbox ("The Outlook PST files are within the recommended size (2GB)", 64, "Microsoft Outlook" )

     End If

     if (oc > 0) and (c=0) and (s=0) then

      x= msgbox ("There is no Outlook PST file configured", 64, "Microsoft Outlook" )

     End If

     if (oc = 0) then

      x= msgbox ("Outlook is not running. Please open your Outlook and run this tool again", 64, "Microsoft Outlook" )

     End If

    Friday, June 08, 2012 8:48 AM
  • Kumar - this thread is closed.  Can you please open a new thread with you question.  Lok at teh threads from the past few days.  The quesion was actually asked and answerd two days ago with a good example.


    ¯\_(ツ)_/¯

    Friday, June 08, 2012 12:42 PM