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