public/Get-SqlConnectionString.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
function Get-SqlConnectionString {
<#
    .SYNOPSIS
    Updates a connection strings to source SQL databases with new server and database names.
 
    .DESCRIPTION
    Helper function to help create valid connection strings to source SQL databases.
 
    .PARAMETER SourceSqlServer
    Name of the SQL server, including instance and port if required.
 
    .PARAMETER SourceSqlDatabase
    Name of the source SQL database.
 
    .PARAMETER ExistingConnectionString
    The existing SQL connection string obtained from the cube definition or config file.
 
    .EXAMPLE
    Get-SqlConnectionString -SourceSqlServer myserver -SourceSqlDatabase mydatabase -ExistingConnectionString 'Provider=SQLNCLI11;Data Source=localhost;Initial Catalog=DatabaseToPublish;Integrated Security=SSPI;Persist Security Info=false';
 
    Returns
    'Provider=SQLNCLI11;Data Source=myserver;Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=mydatabase'
 
    .LINK
    https://github.com/DrJohnT/DeployCube
 
    .NOTES
    Written by (c) Dr. John Tunnicliffe, 2019 https://github.com/DrJohnT/DeployCube
    This PowerShell script is released under the MIT license http://www.opensource.org/licenses/MIT
#>

    [OutputType([string])]
    [CmdletBinding()]
    param
    (
        [String] [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $SourceSqlServer,

        [String] [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $SourceSqlDatabase,

        [String] [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        $ExistingConnectionString
    )

    $ConnBuilder = New-Object System.Data.OleDb.OleDbConnectionStringBuilder($ExistingConnectionString);
    $ConnBuilder["Data Source"] = $SourceSqlServer;
    $ConnBuilder["Initial Catalog"] = $SourceSqlDatabase;
    return $ConnBuilder.ConnectionString;
}