public/Install-ConfluenceDatabase.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<#
 
.SYNOPSIS
    Install the Confluence database objects used by the PowerConfluenceSqlRefresh module
 
.DESCRIPTION
    Runs SQL scripts embedded in the PowerConfluenceSqlRefresh module to create objects and data
    First creates all schema objects, then populates lookup tables with data
 
.PARAMETER SqlInstance
    The connection string for the SQL instance to install the Confluence database on
 
.PARAMETER SqlDatabase
    The name of the database to install the Confluence objects and data in
 
.PARAMETER Username
    The username of the account that will be executing the module
     
.PARAMETER CreateUser
    Set this switch to create the user in the database before adding it to the ConfluenceRefreshRole
 
.EXAMPLE
    Install-ConfluenceDatabase -SqlInstance localhost -SqlDatabase Confluence -Username "DOMAIN\MyConfluenceUser"
 
    Installs objects and data on the local machine in a database called "Confluence"
 
.EXAMPLE
    Install-ConfluenceDatabase -SqlInstance "my.remote.sql.server,1234" -SqlDatabase Confluence -Username "DOMAIN\MyConfluenceUser" -CreateUser
 
    Installs objects and data on a remote Sql Server in a database called "Confluence", and creates the user in the database
#>

function Install-ConfluenceDatabase {
    [CmdletBinding()]
    param (
        # The sql instance of the install database
        [Parameter(Mandatory,Position=0)]
        [string]
        $SqlInstance,

        # The name of the install sql database
        [Parameter(Mandatory,Position=1)]
        [string]
        $SqlDatabase,

        # The user that will execute the Confluence Refresh
        [Parameter(Mandatory,Position=2)]
        [string]
        $Username,

        # Set this switch to create the user in the database before adding it to the role
        [Parameter()]
        [switch]
        $CreateUser
    )
    
    begin {
        Write-Verbose "Installing Confluence database on $SqlInstance in database $SqlDatabase"
    }
    
    process {
        Write-Verbose "Creating database objects"
        $objectsResult = Invoke-SqlCmd -ServerInstance $SqlInstance -Database $SqlDatabase -InputFile $global:PowerConfluenceSqlRefresh.SqlObjectsPath

        if ($CreateUser) {
            Write-Verbose "Creating user $Username"
            $createUserResult = Invoke-SqlCmd -ServerInstance $SqlInstance -Database $SqlDatabase -Query "CREATE USER [$Username]"
        }

        Write-Verbose "Adding user $Username to ConfluenceRefreshRole role"
        $addUserResult = Invoke-SqlCmd -ServerInstance $SqlInstance -Database $SqlDatabase -Query "ALTER ROLE [ConfluenceRefreshRole] ADD MEMBER [$Username]"

        Write-Verbose "Creating lookup table data"
        $lookupResult = Invoke-SqlCmd -ServerInstance $SqlInstance -Database $SqlDatabase -InputFile $global:PowerConfluenceSqlRefresh.SqlLookupsPath
    }
    
    end {
        Write-Verbose "Confluence database installation completed"
    }
}