Skip to content

Instantly share code, notes, and snippets.

@jonman364
Created November 4, 2014 15:58
Show Gist options
  • Save jonman364/fd36fed40c12247c8518 to your computer and use it in GitHub Desktop.
Save jonman364/fd36fed40c12247c8518 to your computer and use it in GitHub Desktop.
'********************************************************
'* pstfinder.vbs *
'* Jon Dunlap 2011 *
'* *
'* pstfinder.vbs finds all PST files on the C: drive *
'* of all computers under the specified OU. *
'* *
'* Usage: pstfinder.vbs [OU | CN] ... *
'* Multiple OUs or computers can be given. Must be *
'* full distinguished name. *
'********************************************************
Option Explicit
Dim args, arg
Set args = WScript.Arguments
If args.count = 0 Then 'If no OU specified, all Workstations
args = Array("OU=COMPUTERS,DC=DOMAIN,DC=COM") ' Change to base OU for computers
End If
For Each arg In args
EnumObjects("LDAP://" & arg)
Next
WScript.Echo "Done"
WScript.Quit
Sub getPSTs(strComputer)
Dim objWMIService, fso, objFile
Dim colFiles
Dim ofile
Dim errnum
Set fso = CreateObject("Scripting.FileSystemObject")
Set ofile = fso.opentextfile("pst.csv",8,true)
On Error Resume Next
Err.Clear
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
If Err.Number <> 0 Then
errnum = Err.Number
On Error GoTo 0
If errnum = 70 Then
ofile.writeline(strComputer & ",Insufficient Permissions")
ElseIf errnum = 462 Then
ofile.writeline(strComputer & ",Machine Unavailable")
Else
ofile.writeline(strComputer & ",Error: " & errnum)
End If
Exit Sub
End If
On Error GoTo 0
Set colFiles = objWMIService.ExecQuery ("Select * from CIM_DataFile Where Drive = 'C:' AND Extension = 'pst'")
If colFiles.count = 0 Then
ofile.writeline(strComputer & ",no PSTs")
Else
For Each objFile in colFiles
Dim uncPath, owner, localPath
uncPath = "\\" & strComputer & objFile.Path & objFile.FileName & "." & objFile.Extension
localPath = "C:\" & objFile.Path & objFile.FileName & "." & objFile.Extension
owner = getFileOwner(objWMIService, localPath)
ofile.writeline(strComputer & ",," & uncPath & "," & objFile.FileSize & "," & objFile.LastModified & "," & owner)
Next
End If
End Sub
Function getFileOwner(objWMI, strPath)
Dim objFSS, objSD
Dim colItems
Dim retval
On Error Resume Next
Set objFSS = objWMI.Get("Win32_LogicalFileSecuritySetting='" & strPath & "'")
If Err.Number <> 0 Then
getFileOwner = "unknown"
Exit Function
End If
retval = objFSS.GetSecurityDescriptor(objSD)
On Error GoTo 0
If retval = 0 Then
getFileOwner = objSD.Owner.Name
Else
getFileOwner = "unknown"
End If
End Function
Sub EnumObjects(strParent)
Dim colOUs
Dim objOU, objCN
On Error Resume Next
Err.Clear
Set colOUs = GetObject(strParent)
If Err.Number <> 0 Then
WScript.Echo strParent & " does not exist."
Exit Sub
End If
On Error GoTo 0
'Enumerate child OUs
colOUs.Filter = Array("organizationalUnit")
For Each objOU in colOUs
EnumObjects("LDAP://" & objOU.distinguishedName)
Next
'Get computers under this
colOUs.Filter = Array("Computer")
For Each objCN in colOUs
getPSTs(objCN.CN)
Next
'If argument was a computer, get PSTs
If colOUs.CN <> "" Then
getPSTs(colOUs.CN)
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment