Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- Useful links from the talk :
- PowerGUI: http://www.quest.com/powergui-freeware/
- Hey Scripting Guy! : http://blogs.technet.com/b/heyscriptingguy/
- GO FORTH AND AUTOMATE!
- @martin_evans
- http://about.me/martin.evans
- #>
- <#
- This is the full module (psm1 file) we build in the "Spinning Plates and Slaying Vampires With PowerShell" talk.
- There are a couple of environment variables that need setting up for this to work - I've added a function in this file called Set-UserEnvironmentVariable to assist you
- "casesFolder" - folder location where all your case notes would be stored
- "solutionRootFolder" - folder location in which different versions of your solution would be stored.
- "localRepo" - folder location of your patch repository - each patch is an sql file named numerically i.e. 1001.sql etc
- Type "Get-Help Set-UserEnvironmentVariable -detailed" for more info. Also, have a look at how the I've bound the help text to the Set-UserEnvironmentVariable function below.
- In the GetDB Connection you must supply the $ServerName and $DatabaseName values for your local sql server instance
- #>
- # This line replaces the now deprecated LoadWithPartialName method I mentioned last night
- Add-Type -AssemblyName "System.Data"
- function Get-Case(){
- param(
- [Parameter(Mandatory=$true)]
- [ValidatePattern('^[0-9]{3,4}$')] [int]$caseNumber
- )
- $folders = gci -Path $env:casesFolder -Filter "$caseNumber*"
- if($folders.length -gt 1){
- Write-Warning "Multiple case folders found containing $caseNumber";
- return;
- }
- if($folders.length -eq 0) {
- ni -Type directory -Path "$env:casesFolder" -name $caseNumber;
- $folder = "$env:casesFolder\$caseNumber";
- }
- else {
- $folder = "$env:casesFolder\$folders";
- }
- $notesFile = "$folder\Notes.txt";
- if((Test-Path $notesFile) -eq $false){
- "Case notes for $caseNumber" >> $notesFile;
- }
- #start "https://you_case_logging_url.com?$caseNumber"
- Invoke-Item $notesFile
- }
- function Get-MyApp() {
- param([parameter(mandatory=$true)]
- [ValidateSet("DEV","UAT","PROD")]
- $ver)
- gci -Path "$env:solutionRootFolder\$ver\myimportantapp" -Filter "*.sln" | ii
- }
- function Install-Patch() {
- Param([Parameter(Mandatory=$true)]
- [int[]]$patchNumbers)
- Push-Location $env:localRepo
- # If your local repo doesnt synch from anywhere, I'd comment out the line below!
- hg pull -u
- $conn = Get-OpenDbConnection
- foreach ($patchNumber in $patchNumbers)
- {
- $file = Get-FileMatchingCriteria $patchNumber
- if ($file){
- "Running Patch $patchNumber"
- $cmdTxt = Get-Content $file
- if($pscmdlet.ShouldProcess($patchNumber)){
- ExecuteCommand $cmdTxt $conn
- }
- else
- {
- $cmdTxt
- }
- }
- }
- $conn.Close();
- $conn.Dispose();
- Pop-Location
- }
- function Get-OpenDbConnection(){
- $ServerName = "localhost"
- $DatabaseName="OneDbToRuleThemAll"
- $connStr = [System.String]::Format("Data Source={0};Initial Catalog={1};Integrated Security=SSPI", $ServerName, $DatabaseName)
- $conn = New-Object System.Data.SqlClient.SqlConnection($connStr)
- # $action is a script block we hook up to the InfoMessage eevent on the connection
- $action = { Write-Host $event.sourceEventArgs.Message -foregroundcolor "magenta"}
- Register-ObjectEvent -InputObject $conn -EventName InfoMessage -Action $action | Out-Null
- $conn.Open()
- return $conn;
- }
- function ExecuteCommand($cmdTxt, $conn){
- $cmd = New-Object System.Data.SqlClient.SqlCommand($cmdTxt, $conn)
- $cmd.ExecuteNonQuery() | Out-Null
- $cmd.Dispose()
- }
- function Get-FileMatchingCriteria($patchNumber) {
- $file = Get-ChildItem . -include "*$patchNumber.sql" -Path "$env:localRepo" -Recurse;
- if($file -eq $null) {
- Write-Host "Unable to find patch $patchNumber" -foregroundcolor "magenta";
- return $null;
- }
- elseif ($file.count -gt 1) {
- Write-Host "Multiple patches ending in $patchNumber found. Please investigate." -foregroundcolor "magenta";
- return $null;
- }
- return $file;
- }
- function Find-Patch{
- param([string] [ValidatePattern('^[a-z]{3,}$')] $criteria)
- gci $env:localrepo -filter "*.sql" | select-string $criteria
- }
- function Show-Patch{
- param(
- [string] [ValidatePattern('^[0-9]{3,4}$')] $patchNumber,
- [switch] $showInNotePad
- )
- $file = Get-FileMatchingCriteria $patchNumber
- if($file) {
- if($showInNotePad -eq $true){
- notepad $file
- }
- else {
- Get-Content $file | Write-Host
- }
- }
- }
- <#
- .SYNOPSIS
- Sets a user environment variable
- .DESCRIPTION
- This function sets a user environment variable on the local machine. This variable will only be available to the logged in user
- .PARAMETER Name
- The name of the variable
- .PARAMETER Value
- The value of the variable you have set.
- .EXAMPLE
- PS C:\> Set-UserEnvironmentVariable casesFolder "C:\MyCases"
- .INPUTS
- System.String,System.String
- .OUTPUTS
- System.String
- .NOTES
- To override the value of an existing variable, call the function again, supplying the variable name and the new value .
- To remove the environment variable call the function again, supplying the variable name and $null as the value.
- #>
- function Set-UserEnvironmentVariable($name, $value){
- [Environment]::SetEnvironmentVariable("$name",$value,"User")
- }
- <#
- .SYNOPSIS
- Allows you to switch between one version of an app with another within the same instance of Visual Studio
- .DESCRIPTION
- I call this function when I need to switch from a dev version of an app to a version of the solution that is currently in test ot even in production.
- The convention being that I will have each version of the code in a dev, uat or prod folder within a the folder named in my $env:solutionRootFolder value
- .PARAMETER version
- The version of the code I wish to load in VS
- .EXAMPLE
- Open-MyApp PROD
- .INPUTS
- System.String
- .NOTES
- .LINK
- about_functions_advanced
- .LINK
- about_comment_based_help
- #>
- function Open-MyApp($version)
- {
- if($dte){
- $solution = $dte.solution;
- $solution.Open("$env:solutionRootFolder\$version\myimportantapp\myimportantapp.sln");
- set-location "$env:demoAppRootFolder\myapp\$version"
- }
- else {
- "This function needs to be called from within Visual Studio"
- }
- }
- Set-Alias oma Open-MyApp -Description "Opens my app in the current version of visual studio"
- Set-Alias case Get-Case -Description "Opens case notes and details in fb for the specified case notes"
- Set-Alias instp Install-Patch -Description "Install database patch"
- Set-Alias findp Find-Patch -Description "Searches for all all patches containing the supplied criteria"
- Set-Alias showp Show-Patch -Description "Displays a patch"
- Set-Alias app Get-MyApp -Description "Loads my app in visual studio"
- Set-Alias sue Set-UserEnvironmentVariable -Description "Allows you to set a user environment variable"
- export-modulemember -alias * -function Install-Patch, Find-Patch, Show-Patch, Get-MyApp, Get-Case, Set-UserEnvironmentVariable, Open-MyApp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement