-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added cmdlet Test-AzMySqlFlexibleServerConnect
- Loading branch information
Showing
15 changed files
with
1,091 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
# ---------------------------------------------------------------------------------- | ||
# | ||
# Copyright Microsoft Corporation | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ---------------------------------------------------------------------------------- | ||
|
||
function Test-AzMySqlFlexibleServerConnect { | ||
[OutputType([System.String])] | ||
[CmdletBinding(DefaultParameterSetName='Test', PositionalBinding=$false)] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Description('Test out the connection to the database server')] | ||
param( | ||
[Parameter(ParameterSetName='Test', Mandatory, HelpMessage = 'The name of the server to connect.')] | ||
[Parameter(ParameterSetName='TestAndQuery', Mandatory, HelpMessage = 'The name of the server to connect.')] | ||
[Alias('ServerName')] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')] | ||
[System.String] | ||
${Name}, | ||
|
||
[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.')] | ||
[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.')] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')] | ||
[System.String] | ||
${ResourceGroupName}, | ||
|
||
[Parameter(HelpMessage = 'The database name to connect.')] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')] | ||
[System.String] | ||
${DatabaseName}, | ||
|
||
[Parameter(ParameterSetName='TestViaIdentityAndQuery', Mandatory, HelpMessage = 'The query for the database to test')] | ||
[Parameter(ParameterSetName='TestAndQuery', Mandatory, HelpMessage = 'The query for the database to test')] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Path')] | ||
[System.String] | ||
${QueryText}, | ||
|
||
[Parameter(ParameterSetName='TestViaIdentity', Mandatory, ValueFromPipeline, HelpMessage = 'The server to connect.')] | ||
[Parameter(ParameterSetName='TestViaIdentityAndQuery', Mandatory, ValueFromPipeline, HelpMessage = 'The server to connect.')] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Models.IMySqlIdentity] | ||
${InputObject}, | ||
|
||
[Parameter(HelpMessage = 'Administrator username for the server. Once set, it cannot be changed.')] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')] | ||
[System.String] | ||
${AdministratorUserName}, | ||
|
||
[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.')] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Body')] | ||
[System.Security.SecureString] | ||
[ValidateNotNullOrEmpty()] | ||
${AdministratorLoginPassword}, | ||
|
||
[Parameter(HelpMessage = 'The credentials, account, tenant, and subscription used for communication with Azure.')] | ||
[Alias('AzureRMContext', 'AzureCredential')] | ||
[ValidateNotNull()] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Azure')] | ||
[System.Management.Automation.PSObject] | ||
${DefaultProfile}, | ||
|
||
[Parameter(DontShow, HelpMessage = 'Wait for .NET debugger to attach.')] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] | ||
[System.Management.Automation.SwitchParameter] | ||
${Break}, | ||
|
||
[Parameter(DontShow)] | ||
[ValidateNotNull()] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.SendAsyncStep[]] | ||
# SendAsync Pipeline Steps to be appended to the front of the pipeline. | ||
${HttpPipelineAppend}, | ||
|
||
[Parameter(DontShow)] | ||
[ValidateNotNull()] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Runtime.SendAsyncStep[]] | ||
# SendAsync Pipeline Steps to be prepended to the front of the pipeline. | ||
${HttpPipelinePrepend}, | ||
|
||
[Parameter(DontShow)] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] | ||
[System.Uri] | ||
# The URI for the proxy server to use. | ||
${Proxy}, | ||
|
||
[Parameter(DontShow)] | ||
[ValidateNotNull()] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] | ||
[System.Management.Automation.PSCredential] | ||
# Credentials for a proxy server to use for the remote call. | ||
${ProxyCredential}, | ||
|
||
[Parameter(DontShow)] | ||
[Microsoft.Azure.PowerShell.Cmdlets.MySql.Category('Runtime')] | ||
[System.Management.Automation.SwitchParameter] | ||
# Use the default credentials for the proxy. | ||
${ProxyUseDefaultCredentials} | ||
) | ||
|
||
process { | ||
if (!(Get-Module -ListAvailable -Name SimplySQL)){ | ||
Write-Error "This cmdlet requires SimplySQL module. Please install the module first by running Install-Module -Name SimplySQL." | ||
exit | ||
} | ||
Import-Module SimplySQL | ||
|
||
$Query = [string]::Empty | ||
if ($PSBoundParameters.ContainsKey('QueryText')) { | ||
$Query = $PSBoundParameters.QueryText | ||
$null = $PSBoundParameters.Remove('QueryText') | ||
} | ||
|
||
$DatabaseName = [string]::Empty | ||
if ($PSBoundParameters.ContainsKey('DatabaseName')) { | ||
$DatabaseName = $PSBoundParameters.DatabaseName | ||
$null = $PSBoundParameters.Remove('DatabaseName') | ||
} | ||
|
||
$Password = $PSBoundParameters['AdministratorLoginPassword'] | ||
$null = $PSBoundParameters.Remove('AdministratorLoginPassword') | ||
|
||
$AdministratorUserName = [string]::Empty | ||
if ($PSBoundParameters.ContainsKey('AdministratorUserName')) { | ||
$AdministratorUserName = $PSBoundParameters.AdministratorUserName | ||
$null = $PSBoundParameters.Remove('AdministratorUserName') | ||
} | ||
|
||
$Server = Az.MySql\Get-AzMySqlFlexibleServer @PSBoundParameters | ||
$HostAddr = $Server.FullyQualifiedDomainName | ||
|
||
if ([string]::IsNullOrEmpty($AdministratorUserName)) { | ||
$AdministratorUserName = $Server.AdministratorLogin | ||
} | ||
|
||
try { | ||
if ([string]::IsNullOrEmpty($DatabaseName)){ | ||
Open-MySqlConnection -Database $DatabaseName -Server $HostAddr -UserName $AdministratorUserName -Password $Password -SSLMode Required -WarningAction 'silentlycontinue' | ||
} | ||
else { | ||
Open-MySqlConnection -Server $HostAddr -UserName $AdministratorUserName -Password $Password -SSLMode Required -WarningAction 'silentlycontinue' | ||
} | ||
} catch { | ||
Write-Host $_.Exception.GetType().FullName | ||
Write-Host $_.Exception.Message | ||
exit | ||
} | ||
|
||
if (![string]::IsNullOrEmpty($Query)) { | ||
Invoke-SqlQuery -Query $Query -WarningAction 'silentlycontinue' | ||
} | ||
else { | ||
$Msg = "The connection testing to {0} was successful!" -f $HostAddr | ||
Write-Host $Msg | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
### Example 1: Test connection by name | ||
```powershell | ||
PS C:\> Get-AzMySqlFlexibleServerConnect -ResourceGroupName PowershellMySqlTest -Name mysql-test -AdministratorLoginPassword $password | ||
The connection testing to mysql-test.database.azure.com was successful! | ||
``` | ||
|
||
Test connection by the resource group and the server name | ||
|
||
### Example 2: Test connection by identity | ||
```powershell | ||
PS C:\> Get-AzMySqlFlexibleServer -ResourceGroupName PowershellMySqlTest -ServerName mysql-test | Get-AzMySqlFlexibleServerConnect -AdministratorLoginPassword $password | ||
The connection testing to mysql-test.database.azure.com was successful! | ||
``` | ||
|
||
Test connection by the identity | ||
|
||
### Example 3: Test query by name | ||
```powershell | ||
PS C:\> Get-AzMySqlFlexibleServerConnect -ResourceGroupName PowershellMySqlTest -Name mysql-test -AdministratorLoginPassword $password -Query "SELECT * FROM test" | ||
col | ||
----- | ||
1 | ||
2 | ||
3 | ||
``` | ||
|
||
Test a query by the resource group and the server name | ||
|
||
### Example 4: Test connection by identity | ||
```powershell | ||
PS C:\> Get-AzMySqlFlexibleServer -ResourceGroupName PowershellMySqlTest -ServerName mysql-test | Get-AzMySqlFlexibleServerConnect -Query "SELECT * FROM test" -AdministratorLoginPassword $password | ||
col | ||
----- | ||
1 | ||
2 | ||
3 | ||
``` | ||
|
||
Test a query by the identity | ||
|
Oops, something went wrong.