|
| 1 | +# ---------------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright Microsoft Corporation |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +function Test-AzMySqlFlexibleServerConnect { |
| 16 | + [OutputType([System.String])] |
| 17 | + [CmdletBinding(DefaultParameterSetName='Test', PositionalBinding=$false)] |
| 18 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Description('Test out the connection to the database server')] |
| 19 | + param( |
| 20 | + [Parameter(ParameterSetName='Test', Mandatory, HelpMessage = 'The name of the server to connect.')] |
| 21 | + [Parameter(ParameterSetName='TestAndQuery', Mandatory, HelpMessage = 'The name of the server to connect.')] |
| 22 | + [Alias('ServerName')] |
| 23 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')] |
| 24 | + [System.String] |
| 25 | + ${Name}, |
| 26 | + |
| 27 | + [Parameter(ParameterSetName='Test', Mandatory, HelpMessage = 'The name of the resource group that contains the resource, You can obtain this value from the Azure Resource Manager API or the portal.')] |
| 28 | + [Parameter(ParameterSetName='TestAndQuery', Mandatory, HelpMessage = 'The name of the resource group that contains the resource, You can obtain this value from the Azure Resource Manager API or the portal.')] |
| 29 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')] |
| 30 | + [System.String] |
| 31 | + ${ResourceGroupName}, |
| 32 | + |
| 33 | + [Parameter(HelpMessage = 'The database name to connect.')] |
| 34 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')] |
| 35 | + [System.String] |
| 36 | + ${DatabaseName}, |
| 37 | + |
| 38 | + [Parameter(ParameterSetName='TestViaIdentityAndQuery', Mandatory, HelpMessage = 'The query for the database to test')] |
| 39 | + [Parameter(ParameterSetName='TestAndQuery', Mandatory, HelpMessage = 'The query for the database to test')] |
| 40 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')] |
| 41 | + [System.String] |
| 42 | + ${QueryText}, |
| 43 | + |
| 44 | + [Parameter(ParameterSetName='TestViaIdentity', Mandatory, ValueFromPipeline, HelpMessage = 'The server to connect.')] |
| 45 | + [Parameter(ParameterSetName='TestViaIdentityAndQuery', Mandatory, ValueFromPipeline, HelpMessage = 'The server to connect.')] |
| 46 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')] |
| 47 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Models.IMySqlIdentity] |
| 48 | + ${InputObject}, |
| 49 | + |
| 50 | + [Parameter(HelpMessage = 'Administrator username for the server. Once set, it cannot be changed.')] |
| 51 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')] |
| 52 | + [System.String] |
| 53 | + ${AdministratorUserName}, |
| 54 | + |
| 55 | + [Parameter(Mandatory, HelpMessage = 'The password of the administrator. Minimum 8 characters and maximum 128 characters. Password must contain characters from three of the following categories: English uppercase letters, English lowercase letters, numbers, and non-alphanumeric characters.')] |
| 56 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')] |
| 57 | + [System.Security.SecureString] |
| 58 | + [ValidateNotNullOrEmpty()] |
| 59 | + ${AdministratorLoginPassword}, |
| 60 | + |
| 61 | + [Parameter(HelpMessage = 'The credentials, account, tenant, and subscription used for communication with Azure.')] |
| 62 | + [Alias('AzureRMContext', 'AzureCredential')] |
| 63 | + [ValidateNotNull()] |
| 64 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Azure')] |
| 65 | + [System.Management.Automation.PSObject] |
| 66 | + ${DefaultProfile}, |
| 67 | + |
| 68 | + [Parameter(DontShow, HelpMessage = 'Wait for .NET debugger to attach.')] |
| 69 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 70 | + [System.Management.Automation.SwitchParameter] |
| 71 | + ${Break}, |
| 72 | + |
| 73 | + [Parameter(DontShow)] |
| 74 | + [ValidateNotNull()] |
| 75 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 76 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.SendAsyncStep[]] |
| 77 | + # SendAsync Pipeline Steps to be appended to the front of the pipeline. |
| 78 | + ${HttpPipelineAppend}, |
| 79 | + |
| 80 | + [Parameter(DontShow)] |
| 81 | + [ValidateNotNull()] |
| 82 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 83 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.SendAsyncStep[]] |
| 84 | + # SendAsync Pipeline Steps to be prepended to the front of the pipeline. |
| 85 | + ${HttpPipelinePrepend}, |
| 86 | + |
| 87 | + [Parameter(DontShow)] |
| 88 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 89 | + [System.Uri] |
| 90 | + # The URI for the proxy server to use. |
| 91 | + ${Proxy}, |
| 92 | + |
| 93 | + [Parameter(DontShow)] |
| 94 | + [ValidateNotNull()] |
| 95 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 96 | + [System.Management.Automation.PSCredential] |
| 97 | + # Credentials for a proxy server to use for the remote call. |
| 98 | + ${ProxyCredential}, |
| 99 | + |
| 100 | + [Parameter(DontShow)] |
| 101 | + [Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] |
| 102 | + [System.Management.Automation.SwitchParameter] |
| 103 | + # Use the default credentials for the proxy. |
| 104 | + ${ProxyUseDefaultCredentials} |
| 105 | + ) |
| 106 | + |
| 107 | + process { |
| 108 | + if (!(Get-Module -ListAvailable -Name SimplySQL)){ |
| 109 | + Write-Error "This cmdlet requires SimplySQL module. Please install the module first by running Install-Module -Name SimplySQL." |
| 110 | + exit |
| 111 | + } |
| 112 | + Import-Module SimplySQL |
| 113 | + |
| 114 | + $Query = [string]::Empty |
| 115 | + if ($PSBoundParameters.ContainsKey('QueryText')) { |
| 116 | + $Query = $PSBoundParameters.QueryText |
| 117 | + $null = $PSBoundParameters.Remove('QueryText') |
| 118 | + } |
| 119 | + |
| 120 | + $DatabaseName = [string]::Empty |
| 121 | + if ($PSBoundParameters.ContainsKey('DatabaseName')) { |
| 122 | + $DatabaseName = $PSBoundParameters.DatabaseName |
| 123 | + $null = $PSBoundParameters.Remove('DatabaseName') |
| 124 | + } |
| 125 | + |
| 126 | + $Password = $PSBoundParameters['AdministratorLoginPassword'] |
| 127 | + $null = $PSBoundParameters.Remove('AdministratorLoginPassword') |
| 128 | + |
| 129 | + $AdministratorUserName = [string]::Empty |
| 130 | + if ($PSBoundParameters.ContainsKey('AdministratorUserName')) { |
| 131 | + $AdministratorUserName = $PSBoundParameters.AdministratorUserName |
| 132 | + $null = $PSBoundParameters.Remove('AdministratorUserName') |
| 133 | + } |
| 134 | + |
| 135 | + $Server = Az.MySql\Get-AzMySqlFlexibleServer @PSBoundParameters |
| 136 | + $HostAddr = $Server.FullyQualifiedDomainName |
| 137 | + |
| 138 | + if ([string]::IsNullOrEmpty($AdministratorUserName)) { |
| 139 | + $AdministratorUserName = $Server.AdministratorLogin |
| 140 | + } |
| 141 | + |
| 142 | + try { |
| 143 | + if ([string]::IsNullOrEmpty($DatabaseName)){ |
| 144 | + Open-MySqlConnection -Database $DatabaseName -Server $HostAddr -UserName $AdministratorUserName -Password $Password -SSLMode Required -WarningAction 'silentlycontinue' |
| 145 | + } |
| 146 | + else { |
| 147 | + Open-MySqlConnection -Server $HostAddr -UserName $AdministratorUserName -Password $Password -SSLMode Required -WarningAction 'silentlycontinue' |
| 148 | + } |
| 149 | + } catch { |
| 150 | + Write-Host $_.Exception.GetType().FullName |
| 151 | + Write-Host $_.Exception.Message |
| 152 | + exit |
| 153 | + } |
| 154 | + |
| 155 | + if (![string]::IsNullOrEmpty($Query)) { |
| 156 | + Invoke-SqlQuery -Query $Query -WarningAction 'silentlycontinue' |
| 157 | + } |
| 158 | + else { |
| 159 | + $Msg = "The connection testing to {0} was successful!" -f $HostAddr |
| 160 | + Write-Host $Msg |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
0 commit comments