What are Toolkit Extensions?

Topics: Archive - Toolkit Extensions
Coordinator
Oct 29, 2013 at 3:46 PM
Edited Nov 21, 2013 at 3:58 PM
One of the key goals of the PowerShell App Deployment Toolkit is to provide a consistent set of core functions that can be reused when deploying applications. It's very likely however, that you will have additional functions that you'd like to reuse. It's easy to create your own functions and extend the toolkit, without any modification to the core toolkit itself - which makes updating to new versions quick and painless.

To add an extension to the toolkit, just go to the body of PSAppDepoyToolkitExtensions.ps1. As an example, copy in the following function:
# Determines whether a database exists in the system.
Function Test-Database {
    Param (
    [string] $SQLServer,
    [string] $DBName)

    Write-Log "Checking for existence of database [$SQLServer - $DBName]..."

    $dbExists = $false
    Try {
        # we set this to null so that nothing is displayed
        $null = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
        # Get reference to database instance
        $server = new-object ("Microsoft.SqlServer.Management.Smo.Server") $SQLServer
        ForEach($db in $server.databases) { 
            If ($db.name -eq $DBName) { 
                $dbExists = $true 
            }
        }
    }
    Catch { $dbExists = $false }

    If ($dbExists -eq $true) { Write-Log "Database [$SQLServer - $DBName] exists" } Else { Write-Log "Database [$SQLServer - $DBName] does not exist" }

    Return $dbExists
}
In your Deploy-Application.ps1, you can now use the following:
If ((Test-Database -SQLServer "$envComputerName" -DBName "MyCustomApp") -eq $true) { 
    # Run executable that runs stored procedures on the local DB
    Execute-Process -FilePath "$dirFiles\MyAppRunStoredProcedures.exe" -WindowStyle Hidden
}
When building your own functions, we recommend to use the Write-Log function so you can see output in the PSAppDeployToolkit log file.

Feel free to post your contributions here! :)