Private/AzMySQL.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

function Get-DBConnArgs {
    [OutputType([string[]])]
    param (
        [string]$username,
        [string]$hostname,
        [string]$password
    )
    $arguments = @(
        "--enable-cleartext-plugin",
        "--user=$username@$hostname",
        "--password=$password",
        "--host=$hostname"
    )
    return $arguments
}

function Connect-DB {
    [OutputType([bool])]
    param ([string[]]$arguments)
    $success = $Null

    try {
        $nl = [Environment]::NewLine
        Write-Output "show databases; $nl exit $nl" | Invoke-Process -Command 'mysql' -Arguments $arguments
        $success = $true
    }
    catch {
        Write-Error "Failed to connect to database. $_"
        $success = $false
    }
    if ($Null -eq $success) {
        Throw "Connection null, not handled correctly."
    }
    Write-Information "Database connection success."
    return $success
}