Tuesday, November 19, 2013

PowerShell Script for finding pdf files in multiple computers

These day's I started working on Powershell, after a quite bit of time I found that using powershell was fun and easy.I have been assigned to get a list of pdf files from multiple computers over the network.After struggling I got it, Below is the script which collects the list of all pdf's.The result is exported to a CSV file.

--------------------------------------------------------------------------------------------------------
$Computers = Get-Content -Path "Give the Path where the list of computers are,this can be in a txt file"
Foreach ($Computer in $Computers){
$PdfFiles = Get-Wmiobject -namespace "root\CIMV2" -computername $Computer -Query "Select * from CIM_DataFile Where Extension = 'pdf'"
$output = @()

foreach ($file in $PdfFiles){
[bool]$firstOutput = $true
if($File.FileName){
$pdfinfo = "" | select Computer, Path, Owner, FileSize, LastModified, LastAccessed
$filepath = $file.description
$query = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=`'$filepath`'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"
$Owner = @(Get-Wmiobject -namespace "root\CIMV2" -computername $Computer -Query $query)
$Owner = $Owner[0]
$pdfinfo.Computer = $Computer
$pdfinfo.Path = $filepath
$pdfinfo.FileSize = $file.FileSize/1KB
$pdfinfo.Owner = "$($Owner.ReferencedDomainName)\$($Owner.AccountName)"
$pdfinfo.LastModified = [System.Management.ManagementDateTimeConverter]::ToDateTime($($file.LastModified))
$pdfinfo.LastAccessed = [System.Management.ManagementDateTimeConverter]::ToDateTime($($file.LastAccessed))
$output += $pdfinfo
$output | Export-Csv -Append "x:\report -$((Get-Date -uformat %Y%m%d).ToString()).csv" -NoClobber -NoTypeInformation
}
-----------------------------------------------------------------------------------------------------------
In the last line export to CSV in place of x give the drive where you want to save the report

No comments:

Post a Comment