Discussion:
Local PST Search
(too old to reply)
snorbens
2010-03-17 20:11:01 UTC
Permalink
Hi,

I have been asked to write a script to audit local drives of all
workstations for any PST files. I found a script, from another thread here a
few years back, which I have tried to modify to write found files to a text
document but I can't get it to write all files found to a text document.
It will write files found on the C:\ drive to the text document but there
are some on a second drive (D:\) but I get a script error stating "Permission
Denied". When I click on on the error box, it proceeds to write the text file
but only with what was found on the C:\ drive.

However, if I comment out all lines relating to creating & writing to the
TextFile, the Wscript.Echo will display PST files on the D:\ drive as well
as the C:\ drive.

My script is as follows:

Set objWSHShell = Wscript.CreateObject("Wscript.Shell")
strLogonServer = objWSHShell.ExpandEnvironmentStrings("%LOGONSERVER%")
Set objNet = CreateObject("Wscript.Network")
strComputer = objNet.ComputerName

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
strLogPath = strLogonServer & "\NETLOGON\Logs\PSTAudit" & "\" & strComputer
& ".txt"


Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strLogPath) Then
Wscript.Quit
Else
Set dc = fso.Drives

For Each d in dc

if d.DriveType = 2 then 'fixed drives only

Set colFiles = objWMIService.ExecQuery("Select * from " & _
"CIM_DataFile where (Drive='" & d.DriveLetter & ":') " & _
"and (Extension = 'pst')")

If colFiles.Count = 0 Then
Wscript.Quit
End If

Set TextFile = fso.CreateTextFile(strLogPath)
For Each objFile in colFiles

'1KB = 1024 bytes
FileSizeKB = objFile.FileSize \ 1024
'1MB = 1024 * 1024 bytes (1KB * 1024)
FileSizeMB = objFile.FileSize \ 1024 \ 1024
'1GB = 1024 * 1024 * 1024 bytes (1MB * 1024)
FileSizeGB = objFile.FileSize \ 1024 \ 1024 \ 1024
If FileSizeGB >= 1 Then
Wscript.Echo objFile.Name & " " & FileSizeGB & " GBytes"
TextFile.Write(objFile.Drive & objFile.Path & objFile.Name & "." &
objFile.Extension & ",")
TextFile.Write(FileSizeGB & " GB" & vbCrLf)
ElseIf FileSizeMB >= 1 Then
Wscript.Echo objFile.Name & " " & FileSizeMB & " MBytes"
TextFile.Write(objFile.Drive & objFile.Path & objFile.Name & "." &
objFile.Extension & ",")
TextFile.Write(FileSizeMB & " MB" & vbCrLf)
ElseIf FileSizeKB >= 1 Then
Wscript.Echo objFile.Name & " " & FileSizeKB & " KBytes"
TextFile.Write(objFile.Drive & objFile.Path & objFile.FileName & "."
& objFile.Extension & ",")
TextFile.Write(FileSizeKB & " KB" & vbCrLf)
Else
Wscript.Echo objFile.Name & " " & objFile.FileSize & " Bytes"
TextFile.Write(objFile.Drive & objFile.Path & objFile.FileName & "."
& objFile.Extension & ",")
TextFile.Write(objFile.FileSize & " Bytes" & vbCrLf)
End If
Next

end if

Next
objTextFile.Close
End If

I have checked security permissions on the drive and I have full control
over it besides wscript.echo does display contents of all drives so I don't
believe it is a permissions issue on the drive itself.

Can someone help me where I am going wrong? I'm sure it is something simple
I am overlooking.

Many Thanks
snorbens
2010-03-18 11:45:02 UTC
Permalink
It's OK, I found where I was going wrong.

The line to create the text file was in the wrong place. They should be just
before the this line:

For Each d in dc
Nelson Rodriguez
2010-03-19 20:46:35 UTC
Permalink
hhh
Post by snorbens
Hi,
I have been asked to write a script to audit local drives of all
workstations for any PST files. I found a script, from another thread here a
few years back, which I have tried to modify to write found files to a text
document but I can't get it to write all files found to a text document.
It will write files found on the C:\ drive to the text document but there
are some on a second drive (D:\) but I get a script error stating "Permission
Denied". When I click on on the error box, it proceeds to write the text file
but only with what was found on the C:\ drive.
However, if I comment out all lines relating to creating & writing to the
TextFile, the Wscript.Echo will display PST files on the D:\ drive as well
as the C:\ drive.
Set objWSHShell = Wscript.CreateObject("Wscript.Shell")
strLogonServer = objWSHShell.ExpandEnvironmentStrings("%LOGONSERVER%")
Set objNet = CreateObject("Wscript.Network")
strComputer = objNet.ComputerName
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
strLogPath = strLogonServer & "\NETLOGON\Logs\PSTAudit" & "\" & strComputer
& ".txt"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strLogPath) Then
Wscript.Quit
Else
Set dc = fso.Drives
For Each d in dc
if d.DriveType = 2 then 'fixed drives only
Set colFiles = objWMIService.ExecQuery("Select * from " & _
"CIM_DataFile where (Drive='" & d.DriveLetter & ":') " & _
"and (Extension = 'pst')")
If colFiles.Count = 0 Then
Wscript.Quit
End If
Set TextFile = fso.CreateTextFile(strLogPath)
For Each objFile in colFiles
'1KB = 1024 bytes
FileSizeKB = objFile.FileSize \ 1024
'1MB = 1024 * 1024 bytes (1KB * 1024)
FileSizeMB = objFile.FileSize \ 1024 \ 1024
'1GB = 1024 * 1024 * 1024 bytes (1MB * 1024)
FileSizeGB = objFile.FileSize \ 1024 \ 1024 \ 1024
If FileSizeGB >= 1 Then
Wscript.Echo objFile.Name & " " & FileSizeGB & " GBytes"
TextFile.Write(objFile.Drive & objFile.Path & objFile.Name & "." &
objFile.Extension & ",")
TextFile.Write(FileSizeGB & " GB" & vbCrLf)
ElseIf FileSizeMB >= 1 Then
Wscript.Echo objFile.Name & " " & FileSizeMB & " MBytes"
TextFile.Write(objFile.Drive & objFile.Path & objFile.Name & "." &
objFile.Extension & ",")
TextFile.Write(FileSizeMB & " MB" & vbCrLf)
ElseIf FileSizeKB >= 1 Then
Wscript.Echo objFile.Name & " " & FileSizeKB & " KBytes"
TextFile.Write(objFile.Drive & objFile.Path & objFile.FileName & "."
& objFile.Extension & ",")
TextFile.Write(FileSizeKB & " KB" & vbCrLf)
Else
Wscript.Echo objFile.Name & " " & objFile.FileSize & " Bytes"
TextFile.Write(objFile.Drive & objFile.Path & objFile.FileName & "."
& objFile.Extension & ",")
TextFile.Write(objFile.FileSize & " Bytes" & vbCrLf)
End If
Next
end if
Next
objTextFile.Close
End If
I have checked security permissions on the drive and I have full control
over it besides wscript.echo does display contents of all drives so I don't
believe it is a permissions issue on the drive itself.
Can someone help me where I am going wrong? I'm sure it is something simple
I am overlooking.
Many Thanks
Loading...