Journeym

remove.doc.pers.info

Jan 10th, 2017
1,463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $successlines=@()
  2. $global:errorlinesexcel=@()
  3. $global:errorlinesword=@()
  4. $errorlinespowerpoint=@()
  5. #------------------------------------------FUNCTIONS---------------------------------------------#
  6. Function Select-FolderDialog
  7. {
  8.     param([string]$Description="Select Folder",[string]$RootFolder="Desktop")
  9.  
  10.  [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
  11.      Out-Null    
  12.  
  13.    $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
  14.         $objForm.Rootfolder = $RootFolder
  15.         $objForm.Description = $Description
  16.         $Show = $objForm.ShowDialog()
  17.         If ($Show -eq "OK")
  18.         {
  19.             Return $objForm.SelectedPath
  20.         }
  21.         Else
  22.         {
  23.             Write-Error "Operation cancelled by user."
  24.         }
  25.     }
  26.  
  27. function CheckforPasswordProtection ($obj){
  28. $Binary = [System.IO.File]::ReadAllBytes($obj.FullName)
  29. $Start = [System.Text.Encoding]::Default.GetString($Binary[0000..2000])
  30. Switch ($obj.Extension){
  31.     ".xls" {
  32.         if($Start -match "E.n.c.r.y.p.t.e.d.P.a.c.k.a.g.e") {
  33.             return $true
  34.         }
  35.         #if ($Binary[0x208]-eq 0xFE){
  36.         #    return $true
  37.         #    }
  38.         if ($Binary[0x214]-eq 0x2F){
  39.             return $true
  40.             }
  41.     }
  42.     ".xlsx"{
  43.         if($Start -match "E.n.c.r.y.p.t.e.d.P.a.c.k.a.g.e") {
  44.             return $true
  45.         }
  46.         #if ($Binary[0x208]-eq 0xFE){
  47.         #    return $true
  48.         #    }
  49.         if ($Binary[0x214]-eq 0x2F){
  50.             return $true
  51.             }
  52.     }
  53.     ".doc" {
  54.         if($Start -match "E.n.c.r.y.p.t.e.d.P.a.c.k.a.g.e") {
  55.             return $true
  56.         }
  57.         if ($Binary[0x20B]-eq 0x13){
  58.                 return $true
  59.             }
  60.     }
  61.     ".docx"{
  62.         if($Start -match "E.n.c.r.y.p.t.e.d.P.a.c.k.a.g.e") {
  63.             return $true
  64.         }
  65.         if ($Binary[0x20B]-eq 0x13){
  66.                 return $true
  67.         }
  68.     }
  69. }
  70. }
  71.  
  72. function TestFileLock ($FilePath ){
  73.     $FileLocked = $false
  74.     $FileInfo = New-Object System.IO.FileInfo $FilePath
  75.     trap {Set-Variable -name Filelocked -Value $true -scope 1; continue}
  76.     $FileStream = $FileInfo.Open( [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None )
  77.     if ($FileStream) {$FileStream.Close()}
  78.     $FileLocked
  79. }
  80.  
  81. function RemoveModificationProtectionsandPersonalInfoWord{
  82.     $temp=$env:TEMP+"\"+$documents.Name
  83.     try{
  84.     $documents.SaveAs([ref] $temp, [ref] $null,[ref] $false ,[ref]'', [ref]$null,[ref]'',[ref] $false)
  85.     }
  86.     catch{
  87.     Write-Host "File '$($obj.fullname)' saving failed, error: '$($_.Exception.Message)'" -ForegroundColor Red
  88.     $global:errorlinesword+="'$($obj.fullname)' saving failed, error: '$($_.Exception.Message)'"
  89.     }
  90.     try{
  91.     $documents.RemoveDocumentInformation($WdRemoveDocType::wdRDIAll)
  92.     $documents.Save()
  93.     }
  94.     catch{
  95.     Write-Host "File '$($obj.fullname)' clearing metadata failed" -ForegroundColor Red
  96.     $global:errorlinesword+="'$($obj.fullname)' clearing metadata failed"
  97.     }
  98.     $documents.close()
  99.     if (!$Error) {
  100.     Move-Item -Path $temp -Destination $obj.FullName -Force
  101.     } else {Remove-Item -Path $temp -Force}
  102.     $temp=$null
  103. }
  104. function RemoveModificationProtectionsandPersonalInfoExcel{
  105.     $temp=$env:TEMP+"\"+$documents.Name
  106.     $documents.CheckCompatibility=$false
  107.     try{
  108.     $documents.SaveAs($temp,$documents.FileFormat,'','',$false)
  109.     }
  110.     catch{
  111.     try{$objexcel.workbooks.close()}catch{}
  112.     Write-Host "File '$($obj.fullname)' saving failed, error: '$($_.Exception.Message)'" -ForegroundColor Red
  113.     $global:errorlinesexcel+="'$($obj.fullname)' saving failed, error: '$($_.Exception.Message)'"
  114.     $temp=$null
  115.     return
  116.     }    
  117.     try {
  118.     $documents.RemoveDocumentInformation($XlRemoveDocType::xlRDIAll)
  119.     $documents.Save()
  120.     }
  121.     catch{
  122.     if ($_.Exception.Message -match "Cannot remove PII from this document because the document is signed, protected, shared, or marked as read-only" ){
  123.     Write-Host "File '$($obj.fullname)' has protected sheets" -ForegroundColor Red
  124.     $global:errorlinesexcel+="'$($obj.fullname)' has protected sheets"
  125.     } else {
  126.     try{$objexcel.workbooks.close()}catch{}
  127.     Write-Host "File '$($obj.fullname)' saving failed, error: '$($_.Exception.Message)'" -ForegroundColor Red
  128.     $global:errorlinesexcel+="'$($obj.fullname)' saving failed, error: '$($_.Exception.Message)'"
  129.     }
  130.     }
  131.     try{$objexcel.workbooks.close()}catch{}
  132.     if (!$Error) {
  133.     Move-Item -Path $temp -Destination $obj.FullName -Force
  134.     } else {try{Remove-Item -Path $temp -Force -ErrorAction Stop} catch {}}
  135.     $temp=$null
  136. }
  137. #------------------------------------------SETTINGS---------------------------------------------#
  138. $path = Select-FolderDialog # choose folder dialor
  139. #$path='C:\Temp'
  140. #$path=(Get-Item -Path ".\" -Verbose).FullName
  141. $errorlog = $('{0}\Errors_{1}.txt' -f $path, $('{0:yyyy-MM-dd_HH-mm-ss}' -f (Get-Date)))
  142. $successlog = $('{0}\Success_{1}.txt' -f $path, $('{0:yyyy-MM-dd_HH-mm-ss}' -f (Get-Date)))
  143. if ($path -eq $null) {exit}
  144. Add-Type -AssemblyName Microsoft.Office.Interop.Word
  145. Add-Type -AssemblyName Microsoft.Office.Interop.Excel
  146. Add-Type -AssemblyName Microsoft.Office.Interop.Powerpoint
  147. $WdRemoveDocType = "Microsoft.Office.Interop.Word.WdRemoveDocInfoType" -as [type]
  148. $XlRemoveDocType = "Microsoft.Office.Interop.Excel.XlRemoveDocInfoType" -as [type]
  149. $PpRemoveDocType = "Microsoft.Office.Interop.PowerPoint.PpRemoveDocInfoType" -as [type]
  150. $wordFiles = Get-ChildItem -Path $path -include *.doc, *.docx -Recurse
  151. $excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -Recurse
  152. $powerpointfiles = Get-ChildItem -Path $path -include *.ppt, *.pptx -Recurse
  153. #------------------------------------------WORD FILES---------------------------------------------#
  154. if ($wordFiles -ne $null) {
  155. $objword = New-Object -ComObject word.application
  156. $objword.visible = $false
  157. Write-Host "Processing Word files"
  158. foreach($obj in $wordFiles)
  159. {
  160.     if (TestFileLock $obj.FullName) {
  161.     Write-Host "File '$($obj.fullname)' is locked" -ForegroundColor Red
  162.     $global:errorlinesword+="'$($obj.fullname)' is locked"
  163.     } else {
  164.     if (CheckforPasswordProtection($obj) -eq $true){
  165.     Write-Host "File '$($obj.fullname)' is password protected" -ForegroundColor Red
  166.     $global:errorlinesword+="'$($obj.fullname)' is password protected"
  167.     } else {
  168.     $Error.Clear()
  169.     try {
  170.     $documents = $objword.Documents.Open($obj.fullname,$null,$true,$null,"11","",$false,"","",'wdOpenFormatAuto',$null,$false)
  171.     }
  172.     catch{
  173.     if ($_.Exception.Message -match "The password is incorrect" ){
  174.     Write-Host "File '$($obj.fullname)' has password for opening" -ForegroundColor Red
  175.     $global:errorlinesword+="'$($obj.fullname)' has password for opening"
  176.     }
  177.     else {
  178.     Write-Host "File '$($obj.fullname)' open failed, error '$($_.Exception.Message)'" -ForegroundColor Red
  179.     $global:errorlinesword+="'$($obj.fullname)' open failed, error '$($_.Exception.Message)'"
  180.     }
  181.     }    
  182.     if (!$Error) {RemoveModificationProtectionsandPersonalInfoWord}
  183.     if (!$Error) {
  184.     Write-Host "File '$($obj.fullname)' removed modification restrictions and cleared metadata" -ForegroundColor Green
  185.     $successlines+="'$($obj.fullname)' removed modification restrictions and cleared metadata"
  186.     }
  187.     }
  188.     }
  189. }
  190. $objword.Quit()
  191. $documents=$null
  192. }
  193.  
  194. #------------------------------------------EXCEL FILES---------------------------------------------#
  195. if ($excelFiles -ne $null) {
  196. $objexcel = New-Object -ComObject excel.application
  197. $objexcel.visible = $false  
  198. $culturebackup=Get-Culture
  199. Set-Culture en-US
  200. Write-Host "Processing Excel files"
  201. foreach($obj in $excelFiles)
  202. {
  203.     if (TestFileLock $obj.FullName) {
  204.     Write-Host "File '$($obj.fullname)' is locked" -ForegroundColor Red
  205.     $global:errorlinesexcel+="'$($obj.fullname)' is locked"
  206.     } else {
  207.     if (CheckforPasswordProtection($obj) -eq $true){
  208.     Write-Host "File '$($obj.fullname)' is password protected" -ForegroundColor Red
  209.     $global:errorlinesexcel+="'$($obj.fullname)' is password protected"
  210.     } else {
  211.     $Error.Clear()
  212.     try {
  213.     $documents = $objexcel.Workbooks.Open($obj.fullname,$false,$true,5,'11','',$true)
  214.     }
  215.     catch{
  216.     if ($_.Exception.Message -match "The password is incorrect" ){
  217.     Write-Host "File '$($obj.fullname)' has password for opening" -ForegroundColor Red
  218.     $global:errorlinesexcel+="'$($obj.fullname)' has password for opening"
  219.     }
  220.     else {
  221.     Write-Host "File '$($obj.fullname)' open failed, error '$($_.Exception.Message)'" -ForegroundColor Red
  222.     $global:errorlinesexcel+="'$($obj.fullname)' open failed, error '$($_.Exception.Message)'"
  223.     }
  224.     }
  225.     if (!$Error) {RemoveModificationProtectionsandPersonalInfoExcel}
  226.     if (!$Error) {
  227.     Write-Host "File '$($obj.fullname)' removed modification restrictions and cleared metadata" -ForegroundColor Green
  228.     $successlines+="'$($obj.fullname)' removed modification restrictions and cleared metadata"
  229.     }
  230.     }
  231.     }
  232. }
  233. $objexcel.Quit()
  234. Set-Culture $culturebackup
  235. $documents=$null
  236. }
  237. #------------------------------------------POWERPOINT FILES---------------------------------------------#
  238. if ($powerpointfiles -ne $null) {
  239. $objpowerpoint = New-Object -ComObject Powerpoint.Application
  240. Write-Host "Processing Powerpoint files"
  241. foreach($obj in $powerpointfiles)
  242. {
  243.     if (TestFileLock $obj.FullName) {
  244.     Write-Host "File '$($obj.fullname)' is locked" -ForegroundColor Red
  245.     $errorlinespowerpoint+="'$($obj.fullname)' is locked"
  246.     } else {
  247.     $documents=$objpowerpoint.Presentations.Open($obj.FullName, $false, $null, $false)
  248. #    Start-Sleep -s 2
  249.     $documents.RemoveDocumentInformation($PpRemoveDocType::ppRDIAll)
  250.     $documents.Save()
  251.     $documents.Close()
  252.     Write-Host "File '$($obj.fullname)' metadata cleared" -ForegroundColor Green
  253.     $successlines+="'$($obj.fullname)' metadata cleared"
  254.     }
  255. }
  256. $objpowerpoint.Quit()
  257. $documents=$null
  258. }
  259. try {
  260. $objpowerpoint.Quit()
  261. $objexcel.Quit()
  262. $objword.Quit()
  263. } catch {write-host “Killing remaining active applications”}
  264. #------------------------------------------LOG FILES---------------------------------------------#
  265. $global:errorlinesexcel | Out-File -FilePath $errorlog
  266. $global:errorlinesword | Out-File -FilePath $errorlog -Append
  267. $errorlinespowerpoint | Out-File -FilePath $errorlog -Append
  268. $successlines | Out-File -FilePath $successlog
  269. #------------------------------------------SUMMARY---------------------------------------------#
  270. Write-Host "Summary:" -ForegroundColor Red
  271. Write-Host "Processed $($wordFiles.Count+$excelFiles.Count+$powerpointfiles.Count) files"
  272. Write-Host "Excel files $($excelFiles.Count), Errors: $($global:errorlinesexcel.count)"
  273. Write-Host "Word files $($wordFiles.Count), Errors: $($global:errorlinesword.count)"
  274. Write-Host "Powerpoint files $($powerpointfiles.Count), Errors: $($errorlinespowerpoint.count)"
  275. Write-Host "Success log is located: $($successlog)" -ForegroundColor Red
  276. Write-Host "Error log is located: $($errorlog)" -ForegroundColor Red
  277. $HOST.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”) | OUT-NULL
Advertisement
Add Comment
Please, Sign In to add comment