Skip to content

Instantly share code, notes, and snippets.

@am1ru1
Created September 1, 2022 18:22
Show Gist options
  • Save am1ru1/110231cb3e0e737ea65f43e3e8850d6b to your computer and use it in GitHub Desktop.
Save am1ru1/110231cb3e0e737ea65f43e3e8850d6b to your computer and use it in GitHub Desktop.
$FindText = "hunttext" # <= Find this text
$MatchCase = $false
$MatchWholeWorld = $true
$MatchWildcards = $false
$MatchSoundsLike = $false
$MatchAllWordForms = $false
$Forward = $false
$Format = $false
$extractAll001 = $false
if ($extractAll001){
write-host "Extract zip.001 in to diff dir. Usually first run from dump."
dir -Recurse -file -filter *.zip.001 | %{7z.exe e -spf2 -aos $_.FullName -oC:\extracted\$_\ }
}
$extractAllZipInDir = $false
if ($extractAllZipInDir){
write-host "Extract zip in dir. Following first extract of dump, you will have archives within dump."
dir -Recurse -file -filter *.zip | %{$newname =$_.Name.replace('.zip','') ; 7z.exe e -spf2 -ppassword -aos $_.FullName -o".\$newname\" }
}
write-host "Find string in file names"
Get-ChildItem -recurse -Filter ("*"+$FindText+"*") | %{write-host FOUND: $_.fullname}
write-host "Find string in Word docs"
$word = New-Object -ComObject Word.Application
$WordFiles = Get-ChildItem -Recurse -Include "*.docx"
foreach($WordFile in $WordFiles) {
$file = $WordFile.FullName
try {
if($fileOpen = $word.Documents.Open($file)){
if ($fileOpen.Content.Find.Execute($FindText, $MatchCase, $MatchWholeWorld, $MatchWildcards, $MatchSoundsLike, $MatchAllWordForms)) {
write-host FOUND: $file contains $FindText
}
}
if($word.Application.ActiveDocument){
$word.Application.ActiveDocument.Close()
}
}catch{
}
}
if($word.Application){
$word.Application.quit(0)
}
write-host "Find string in Presentation docs"
function FindText {
param(
[object]$shape,
[string]$find
)
if ($shape.HasTextFrame)
{
$textFrame = $shape.TextFrame
$textRange = $textFrame.TextRange
return $textRange.Find($find, 0, $msoFalse, $msoFalse)
<#
#Use this if above replacement causes formatting to be lost.
$paragraphs = $textRange.Paragraphs()
foreach ($paragraph in $paragraphs)
{
$text = $paragraph.Text
if($text.Contains($find)) {
$text = $text.Replace($find, $replace)
$paragraph.Text = $text
}
}
#>
}
}
$ppoint = New-Object -ComObject Powerpoint.Application
$msoTrue = [Microsoft.Office.Core.MsoTriState]::msoTrue
$msoFalse = [Microsoft.Office.Core.MsoTriState]::msoFalse
$pptFiles = Get-ChildItem -Recurse -Include "*.pptx"
foreach($pptFile in $pptFiles) {
$file = $pptFile.FullName
#Open presentation with ReadOnly:False, Untitled:False, Visible:True
$fileOpen = $ppoint.Presentations.Open($file , $msoTrue, $msoFalse, $msoFalse)
foreach ($slide in $fileOpen.Slides) {
foreach ($shape in $slide.Shapes) {
# [Microsoft.Office.Core.MsoShapeType]::msoGroup
if ($shape.Type -eq 6) {
foreach ($item in $shape.GroupItems) {
if (FindText $item $FindText ){
write-host FOUND: $file contains $FindText
}
}
} else {
if (FindText $shape $FindText){
write-host FOUND: $file contains $FindText
}
}
}
}
if($fileOpen){
$fileOpen.Close()
}
}
if($ppoint.Application){
$ppoint.Application.quit(0)
}
Function Search-Excel {
[cmdletbinding()]
Param (
[parameter(Mandatory)]
[ValidateScript({
Try {
If (Test-Path -Path $_) {$True}
Else {Throw "$($_) is not a valid path!"}
}
Catch {
Throw $_
}
})]
[string]$Source,
[parameter(Mandatory)]
[string]$SearchText
#You can specify wildcard characters (*, ?)
)
$Excel = New-Object -ComObject Excel.Application
Try {
$Source = Convert-Path $Source
}
Catch {
Write-Warning "Unable locate full path of $($Source)"
BREAK
}
$Workbook = $Excel.Workbooks.Open($Source)
ForEach ($Worksheet in @($Workbook.Sheets)) {
# Find Method https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-find-method-excel
$Found = $WorkSheet.Cells.Find($SearchText) #What
If ($Found) {
# Address Method https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-address-property-excel
$BeginAddress = $Found.Address(0,0,1,1)
#Initial Found Cell
[pscustomobject]@{
Filename = $Source
WorkSheet = $Worksheet.Name
Column = $Found.Column
Row =$Found.Row
Text = $Found.Text
Address = $BeginAddress
}
Do {
$Found = $WorkSheet.Cells.FindNext($Found)
$Address = $Found.Address(0,0,1,1)
If ($Address -eq $BeginAddress) {
BREAK
}
[pscustomobject]@{
Filename = $Source
WorkSheet = $Worksheet.Name
Column = $Found.Column
Row =$Found.Row
Text = $Found.Text
Address = $Address
}
} Until ($False)
}
Else {
}
}
$workbook.close($false)
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$excel)
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
Remove-Variable excel -ErrorAction SilentlyContinue
}
write-host "Find string in Excel docs"
$ExcelFiles = Get-ChildItem -Recurse -Include "*.xls*"
foreach($ExcelFile in $ExcelFiles) {
$file = $ExcelFile.FullName
Search-Excel -Source $file -SearchText $FindText | Format-Table
}
write-host "Find in String context"
Get-ChildItem -Exclude *.docx,*.pptx,*.xlsx,*.zip -Recurse | foreach-object {
$file = $_.FullName
if ((Get-Content $file | %{$_ -match $FindText }) -contains $true) {
write-host FOUND: $file contains $FindText
#Add-Content c:\temp\log.txt WARNING: $_.FullName contains $lookingfor
}
}
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($ppoint) | Out-Null
Remove-Variable -Name ppoint
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($word) | Out-Null
Remove-Variable -Name word
[gc]::collect()
[gc]::WaitForPendingFinalizers()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment