PST (or other file) Locator Logon Script


Please click the +1 button if you find this post helpful.


Many organizations use Exchange for e-mail, and set mailbox size limits. In order to retain e-mail, users are then forced to archive e-mail to PST files stored on local PC's. For organizations concerned with retaining corporate knowledge and for administrations, these PST files can cause a problem, particularly since most of the time, users don't pay attention to where they are located. They easily get lost, and e-mail goes missing.

The logon script below was created a while back in order to track down PST files scattered across network shares and thousands of workstations in order to re-collect the e-mail and import it back in to a centralized e-mail archive system. While it's a pretty basic VB script, it dose the job and saved the organization (and US taxpayers) about $40,000.

The script can be easily modified to locate any file type, based on the extension, so it many possible uses. To use it you will first need to set up a database with a single table and five columns. The table name used in the script below is "PSTIndex". The column names used in the script are "ID", "LIUser", "Computer", "FileName", "FileSize". Once the database is created, you'll also need to modify the connection string in the  script to contain the proper database name, username and password. 

This script was thrown together pretty quickly, so it's pretty stupid. It simply finds PST's and writes them to the database. There is not checking to see if the data already exists or filtering out of undesirable objects. Used in conjunction with a SQL stored procedure to eliminate duplicates or a set of views or queries that gives you the results  you need & your in business. Also, while the script can be run independently, the nice part of having it run as a login script is that it runs in the user context, assigning a user name to any PST they have permissions to access. Also, since it runs every time the user logs in, if the file locations change, or new files are added, those changes are captured. The downside is that unless it's cleaned up, this database table can get very large very quickly.


'NAME:        PST Locator'
'AUTHOR:      England, Matthew D.'
'LANGUAGE:    VBScript'
'DESCRITPION: This script uses WMI to search for all PST files located on _'
'             drives that are mapped to the system it is running on. It then _'
'             writes the Computer Name, the File Name, including it's full _'
'             path, and the size of the file (in MB) to a SQL database OR text_'
'             file. This is intended to be used as a login script to locate PST _'
'             files for the LiveLink Archive Service to begin collecting & _'
'             archiving. It can also be scheduled to run on a per machine basis.'


'***************************** START OF CODE *****************************'
On Error Resume Next


Const ForAppending = 8
Const CONVERSION_FACTOR = 1024


DIM vLIUser


strComputer = "."


'DETERMINE LOGGED IN USER & STORE TO VARIABLE
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 


Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")


For Each objComputer in colComputer
vLIUser = objComputer.UserName
Next




'******USE FOR WRITING TO A DATABASE******'
Const adOpenStatic = 3
Const adLockOptimistic = 3


Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")


      objConnection.Open("Provider=SQLOLEDB;Data Source=DATABASECONNECTION;" & _
        "Initial Catalog=DATABASENAME;" & _
             "User ID=USERNAME;Password=PASSWORD;")
             
'*****************************************'


'****ABSTRACT DATA FROM LOCAL MACHINE ****'
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")


Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile Where Extension = 'pst'")
'*****************************************'




'******USE FOR WRITING TO A DATABASE******'
For Each objFile in colFiles
     objRecordset.Open "SELECT * FROM PSTIndex", _
        objConnection, adOpenStatic, adLockOptimistic
    
    objRecordset.AddNew
'   objrecordset("ID") = ""
    objRecordset("LIUser") = vLIUser
    objRecordset("Computer") = objFile.CSName
    objRecordset("FileName") = objFile.Name
    objRecordset("FileSize") = FormatNumber(objFile.FileSize / CONVERSION_FACTOR / CONVERSION_FACTOR, Default, Default, Default, GroupDigits)
    objRecordset.Update
    
    objRecordset.Close
Next
    objConnection.Close
'*****************************************'
'****************************** END OF CODE ******************************'

No comments :

Post a Comment