Home > Miscellaneous, Powershell, Scripting > Oooey-GUI Script Searcher

Oooey-GUI Script Searcher

October 6th, 2011 Mark A. Weaver
Rating 3.50 out of 5

As a follow up to my Script Tagging post, I have put together a Powershell GUI script that one can use to search a folder structure that has tagged scripts in it.

I haven’t documented this as well as I normally would, so perhaps that will come at a later time.

Basically you launch it and the “Folder Root” will default to the current directory.

Once you enter some values in the text boxes, and “Execute” the search, the script will recursively interrogate all powershell scripts in the root folder and all child folders looking for the appropriate tags as created by the “Tagging” post.

UI ScreenShot

I hope you find it helpful and here is the code!
As always… Happy scripting!!!
— Mark

function Find-Script
{
param($Term="/*" ,$Path = ".\",$FilePattern = "*.ps1",$Tag="/*", $Auth="/*", $Desc="/*", $Misc="/*")
 
$AllScripts = Get-ChildItem * -Path $Path -include $FilePattern -recurse
[array]$AllTags = $Null
[array]$FoundScripts=$null
Foreach ($Script in $AllScripts)
{
  $Contents = Get-Content $Script
  [array]$AllTags = $Null
  if ($Contents -imatch "_BEGINMARKUP")
   {
    # ($Contents -imatch "_TAG:").gettype()
	$Found = $False
     $Tags = ([string]($Contents -match "_TAG:")).replace("#","").trim().split(":")[1].trim()
	 $Author= ([string]($Contents -match "_AUTH:")).replace("#","").trim().split(":")[1].trim()
     $Description = ([string]($Contents -match "_DESC:")).replace("#","").trim().split(":")[1].trim()
	 $Version = ([string]($Contents -match "_VER:")).replace("#","").trim().split(":")[1].trim()
	 $Miscellaneous = ([string]($Contents -match "_MISC:")).replace("#","").trim().split(":")[1].trim()
	 $ModifiedDate =([string]($Contents -match "_DATE:")).replace("#","").trim().split(":")[1].trim()
	 $AllTags +=$Tags
	 $AllTags +=$Author
	 $AllTags +=$Description
	 $AllTags +=$Version
	 $AllTags +=$Miscellaneous
 
	if (($Tags -imatch $Tag) -and ($Author -imatch $Auth) -and ($Description -imatch $Desc) -and ($Miscellaneous -imatch $Misc)-and ($AllTags -imatch $Term)){$Found = $True}
 
	If ($Found -eq $true)
	{
	  $FoundItem = "" | Select-Object Script,Author,LastModified,Tags,Description
	  $FoundItem.Script = $Script
	  $FoundITem.Author = $Author
	  $FoundItem.LastModified = $Script.LastWriteTime
	  $FoundItem.Tags = $Tags
	  $FoundItem.Description = $Description
	  $FoundScripts += $FoundItem
	}
   }
}
If ($FoundScripts -ne $null){
 # Write-Host "Search Term: $Term"
 return $FoundScripts
  }
  else
  {
    Write-Host "No Scripts found with specified criteria." -ForegroundColor Yellow
  }
}
#Generated Form Function
function GenerateForm {
 
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion
 
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$btnBrowse = New-Object System.Windows.Forms.Button
$txtPath = New-Object System.Windows.Forms.TextBox
$button1 = New-Object System.Windows.Forms.Button
$label5 = New-Object System.Windows.Forms.Label
$label4 = New-Object System.Windows.Forms.Label
$label3 = New-Object System.Windows.Forms.Label
$label2 = New-Object System.Windows.Forms.Label
$label1 = New-Object System.Windows.Forms.Label
$label6 = New-Object System.Windows.Forms.Label
$txtTag = New-Object System.Windows.Forms.TextBox
$txtMisc = New-Object System.Windows.Forms.TextBox
$txtAuth = New-Object System.Windows.Forms.TextBox
$fldBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$txtDesc = New-Object System.Windows.Forms.TextBox
$textBox2 = New-Object System.Windows.Forms.TextBox
$txtTerm = New-Object System.Windows.Forms.TextBox
$dataGridView1 = New-Object System.Windows.Forms.DataGridView
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
 
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$button1_OnClick=
{
#TODO: Place custom script here
 
if ($txtPath.text -eq "") {
$fldBrowser.ShowDialog()
$txtPath.Text = $fldBrowser.SelectedPath
}
if ($txtAuth.Text -eq ""){$Auth = "/*"}else{$Auth = $txtAuth.Text.trim()}
if ($txtTag.Text -eq ""){$Tags = "/*"}else{$Tags = $txtTag.Text.trim()}
if ($txtMisc.Text -eq ""){$Misc = "/*"}else{$Misc = $txtMisc.Text.trim()}
if ($txtTerm.Text -eq ""){$Term = "/*"}else{$Term = $txtTerm.Text.trim()}
if ($txtDesc.Text -eq ""){$Desc = "/*"}else{$Desc = $txtDesc.Text.trim()}
 
#Write-Host ($txtAuth.Text -eq "")
$Path = $txtPath.Text
#Write-Host "find-script -Path ""$Path"" -Auth $Auth -Desc $Desc -Misc $Misc -Tag $Tags -Term $Term"
 
$FoundScripts = find-script -Path "$Path" -Auth "$Auth" -Desc "$Desc" -Misc "$Misc" -Tag "$Tags" -Term "$Term"
$array= new-object System.Collections.ArrayList
if ($FoundScripts.count -gt 1) {
$array.addrange($FoundScripts)}
else
{
$array.add($FoundScripts)
}
    $dataGridview1.datasource = $array
    $form1.refresh() 
 
}
 
$dataGridView1_Selectionchanged =
{
   $f = ""
   $SelectedPath = $datagridview1.selectedcells[0].value
   if ($SelectedPath -ne $null)
   {
   $f = Get-Content $SelectedPath
   }
   $textBox2.lines =$f
}
$btnBrowse_OnClick=
{
$object = New-Object -comObject Shell.Application  
 
    $folder = $object.BrowseForFolder(0, $message, 0, "")
    if ($folder -ne $null) { 
 
$txtPath.Text = $folder.self.Path
}
}
 
$handler_form1_Load=
{
#TODO: Place custom script here
 
}
 
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}
 
#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 650
$System_Drawing_Size.Width = 1050
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Script Search Interface"
$form1.add_Load($handler_form1_Load)
 
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 900
$System_Drawing_Point.Y = 35
$button1.Location = $System_Drawing_Point
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 100
$button1.Size = $System_Drawing_Size
$button1.TabIndex = 2
$button1.Text = "Execute Search"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($button1_OnClick)
 
$form1.Controls.Add($button1)
 
$btnBrowse.DataBindings.DefaultDataSourceUpdateMode = 0
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 259
$System_Drawing_Point.Y = 36
$btnBrowse.Location = $System_Drawing_Point
$btnBrowse.Name = "btnBrowse"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 60
$btnBrowse.Size = $System_Drawing_Size
$btnBrowse.TabIndex = 14
$btnBrowse.Text = "Browse..."
$btnBrowse.UseVisualStyleBackColor = $True
$btnBrowse.add_Click($btnBrowse_OnClick)
 
$form1.Controls.Add($btnBrowse)
$txtPath.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 38
$txtPath.Location = $System_Drawing_Point
$txtPath.Name = "txtPath"
$txtPath.Text = (Get-Location).Path
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 241
$txtPath.Size = $System_Drawing_Size
$txtPath.TabIndex = 13
 
$form1.Controls.Add($txtPath)
 
$fldBrowser.RootFolder = [System.Environment+SpecialFolder]::Personal
 
$dataGridView1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 80
$dataGridView1.Location = $System_Drawing_Point
$dataGridView1.Name = "dataGridView1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 218
$System_Drawing_Size.Width = 1000
$dataGridView1.Size = $System_Drawing_Size
$dataGridView1.TabIndex = 0
$dataGridView1.Autosizecolumnsmode="AllCells"
$dataGridView1.SelectionMode = 1
$dataGridView1.Readonly = $True
$dataGridView1.add_SelectionChanged($dataGridView1_Selectionchanged)
$form1.Controls.Add($dataGridView1)
 
$textBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 300
$textBox2.Location = $System_Drawing_Point
$textBox2.Multiline = $True
$textBox2.Name = "textBox2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 350
$System_Drawing_Size.Width = 1000
$textBox2.Size = $System_Drawing_Size
$textBox2.TabIndex = 3
$textbox2.scrollbars = 3
$textBox2.Readonly=$true
$textBox2.wordwrap = $False
 
$form1.Controls.Add($textBox2)
$label5.DataBindings.DefaultDataSourceUpdateMode = 0
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 703
$System_Drawing_Point.Y = 38
$label5.Location = $System_Drawing_Point
$label5.Name = "label5"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 17
$System_Drawing_Size.Width = 46
$label5.Size = $System_Drawing_Size
$label5.TabIndex = 12
$label5.Text = "Misc:"
 
$form1.Controls.Add($label5)
 
$label6.DataBindings.DefaultDataSourceUpdateMode = 0
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$label6.Location = $System_Drawing_Point
$label6.Name = "label6"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 17
$System_Drawing_Size.Width = 200
$label6.Size = $System_Drawing_Size
$label6.TabIndex = 13
$label6.Text = "Search Folder Root:"
 
$form1.Controls.Add($label6)
 
$label4.DataBindings.DefaultDataSourceUpdateMode = 0
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X =703
$System_Drawing_Point.Y = 15
$label4.Location = $System_Drawing_Point
$label4.Name = "label4"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 46
$label4.Size = $System_Drawing_Size
$label4.TabIndex = 11
$label4.Text = "Tags:"
 
$form1.Controls.Add($label4)
 
$label3.DataBindings.DefaultDataSourceUpdateMode = 0
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 484
$System_Drawing_Point.Y = 41
$label3.Location = $System_Drawing_Point
$label3.Name = "label3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 64
$label3.Size = $System_Drawing_Size
$label3.TabIndex = 10
$label3.Text = "Description:"
 
$form1.Controls.Add($label3)
 
[System.Environment+SpecialFolder]$SearchRoot="MyDocuments"
$fldBrowser.RootFolder = $SearchRoot
 
$label2.DataBindings.DefaultDataSourceUpdateMode = 0
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 507
$System_Drawing_Point.Y = 12
$label2.Location = $System_Drawing_Point
$label2.Name = "label2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 41
$label2.Size = $System_Drawing_Size
$label2.TabIndex = 9
$label2.Text = "Author:"
 
$form1.Controls.Add($label2)
 
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
 
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 310
$System_Drawing_Point.Y = 15
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 60
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 8
$label1.Text = "All Fields:"
 
$form1.Controls.Add($label1)
 
$txtTag.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X =755
$System_Drawing_Point.Y = 12
$txtTag.Location = $System_Drawing_Point
$txtTag.Name = "txtTag"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 100
$txtTag.Size = $System_Drawing_Size
$txtTag.TabIndex = 7
 
$form1.Controls.Add($txtTag)
 
$txtMisc.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 755
$System_Drawing_Point.Y = 35
$txtMisc.Location = $System_Drawing_Point
$txtMisc.Name = "txtMisc"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 100
$txtMisc.Size = $System_Drawing_Size
$txtMisc.TabIndex = 6
 
$form1.Controls.Add($txtMisc)
 
$txtAuth.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 554
$System_Drawing_Point.Y = 12
$txtAuth.Location = $System_Drawing_Point
$txtAuth.Name = "txtAuth"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 100
$txtAuth.Size = $System_Drawing_Size
$txtAuth.TabIndex = 5
 
$form1.Controls.Add($txtAuth)
 
$txtDesc.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 554
$System_Drawing_Point.Y = 38
$txtDesc.Location = $System_Drawing_Point
$txtDesc.Name = "txtDesc"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 100
$txtDesc.Size = $System_Drawing_Size
$txtDesc.TabIndex = 4
 
$form1.Controls.Add($txtDesc)
 
$txtTerm.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 374
$System_Drawing_Point.Y = 12
$txtTerm.Location = $System_Drawing_Point
$txtTerm.Name = "txtTerm"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 100
$txtTerm.Size = $System_Drawing_Size
$txtTerm.TabIndex = 1
 
$form1.Controls.Add($txtTerm)
#endregion Generated Form Code
 
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
 
} #End Function
 
#Call the Function
GenerateForm
Categories: Miscellaneous, Powershell, Scripting Tags:
Comments are closed.