Archive

Archive for the ‘Powershell’ Category

Oooey-GUI Script Searcher

October 6th, 2011 Mark A. Weaver Comments off
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:

Powershell 3.0 – drool

September 21st, 2011 Mark A. Weaver No comments
Rating 3.00 out of 5

Okay, so if you live under a rock and haven’t heard, the CTP for the Windows Management Framework 3.0 has been released by Microsoft (http://www.microsoft.com/download/en/details.aspx?id=27548).

I know, you are probably thinking “Big Deal!!” another Community Tech Preview. Not so fast there… THIS CTP includes Powershell 3.0!

Let me restate: “THIS CTP INCLUDES POWERSHELL 3.0!”

What I hope to do is post more as I begin to learn about what is new, what is cool, what is fresh, etc.

A little story about me and Powershell 3.0 ….

When I found out about the Windows 8 – Developer Preview, I wasn’t excited about the new interface, the new look of familiar applications… no. After I found out how to actually get to a command prompt, my first thing was to run Powershell and do a:

$host.version

To my UTTER DELIGHT, I saw the number “3″ in the output. My colleagues, of course, we interested in why I was ooo-ing and ahhh-ing.. then they saw that I was sitting at a Powershell prompt (of course) and the “geek”, “nerd”, and “dork” comments began.

Anyway, needless to say, I soon discovered I could install Powershell 3 on my Windows 7, SP1 laptop at home so I prompty did so upon arriving home from work.

Wow…that was a longer little story than I was expecting.

The bottom line is that I finally have it installed and have begun playing with it and I am a TOTAL GEEK.

——————————

One of the things I was afraid of was losing my Powershell 2.0 installation, yet I had read that they could co-exist. Well, seeing is believing, and I am a believer now.

Launching “powershell.exe” from a command line, by default, will open up version 3.

Launching “powershell.exe -version 2″ from a command line will bring up version 2.

Yes, it really is that simple.

Upon a cursory glance at the cmdlets available I notice that there are many more geared toward command line management of the system and features.

get-controlpanelitem

show-controlpanelitem “Display”

get-counter

new-webbinding

Also there is a cmdlet that helps you explore all of the cmdlets available via a GUI:

show-command

I have, literally, been playing with the new powershell for about an hour, so stay tuned for more updates on this.

Please, everyone, go grab it, kick the tires, run it through its paces, and poke it with a stick.

As always, happy scripting and have a good time discovering Powershell 3.0.

— Mark

Give me some DATA! (with Powershell)

March 17th, 2011 Mark A. Weaver No comments
Rating 3.00 out of 5

Ah…wow.. has it really been over a YEAR since I last post? Sorry about that, but it has been an absolutely nutty year.

Anyway..
In my new job and new role, I get to have fun showing people the power of automation with Powershell.
One of the recent tasks I was helping with is to take a list of users (from AD) and query a database to get some info about these users.

While I had done some work with SQL queries with Powershell, I hadn’t ever “functionalized” it, so I decided to take this opportunity to do so.

While I am sure there are a bazillion examples of this out there, this is what I came up with and here is what it looks like.
Sorry there aren’t many (any) in-line comments like I normally do, but if you have questions, please let me know!
Thanks and hope you find this helpful.

As always… Happy Scripting!
– Mark

##-------------------------------------------------------------------
## Script: Get-SQLData.ps1
## Written by: Mark A. Weaver 
## Website: vmweaver.com
## 
## Version: 1.0
## Creation Date: 3/16/2011
## Purpose: This is really a function that you pass 3 parameters to. It will connect to the 
##          SQL Server you specify, to the Database you specify, and execute the
##          query you specify.
##          This connects with you existing credentials (integrated).
##
##    
##   Input: 
##                      -SQLServer "Name of your sql server"
##                      -Database "Name of your database"
##                      -Query "your query"
##     *Note if you want to have a query that spans multiple lines, etc, you can pass in a 
##      'here-string' or construct another multi-line (not array) string.
##          
##
##  Output: 
##      An array is passed back containing the data from your query.
##      If no data is returned, the array will be $null
##                 
##                              
##  Assumptions: 
##               
##               
#############################
## Updates:
##    
##
##----------------------------------------------------------------------
Function Get-SQLData
{
   param ([string]$SQLServer,
          [string]$Database,
          [string]$Query)    
 
        $SQLData=@()     
        $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
        $SqlConnection.ConnectionString = "Server = $SqlServer; Database = $Database; Integrated Security = True"
        $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
        $SqlCmd.CommandText = $Query
        $SqlCmd.Connection = $SqlConnection
        $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
        $SqlAdapter.SelectCommand = $SqlCmd
        $DataSet = New-Object System.Data.DataSet
        $SqlAdapter.Fill($DataSet) | Out-Null
        $SqlConnection.Close()
 
        $RecordCount = ($DataSet.tables[0] | Measure-Object).count
        if ($RecordCount -eq 0)
        {
            $SQLData = $Null
        }
        else
        {
            $SQLData = $DataSet.tables[0] 
        }
  return $SQLData
}