Permalink
Browse files

Suggested changes to messaging

  • Loading branch information...
nvarscar committed Dec 6, 2018
1 parent 99da9db commit 2db5ff186223918d13099b5e9476f6ecf0594864
Showing with 42 additions and 26 deletions.
  1. +15 −10 functions/Update-DbaInstance.ps1
  2. +5 −5 internal/functions/Get-SqlServerUpdate.ps1
  3. +22 −11 tests/Update-DbaInstance.Tests.ps1
@@ -243,9 +243,9 @@ function Update-DbaInstance {
}
# defining how to process the final results
$outputHandler = {
$_ | Select-DefaultView -Property ComputerName, MajorVersion, TargetLevel, KB, Successful, Restarted, InstanceName, Installer, Message
$_ | Select-DefaultView -Property ComputerName, MajorVersion, TargetLevel, KB, Successful, Restarted, InstanceName, Installer, Notes
if ($_.Successful -eq $false) {
Write-Message -Level Warning -Message $_.$message
Write-Message -Level Warning -Message "Update failed: $($_.Notes -join ' | ')"
}
}
}
@@ -256,6 +256,11 @@ function Update-DbaInstance {
$resolvedComputers = @()
foreach ($computer in $ComputerName) {
$null = Test-ElevationRequirement -ComputerName $computer -Continue
if (!$computer.IsLocalHost) {
if (!$Credential) {
Write-Message -Level Warning -Message "Explicit credentials are required when running agains remote hosts. Make sure to define the -Credential parameter"
}
}
if ($resolvedComputer = Resolve-DbaNetworkName -ComputerName $computer.ComputerName) {
$resolvedComputers += $resolvedComputer.FullComputerName
}
@@ -297,7 +302,7 @@ function Update-DbaInstance {
if ($upgradeDetails.Successful -contains $false) {
#Exit the actions loop altogether - upgrade cannot be performed
$upgradeDetails
Stop-Function -Message "Update cannot be applied to $resolvedName | $($upgradeDetails.Message)" -Continue -ContinueLabel computers
Stop-Function -Message "Update cannot be applied to $resolvedName | $($upgradeDetails.Notes)" -Continue -ContinueLabel computers
} else {
# update components to mirror the updated version - will be used for multi-step upgrades
foreach ($component in $components) {
@@ -354,8 +359,8 @@ function Update-DbaInstance {
Write-Message -Level Verbose -Message "Extracting $($currentAction.Installer) to $spExtractPath" -FunctionName Update-DbaInstance
$extractResult = Invoke-Program @execParams -Path $currentAction.Installer -ArgumentList "/x`:`"$spExtractPath`" /quiet" -EnableException $true
if (-not $extractResult.Successful) {
$output.Message = "Extraction failed with exit code $($extractResult.ExitCode)"
Stop-Function -Message $output.Message -FunctionName Update-DbaInstance
$output.Notes = "Extraction failed with exit code $($extractResult.ExitCode)"
Stop-Function -Message $output.Notes -FunctionName Update-DbaInstance
return $output
}
# Install the patch
@@ -370,14 +375,14 @@ function Update-DbaInstance {
if ($updateResult.Successful) {
$output.Successful = $true
} else {
$output.Message = "Update failed with exit code $($updateResult.ExitCode)"
Stop-Function -Message $output.Message -FunctionName Update-DbaInstance
$output.Notes = "Update failed with exit code $($updateResult.ExitCode)"
Stop-Function -Message $output.Notes -FunctionName Update-DbaInstance
return $output
}
$output.Log = $updateResult.stdout
} catch {
Stop-Function -Message "Upgrade failed" -ErrorRecord $_ -FunctionName Update-DbaInstance
$output.Message = $_.Exception.Message
$output.Notes = $_.Exception.Notes
return $output
} finally {
## Cleanup temp
@@ -392,7 +397,7 @@ function Update-DbaInstance {
} catch {
$message = "Failed to cleanup temp folder on computer $($computer)`: $($_.Exception.Message)"
Write-Message -Level Verbose -Message $message -FunctionName Update-DbaInstance
$output.Message += $message
$output.Notes += $message
}
}
}
@@ -408,7 +413,7 @@ function Update-DbaInstance {
return $output
}
} else {
$output.Message = "Restart is required for computer $($computer) to finish the installation of SQL$($currentAction.MajorVersion)$($currentAction.TargetLevel)"
$output.Notes = "Restart is required for computer $($computer) to finish the installation of SQL$($currentAction.MajorVersion)$($currentAction.TargetLevel)"
}
$output
Write-Progress -Activity $activity -Completed
@@ -80,7 +80,7 @@ function Get-SqlServerUpdate {
InstanceName = $InstanceName
Installer = $null
ExtractPath = $null
Message = $null
Notes = $null
ExitCode = $null
Log = $null
}
@@ -149,9 +149,9 @@ function Get-SqlServerUpdate {
Write-Message -Level Verbose -Message "Upgrading SQL$($targetKB.NameLevel) to $targetLevel (KB$($targetKBLevel))"
$kbLookupParams.KB = $targetKBLevel
} else {
$output.Message = "Could not find a KB$KB reference for $currentMajorVersion SP $ServicePack CU $CumulativeUpdate"
$output.Notes = "Could not find a KB$KB reference for $currentMajorVersion SP $ServicePack CU $CumulativeUpdate"
$output
Stop-Function -Message $output.Message -Continue
Stop-Function -Message $output.Notes -Continue
}
# Compare versions - whether to proceed with the installation
@@ -178,9 +178,9 @@ function Get-SqlServerUpdate {
$installer = Find-SqlServerUpdate @kbLookupParams
if (!$installer) {
$output.Message = "Could not find installer for the $currentMajorVersion update KB$($kbLookupParams.KB)"
$output.Notes = "Could not find installer for the $currentMajorVersion update KB$($kbLookupParams.KB)"
$output
Stop-Function -Message $output.Message -Continue
Stop-Function -Message $output.Notes -Continue
}
$output.Installer = $installer.FullName
$output.Successful = $true
@@ -119,7 +119,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
$result.Successful | Should -Be $true
$result.Restarted | Should -Be $true
$result.Installer | Should -Be (Join-Path $exeDir 'SQLServer2017-KB4464082-x64-ENU.exe')
$result.Message | Should -BeNullOrEmpty
$result.Notes | Should -BeNullOrEmpty
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
}
It "Should mock-upgrade SQL2008\LAB2 to latest SP" {
@@ -140,7 +140,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
$result.Restarted | Should -Be $true
$result.InstanceName | Should -Be LAB2
$result.Installer | Should -Be (Join-Path $exeDir 'SQLServer2008SP4-KB2979596-x64-ENU.exe')
$result.Message | Should -BeNullOrEmpty
$result.Notes | Should -BeNullOrEmpty
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
}
It "Should mock-upgrade two versions to latest SPs" {
@@ -164,7 +164,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
$result.Successful | Should -Be $true
$result.Restarted | Should -Be $true
$result.Installer | Should -Be (Join-Path $exeDir 'SQLServer2008SP4-KB2979596-x64-ENU.exe')
$result.Message | Should -BeNullOrEmpty
$result.Notes | Should -BeNullOrEmpty
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
#2012SP4
@@ -176,7 +176,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
$result.Successful | Should -Be $true
$result.Restarted | Should -Be $true
$result.Installer | Should -Be (Join-Path $exeDir 'SQLServer2012-KB4018073-x64-ENU.exe')
$result.Message | Should -BeNullOrEmpty
$result.Notes | Should -BeNullOrEmpty
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
}
}
@@ -246,7 +246,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
$result.Successful | Should -Be $true
$result.Restarted | Should -Be $true
$result.Installer | Should -Be (Join-Path $exeDir 'SQLServer2008SP3-KB2546951-x64-ENU.exe')
$result.Message | Should -BeNullOrEmpty
$result.Notes | Should -BeNullOrEmpty
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
}
It "Should mock-upgrade SQL2016 to SP1CU4 (KB3182545 + KB4024305) " {
@@ -265,7 +265,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
$result.Successful | Should -Be $true
$result.Restarted | Should -Be $true
$result.Installer | Should -Be (Join-Path $exeDir 'SQLServer2016-KB4024305-x64-ENU.exe')
$result.Message | Should -BeNullOrEmpty
$result.Notes | Should -BeNullOrEmpty
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
}
It "Should mock-upgrade both versions to different KBs" {
@@ -287,7 +287,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
$result.Successful | Should -Be $true
$result.Restarted | Should -Be $true
$result.Installer | Should -Be (Join-Path $exeDir 'SQLServer2016-KB4040714-x64.exe')
$result.Message | Should -BeNullOrEmpty
$result.Notes | Should -BeNullOrEmpty
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
#2008SP3
@@ -298,7 +298,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
$result.Successful | Should -Be $true
$result.Restarted | Should -Be $true
$result.Installer | Should -Be (Join-Path $exeDir 'SQLServer2008SP3-KB2546951-x64-ENU.exe')
$result.Message | Should -BeNullOrEmpty
$result.Notes | Should -BeNullOrEmpty
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
#2008SP3CU7
@@ -309,7 +309,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
$result.Successful | Should -Be $true
$result.Restarted | Should -Be $true
$result.Installer | Should -Be (Join-Path $exeDir 'SQLServer2008-KB2738350-x64-ENU.exe')
$result.Message | Should -BeNullOrEmpty
$result.Notes | Should -BeNullOrEmpty
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
}
}
@@ -357,7 +357,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
$result.Restarted | Should -Be $true
$result.InstanceName | Should -Be LAB
$result.Installer | Should -Be 'c:\mocked\filename.exe'
$result.Message | Should -BeNullOrEmpty
$result.Notes | Should -BeNullOrEmpty
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
}
}
@@ -544,7 +544,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
$result.Successful | Should -Be $true
$result.Restarted | Should -Be $true
$result.Installer | Should -Be 'c:\mocked\filename.exe'
$result.Message | Should -BeNullOrEmpty
$result.Notes | Should -BeNullOrEmpty
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
}
}
@@ -614,6 +614,17 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
#override default mock
Mock -CommandName Invoke-Program -MockWith { [pscustomobject]@{ Successful = $false; ExitCode = 12345 } } -ModuleName dbatools
{ Update-DbaInstance -Version 2008SP3 -EnableException -Path 'mocked' -Confirm:$false } | Should throw 'failed with exit code 12345'
$result = Update-DbaInstance -Version 2008SP3 -Path 'mocked' -Confirm:$false -WarningVariable warVar 3>$null
$result | Should -Not -BeNullOrEmpty
$result.MajorVersion | Should -Be 2008
$result.TargetLevel | Should -Be SP3
$result.KB | Should -Be 2546951
$result.Successful | Should -Be $false
$result.Restarted | Should -Be $false
$result.Installer | Should -Be 'c:\mocked\filename.exe'
$result.Notes | Should -BeLike '*failed with exit code 12345'
$result.ExtractPath | Should -BeLike '*\dbatools_KB*Extract'
$warVar | Should -BeLike '*failed with exit code 12345'
#revert default mock
Mock -CommandName Invoke-Program -MockWith { [pscustomobject]@{ Successful = $true } } -ModuleName dbatools
}

0 comments on commit 2db5ff1

Please sign in to comment.