/
Find-PSTs.ps1
79 lines (61 loc) · 2.35 KB
/
Find-PSTs.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#region Credits
# Author: Federico Lillacci - Coesione Srl - www.coesione.net
# GitHub: https://github.com/tsmagnum
# Version: 1.1
# Date: 26/02/2024
#endregion
#region TODO
#endregion
#Storing the execution time in a variable; used to name the log and results file
$executionTime = (Get-Date).ToString('yyyyMMdd-hhmm')
#region VARIABLES
#please set these variables before running the script
#Txt file with the computers to process, one per line
$Computers = Get-Content -Path "C:\Scripts\ClientsTest.txt"
#Log file of the processed computers
$processedPCs = "C:\Scripts\$($executionTime)_ProcessedPC.txt"
#Results file
$csvPath = "C:\Scripts\$($executionTime)_PstFilesFound.csv"
#endregion
#Begin script execution
#Creating an empty array for the results
$results = @()
Set-Content $processedPCs -Value "Logging script execution - $($executionTime)"
Foreach ($Computer in $Computers)
{
#Checking if the target computer is online: if so, the check continues
Write-Host -ForegroundColor Cyan "Checking if $($Computer) is online"
$pingtest = Test-Connection -ComputerName $Computer -Quiet -Count 1 -ErrorAction SilentlyContinue
if ($pingtest)
{
$message = "$($Computer) is online, looking for PST files..."
Write-Host -ForegroundColor Cyan $message
Add-Content -Path $processedPCs -Value $message
#Performing the search
$pstFiles = Get-Wmiobject -namespace "root\CIMV2" -computername $Computer -Query "Select * from CIM_DataFile Where Extension = 'pst'"
#Storing the results in PS Object
Foreach ($file in $PstFiles)
{
$result = [PSCustomObject]@{
Computer = $file.CSName
Name = $file.Filename
Path = $file.Description
Size = ($file.FileSize)/1GB
LastAccess = ($file.LastAccessed.Split("."))[0]
}
Write-Host -ForegroundColor Green "PST found! Adding $($result.Name) to results"
$results += $result
#End 3rd foreach
}
#End 2nd foreach
}
#Logging offline computers
else {
$message = "$($Computer) is offline, skipping PST search"
Write-Host -ForegroundColor Red $message
Add-Content -Path $processedPCs -Value $message
}
#End 1st foreach
}
#Saving results to a CSV file
$results | Export-Csv -Path $csvPath -NoTypeInformation