Home > Miscellaneous, Scripting > Tag, You’re It!

Tag, You’re It!

July 2nd, 2011 Mark A. Weaver
Rating 3.00 out of 5

This will be part one of two posts to talk about a few little scripts I have been working on to help me search through my script repository.

Using dropbox, I have created my own little script repository that is out in the “cloud”.  I didn’t know how much I would like it, but it certainly makes it easy for me and others on my team to share the same scripts.

Anyway…the thought behind this part was to have an easy way to “tag” my scripts with information that would make it easier to search all of the scripts I have been working on.

All it is basically doing is creating some well-formed comments that I could then parse out. As a safety precaution, this version makes a backup copy of the script before modifying it. If you don’t want to back it up, just comment out the following line:

	Copy-Item -Path $Script -Destination $BackupScriptPath -Force

Without further ado, here is the code:

 

 
##-------------------------------------------------------------------
## Script: Add-ScriptTags.ps1
## Written by: Mark A. Weaver 
## Website: vmweaver.com
## 
## Version: 1.0
## Creation Date: 7/2/2011
## Purpose: This script will be used to add the specified tags to a script file
##              It will prepend the tag data to the script text as commented strings.
## Markup Definitions:
##       _TAG:  - Comma-separated list of tag words
##	 _AUTH: - Text with Authors name
##       _DESC: - Text with Description  
##       _DATE: - DateTime text with last update
##       _VER:  - Version number
##       _MISC: - Miscellaneous information
##
##    
##   Input: 
##                      -Script "Path to Script you are tagging"
##                      -Auth "AUthor Name"
##                      -Tag "comma separated list of tags"
##			-Desc "Description of the script"
##			-Date "Date information"
##			-Ver "Script Version"
##			-Misc "Miscellaneous information you wish to include"
##          
##
##  Output: 
##	SCript is modified with the information. If this script is run multiple times, this 
##      information will be over-written.
##                 
##                              
##  Assumptions: 
##
##
##  Example:
##  	.\Add-ScriptTags.ps1 -Script ..\MyScript.ps1 -Auth "Mark A. Weaver" 
##            -Tag "Test,Foo,Blah" -Desc "Test Script" -Ver "1.2"              
##               
#############################
## Updates:
##    
##
##----------------------------------------------------------------------
 
param($Script=$(throw "Script to tag must be specified!"),$Tag, $Auth, $Desc, $Date,$Ver,$Misc)
 
if (Test-Path $Script) 
{
  $Contents = Get-Content $Script 
  [array]$PostMarkup = $Contents
 
	[array]$Markup = $Null
	$Markup += "################################"
	$Markup += "## _BEGINMARKUP"
	$Markup += "####"
	$Markup += "## _TAG:" + $Tag
	$Markup += "## _AUTH:"+ $Auth
	$Markup += "## _DESC:" + $Desc
	$Markup += "## _DATE:" + $Date
	$Markup += "## _VER:" + $Ver
	$Markup += "## _MISC:" + $Misc
	$Markup += "####"
	$Markup += "## _ENDMARKUP"
	$Markup += "################################"
 
 if ($Contents -match "_BEGINMARKUP") 
  {
	  $LineCount = 0
	  $BeginLine = 0
	  $EndLine = 0
	  Foreach($Line in $Contents)
	  {
 
	    if ($Line -imatch "_BEGINMARKUP") {$BeginLine = $LineCount - 1}
		if ($Line -imatch "_ENDMARKUP"){$EndLine = $LineCount + 1}
		 $LineCount += 1
       }
	  if ($BeginLine -eq 0)
	  {
	    [array]$PreMarkup = $Null
	  }
	  else
	  {
 
 
	   [array]$PreMarkup = $Contents[0..($BeginLine - 1)]
	  }
 
	  if ($EndLine -eq ($Contents.count -1))
	  {
         [array]$PostMarkup = $Null
	  }
	  else
	  {
	     [array]$PostMarkup = $Contents[($EndLine+1)..($Contents.count -1)]
	  }
   }
 
	$MarkedUpScript = $PreMarkup + $Markup + $PostMarkup
	$ScriptObject = Get-Item $Script
	$BackupName = "Backup_" + $ScriptObject.Name
	$BackupScriptPath = $ScriptObject.DirectoryName +"\"+ $BackupName
 
	Copy-Item -Path $Script -Destination $BackupScriptPath -Force
	Out-File -FilePath $Script -InputObject $MarkedupScript -Force
	Write-Host "--- Markup written to '$($Script)' ---" -ForegroundColor Green
	Write-Host "--- (Backup: '$($BackupName)') ---" -ForegroundColor DarkYellow
        $Markup | ForEach-Object{Write-Host $_ -ForegroundColor DarkGreen}
 
}
else
{
	Throw("Script file ""$Script"" does not exist.")
}

Hope this makes sense for you.. and stay tuned for the next post which will be on processing these files and searching a repository for scripts.
As always… Happy Scripting!
— Mark

Categories: Miscellaneous, Scripting Tags:
Comments are closed.