@@ -100,22 +100,27 @@ You can save this file as `/opt/mssql/bin/runassessment.ps1`.
100100
101101``` powershell
102102[CmdletBinding()] param ()
103+
103104$Error.Clear()
105+
104106# Create output directory if not exists
107+
105108$outDir = '/var/opt/mssql/log/assessments'
106109if (-not ( Test-Path $outDir )) { mkdir $outDir }
107110$outPath = Join-Path $outDir 'assessment-latest'
111+
108112$errorPath = Join-Path $outDir 'assessment-latest-errors'
109- if ( Test-Path $errorPath ) { remove-item $errorPath }
113+ if( Test-Path $errorPath ) { remove-item $errorPath }
114+
110115function ConvertTo-LogOutput {
111116 [CmdletBinding()]
112117 param (
113- [Parameter(ValueFromPipeline = $true)]
118+ [Parameter(ValueFromPipeline= $true)]
114119 $input
115120 )
116121 process {
117- switch ($input) {
118- { $_ -is [System.Management.Automation.WarningRecord] } {
122+ switch($input){
123+ { $_ -is [System.Management.Automation.WarningRecord] }{
119124 $result = @{
120125 'TimeStamp' = $(Get-Date).ToString("O");
121126 'Warning' = $_.Message
@@ -152,31 +157,31 @@ function Get-TargetsRecursive {
152157
153158 [CmdletBinding()]
154159 Param (
155- [Parameter(ValueFromPipeline = $true)]
160+ [Parameter(ValueFromPipeline= $true)]
156161 [Microsoft.SqlServer.Management.Smo.Server] $server
157162 )
158163
159164 $server
160165 $server.Databases
161166}
162167
163- function Get-ConfSetting {
168+ function Get-ConfSetting{
164169 [CmdletBinding()]
165170 param (
166171 $confFile,
167172 $section,
168173 $name,
169- $defaultValue = $null
174+ $defaultValue= $null
170175 )
171176
172177 $inSection = $false
173178
174- switch -regex -file $confFile {
175- "^\s*\[\s*(.+?)\s*\]" {
179+ switch -regex -file $confFile{
180+ "^\s*\[\s*(.+?)\s*\]"{
176181 $inSection = $matches[1] -eq $section
177182 }
178- "^\s*$($name)\s*=\s*(.+?)\s*$" {
179- if ($inSection) {
183+ "^\s*$($name)\s*=\s*(.+?)\s*$"{
184+ if($inSection){
180185 return $matches[1]
181186 }
182187 }
@@ -189,25 +194,28 @@ try {
189194
190195 Write-Verbose "Acquiring credentials"
191196
192- $login, $pwd = Get-Content '/var/opt/mssql/secrets/assessment' -Encoding UTF8NoBOM -TotalCount 2
197+ $login, $pwd = Get-Content '/var/opt/mssql/secrets/assessment' -Encoding UTF8NoBOM -TotalCount 2
193198 $securePassword = ConvertTo-SecureString $pwd -AsPlainText -Force
194- $credential = New-Object System.Management.Automation.PSCredential ($login, $securePassword)
195-
199+ $credential = New-Object System.Management.Automation.PSCredential ($login, $securePassword)
200+ $securePassword.MakeReadOnly()
201+
196202 Write-Verbose "Acquired credentials"
197203
198204 $serverInstance = '.'
199205
200- if (Test-Path /var/opt/mssql/mssql.conf) {
206+ if(Test-Path /var/opt/mssql/mssql.conf) {
201207 $port = Get-ConfSetting /var/opt/mssql/mssql.conf network tcpport
202208
203- if (-not [string]::IsNullOrWhiteSpace($port)) {
209+
210+ if(-not [string]::IsNullOrWhiteSpace($port)) {
204211 Write-Verbose "Using port $($port)"
205212 $serverInstance = "$($serverInstance),$($port)"
206213 }
207214 }
208215
216+ # IMPORTANT: If the script is run in trusted environments and there is a prelogin handshake error, add -TrustServerCertificate flag in the commands for $serverName, $hostName and Get-SqlInstance lines below.
209217 $serverName = (Invoke-SqlCmd -ServerInstance $serverInstance -Credential $credential -Query "SELECT @@SERVERNAME")[0]
210- $hostName = (Invoke-SqlCmd -ServerInstance $serverInstance -Credential $credential -Query "SELECT HOST_NAME()")[0]
218+ $hostName = (Invoke-SqlCmd -ServerInstance $serverInstance -Credential $credential -Query "SELECT HOST_NAME()")[0]
211219
212220 # Invoke assessment and store results.
213221 # Replace 'ConvertTo-Json' with 'ConvertTo-Csv' to change output format.
@@ -216,7 +224,7 @@ try {
216224
217225 Get-SqlInstance -ServerInstance $serverInstance -Credential $credential -ErrorAction Stop
218226 | Get-TargetsRecursive
219- | % { Write-Verbose "Invoke assessment on $($_.Urn)"; $_ }
227+ | %{ Write-Verbose "Invoke assessment on $($_.Urn)"; $_ }
220228 | Invoke-SqlAssessment 3>&1
221229 | ConvertTo-LogOutput
222230 | ConvertTo-Json -AsArray
@@ -234,6 +242,8 @@ finally {
234242 }
235243}
236244```
245+ > [ !NOTE]
246+ > In the above script, when it is run in trusted environments and there is a prelogin handshake error, add -TrustServerCertificate flag in the commands for $serverName, $hostName and Get-SqlInstance lines in the above code. .
237247
238248### Run the assessment
239249
0 commit comments