Skip to content

Commit

Permalink
Suggested changes to messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
nvarscar committed Dec 6, 2018
1 parent 99da9db commit 2db5ff1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
25 changes: 15 additions & 10 deletions functions/Update-DbaInstance.ps1
Expand Up @@ -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 ' | ')"
}
}
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
}
}
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions internal/functions/Get-SqlServerUpdate.ps1
Expand Up @@ -80,7 +80,7 @@ function Get-SqlServerUpdate {
InstanceName = $InstanceName
Installer = $null
ExtractPath = $null
Message = $null
Notes = $null
ExitCode = $null
Log = $null
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 22 additions & 11 deletions tests/Update-DbaInstance.Tests.ps1
Expand Up @@ -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" {
Expand All @@ -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" {
Expand All @@ -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
Expand All @@ -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'
}
}
Expand Down Expand Up @@ -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) " {
Expand All @@ -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" {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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'
}
}
Expand Down Expand Up @@ -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'
}
}
Expand Down Expand Up @@ -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'
}
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 2db5ff1

Please sign in to comment.