Skip to content

Commit 3222864

Browse files
author
msJinLei
committed
Added cmdlet Test-AzMySqlFlexibleServerConnect
1 parent 2b73fd2 commit 3222864

15 files changed

+1091
-30
lines changed

src/MySql/Az.MySql.psd1

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: Microsoft Corporation
55
#
6-
# Generated on: 1/14/2021
6+
# Generated on: 2/2/2021
77
#
88

99
@{
@@ -45,7 +45,7 @@ PowerShellVersion = '5.1'
4545
DotNetFrameworkVersion = '4.7.2'
4646

4747
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
48-
# CLRVersion = ''
48+
# ClrVersion = ''
4949

5050
# Processor architecture (None, X86, Amd64) required by this module
5151
# ProcessorArchitecture = ''
@@ -89,8 +89,9 @@ FunctionsToExport = 'Get-AzMySqlConfiguration', 'Get-AzMySqlConnectionString',
8989
'Remove-AzMySqlVirtualNetworkRule', 'Restart-AzMySqlFlexibleServer',
9090
'Restart-AzMySqlServer', 'Restore-AzMySqlFlexibleServer',
9191
'Restore-AzMySqlServer', 'Start-AzMySqlFlexibleServer',
92-
'Stop-AzMySqlFlexibleServer', 'Update-AzMySqlConfiguration',
93-
'Update-AzMySqlFirewallRule', 'Update-AzMySqlFlexibleServer',
92+
'Stop-AzMySqlFlexibleServer', 'Test-AzMySqlFlexibleServerConnect',
93+
'Update-AzMySqlConfiguration', 'Update-AzMySqlFirewallRule',
94+
'Update-AzMySqlFlexibleServer',
9495
'Update-AzMySqlFlexibleServerConfiguration',
9596
'Update-AzMySqlFlexibleServerDatabase',
9697
'Update-AzMySqlFlexibleServerFirewallRule', 'Update-AzMySqlServer',
@@ -132,9 +133,7 @@ PrivateData = @{
132133
# IconUri = ''
133134

134135
# ReleaseNotes of this module
135-
ReleaseNotes = '* Updated New-AzMySqlFlexibleServer cmdlet to provide easy resource group and network management within RDBMS flexible server management
136-
* Added new cmdlets Get-AzMySqlFlexibleServerLocationBasedCapability and Get-AzMySqlConnectionString
137-
* Added parameter MinimalTlsVersion'
136+
# ReleaseNotes = ''
138137

139138
# Prerelease string of this module
140139
# Prerelease = ''

src/MySql/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Added cmdlet `Test-AzMySqlFlexibleServerConnect`
2122

2223
## Version 0.4.0
2324
* Updated New-AzMySqlFlexibleServer cmdlet to provide easy resource group and network management within RDBMS flexible server management

src/MySql/custom/New-AzMySqlFlexibleServer.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ process {
191191
$Password = Get-GeneratePassword
192192
$PSBoundParameters.AdministratorLoginPassword = $Password | ConvertTo-SecureString -AsPlainText -Force
193193
}
194-
$PSBoundParameters.AdministratorLoginPassword = . "$PSScriptRoot/../utils/Unprotect-SecureString.ps1" $PSBoundParameters['AdministratorLoginPassword']
195194

196195
Import-Module -Name Az.Resources
197196
if(!$PSBoundParameters.ContainsKey('ResourceGroupName')) {
@@ -308,7 +307,7 @@ process {
308307
$Server.FirewallRuleName = $FirewallRuleName
309308
}
310309
$Server.DatabaseName = $DEFAULT_DB_NAME
311-
$Server.SecuredPassword = $PSBoundParameters.AdministratorLoginPassword | ConvertTo-SecureString -AsPlainText -Force
310+
$Server.SecuredPassword = $PSBoundParameters.AdministratorLoginPassword
312311

313312
return $Server
314313

@@ -526,7 +525,7 @@ function CreateFirewallRule($Parameters) {
526525
}
527526
return $FirewallRule.Name
528527
}
529-
elseif ($Parameters.ContainsKey('PublicAccess') -And $Parameters.PublicAccess.ToLower() -ne 'none') {
528+
elseif ($Parameters.ContainsKey('PublicAccess') -And $Parameters.PublicAccess.ToLower() -eq 'none') {
530529
Write-Host "No firewall rule was set"
531530
}
532531
}

src/MySql/custom/New-AzMySqlServer.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ function New-AzMySqlServer {
224224
$Parameter.Property.AdministratorLogin = $PSBoundParameters['AdministratorUserName']
225225
$null = $PSBoundParameters.Remove('AdministratorUserName')
226226

227-
$Parameter.Property.AdministratorLoginPassword = . "$PSScriptRoot/../utils/Unprotect-SecureString.ps1" $PSBoundParameters['AdministratorLoginPassword']
227+
$Parameter.Property.AdministratorLoginPassword = $PSBoundParameters['AdministratorLoginPassword']
228228
$null = $PSBoundParameters.Remove('AdministratorLoginPassword')
229229

230230
$PSBoundParameters.Add('Parameter', $Parameter)
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+

src/MySql/custom/Update-AzMySqlFlexibleServer.ps1

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,6 @@ function Update-AzMySqlFlexibleServer {
161161

162162
process {
163163
try {
164-
if ($PSBoundParameters.ContainsKey('AdministratorLoginPassword')) {
165-
$PSBoundParameters.AdministratorLoginPassword = . "$PSScriptRoot/../utils/Unprotect-SecureString.ps1" $PSBoundParameters['AdministratorLoginPassword']
166-
}
167-
168164
if ($PSBoundParameters.ContainsKey('StorageInMb')) {
169165
$PSBoundParameters.StorageProfileStorageMb = $PSBoundParameters['StorageInMb']
170166
$null = $PSBoundParameters.Remove('StorageInMb')

src/MySql/custom/Update-AzMySqlServer.ps1

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,6 @@ function Update-AzMySqlServer {
165165

166166
process {
167167
try {
168-
if ($PSBoundParameters.ContainsKey('AdministratorLoginPassword')) {
169-
$password = . "$PSScriptRoot/../utils/Unprotect-SecureString.ps1" $PSBoundParameters['AdministratorLoginPassword']
170-
$PSBoundParameters.Add('AdministratorLoginPassword', $password)
171-
$null = $PSBoundParameters.Remove('AdministratorLoginPassword')
172-
}
173-
174168
if ($PSBoundParameters.ContainsKey('StorageInMb')) {
175169
$PSBoundParameters.Add('StorageProfileStorageMb', $PSBoundParameters['StorageInMb'])
176170
$null = $PSBoundParameters.Remove('StorageInMb')
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
### Example 1: Test connection by name
2+
```powershell
3+
PS C:\> Get-AzMySqlFlexibleServerConnect -ResourceGroupName PowershellMySqlTest -Name mysql-test -AdministratorLoginPassword $password
4+
5+
The connection testing to mysql-test.database.azure.com was successful!
6+
```
7+
8+
Test connection by the resource group and the server name
9+
10+
### Example 2: Test connection by identity
11+
```powershell
12+
PS C:\> Get-AzMySqlFlexibleServer -ResourceGroupName PowershellMySqlTest -ServerName mysql-test | Get-AzMySqlFlexibleServerConnect -AdministratorLoginPassword $password
13+
14+
The connection testing to mysql-test.database.azure.com was successful!
15+
```
16+
17+
Test connection by the identity
18+
19+
### Example 3: Test query by name
20+
```powershell
21+
PS C:\> Get-AzMySqlFlexibleServerConnect -ResourceGroupName PowershellMySqlTest -Name mysql-test -AdministratorLoginPassword $password -Query "SELECT * FROM test"
22+
23+
col
24+
-----
25+
1
26+
2
27+
3
28+
```
29+
30+
Test a query by the resource group and the server name
31+
32+
### Example 4: Test connection by identity
33+
```powershell
34+
PS C:\> Get-AzMySqlFlexibleServer -ResourceGroupName PowershellMySqlTest -ServerName mysql-test | Get-AzMySqlFlexibleServerConnect -Query "SELECT * FROM test" -AdministratorLoginPassword $password
35+
36+
col
37+
-----
38+
1
39+
2
40+
3
41+
```
42+
43+
Test a query by the identity
44+

0 commit comments

Comments
 (0)