Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
619 changes: 311 additions & 308 deletions public/Install-DbaInstance.ps1

Large diffs are not rendered by default.

707 changes: 355 additions & 352 deletions public/Install-DbaMaintenanceSolution.ps1

Large diffs are not rendered by default.

336 changes: 165 additions & 171 deletions public/Install-DbaSqlPackage.ps1

Large diffs are not rendered by default.

277 changes: 140 additions & 137 deletions public/Invoke-DbaAdvancedInstall.ps1

Large diffs are not rendered by default.

225 changes: 114 additions & 111 deletions public/Invoke-DbaAdvancedUpdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,137 +125,140 @@ Function Invoke-DbaAdvancedUpdate {
$restartParams.WsmanAuthentication = $Authentication
}
try {
$restartNeeded = Test-PendingReboot -ComputerName $computer -Credential $Credential -NoPendingRename:$NoPendingRenameCheck
} catch {
$restartNeeded = $false
Stop-Function -Message "Failed to get reboot status from $computer" -ErrorRecord $_
}
if ($restartNeeded -and $Restart) {
# Restart the computer prior to doing anything
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Restarting computer $($computer) due to pending restart"
Write-Message -Level Verbose "Restarting computer $($computer) due to pending restart"
try {
$null = Restart-Computer @restartParams
$restarted = $true
$restartNeeded = Test-PendingReboot -ComputerName $computer -Credential $Credential -NoPendingRename:$NoPendingRenameCheck
} catch {
Stop-Function -Message "Failed to restart computer $($computer)" -ErrorRecord $_
}
}
Write-Message -Level Debug -Message "Processing $($computer) with $(($Actions | Measure-Object).Count) actions"
#foreach action passed to the script for this particular computer
foreach ($currentAction in $Action) {
$output = $currentAction
$output.Successful = $false
$output.Restarted = $restarted
## Start the installation sequence
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Launching installation of $($currentAction.TargetLevel) KB$($currentAction.KB) ($($currentAction.Installer)) for SQL$($currentAction.MajorVersion) ($($currentAction.Build))"
$execParams = @{
ComputerName = $computer
ErrorAction = 'Stop'
Authentication = $Authentication
}
if ($Credential) {
$execParams.Credential = $Credential
$restartNeeded = $false
Stop-Function -Message "Failed to get reboot status from $computer" -ErrorRecord $_
}

if (!$ExtractPath) {
# Find a temporary folder to extract to - the drive that has most free space
if ($restartNeeded -and $Restart) {
# Restart the computer prior to doing anything
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Restarting computer $($computer) due to pending restart"
Write-Message -Level Verbose "Restarting computer $($computer) due to pending restart"
try {
$chosenDrive = (Get-DbaDiskSpace -ComputerName $computer -Credential $Credential -EnableException:$true | Sort-Object -Property Free -Descending | Select-Object -First 1).Name
if (!$chosenDrive) {
# Fall back to the system drive
$chosenDrive = Invoke-Command2 -ComputerName $computer -Credential $Credential -ScriptBlock { $env:SystemDrive } -Raw -ErrorAction Stop
}
$null = Restart-Computer @restartParams
$restarted = $true
} catch {
$msg = "Failed to retrieve a disk drive to extract the update"
$output.Notes += $msg
Stop-Function -Message $msg -ErrorRecord $_
return $output
Stop-Function -Message "Failed to restart computer $($computer)" -ErrorRecord $_
}
} else {
$chosenDrive = $ExtractPath
}
$spExtractPath = $chosenDrive.TrimEnd('\') + "\dbatools_KB$($currentAction.KB)_Extract_$([guid]::NewGuid().Guid.Replace('-',''))"
$output.ExtractPath = $spExtractPath
try {
# Extract file
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Extracting $($currentAction.Installer) to $spExtractPath"
Write-Message -Level Verbose -Message "Extracting $($currentAction.Installer) to $spExtractPath"
$extractResult = Invoke-Program @execParams -Path $currentAction.Installer -ArgumentList @("/x`:`"$spExtractPath`"", "/quiet") -Fallback
if (-not $extractResult.Successful) {
$msg = "Extraction failed with exit code $($extractResult.ExitCode), try specifying a different location using -ExtractPath"
$output.Notes += $msg
Stop-Function -Message $msg
return $output
Write-Message -Level Debug -Message "Processing $($computer) with $(($Actions | Measure-Object).Count) actions"
#foreach action passed to the script for this particular computer
foreach ($currentAction in $Action) {
$output = $currentAction
$output.Successful = $false
$output.Restarted = $restarted
## Start the installation sequence
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Launching installation of $($currentAction.TargetLevel) KB$($currentAction.KB) ($($currentAction.Installer)) for SQL$($currentAction.MajorVersion) ($($currentAction.Build))"
$execParams = @{
ComputerName = $computer
ErrorAction = 'Stop'
Authentication = $Authentication
}
# Install the patch
if ($currentAction.InstanceName) {
$instanceClause = "/instancename=$($currentAction.InstanceName)"
} else {
$instanceClause = '/allinstances'
if ($Credential) {
$execParams.Credential = $Credential
}
if ($currentAction.Build -like "10.0.*") {
$programArgumentList = $ArgumentList + @('/quiet', $instanceClause)

if (!$ExtractPath) {
# Find a temporary folder to extract to - the drive that has most free space
try {
$chosenDrive = (Get-DbaDiskSpace -ComputerName $computer -Credential $Credential -EnableException:$true | Sort-Object -Property Free -Descending | Select-Object -First 1).Name
if (!$chosenDrive) {
# Fall back to the system drive
$chosenDrive = Invoke-Command2 -ComputerName $computer -Credential $Credential -ScriptBlock { $env:SystemDrive } -Raw -ErrorAction Stop
}
} catch {
$msg = "Failed to retrieve a disk drive to extract the update"
$output.Notes += $msg
Stop-Function -Message $msg -ErrorRecord $_
return $output
}
} else {
$programArgumentList = $ArgumentList + @('/quiet', $instanceClause, '/IAcceptSQLServerLicenseTerms')
$chosenDrive = $ExtractPath
}
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Now installing update SQL$($currentAction.MajorVersion)$($currentAction.TargetLevel) from $spExtractPath"
Write-Message -Level Verbose -Message "Starting installation from $spExtractPath"
$updateResult = Invoke-Program @execParams -Path "$spExtractPath\setup.exe" -ArgumentList $programArgumentList -WorkingDirectory $spExtractPath -Fallback
$output.ExitCode = $updateResult.ExitCode
if ($updateResult.Successful) {
$output.Successful = $true
} else {
$msg = "Update failed with exit code $($updateResult.ExitCode)"
$output.Notes += $msg
Stop-Function -Message $msg
$spExtractPath = $chosenDrive.TrimEnd('\') + "\dbatools_KB$($currentAction.KB)_Extract_$([guid]::NewGuid().Guid.Replace('-',''))"
$output.ExtractPath = $spExtractPath
try {
# Extract file
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Extracting $($currentAction.Installer) to $spExtractPath"
Write-Message -Level Verbose -Message "Extracting $($currentAction.Installer) to $spExtractPath"
$extractResult = Invoke-Program @execParams -Path $currentAction.Installer -ArgumentList @("/x`:`"$spExtractPath`"", "/quiet") -Fallback
if (-not $extractResult.Successful) {
$msg = "Extraction failed with exit code $($extractResult.ExitCode), try specifying a different location using -ExtractPath"
$output.Notes += $msg
Stop-Function -Message $msg
return $output
}
# Install the patch
if ($currentAction.InstanceName) {
$instanceClause = "/instancename=$($currentAction.InstanceName)"
} else {
$instanceClause = '/allinstances'
}
if ($currentAction.Build -like "10.0.*") {
$programArgumentList = $ArgumentList + @('/quiet', $instanceClause)
} else {
$programArgumentList = $ArgumentList + @('/quiet', $instanceClause, '/IAcceptSQLServerLicenseTerms')
}
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Now installing update SQL$($currentAction.MajorVersion)$($currentAction.TargetLevel) from $spExtractPath"
Write-Message -Level Verbose -Message "Starting installation from $spExtractPath"
$updateResult = Invoke-Program @execParams -Path "$spExtractPath\setup.exe" -ArgumentList $programArgumentList -WorkingDirectory $spExtractPath -Fallback
$output.ExitCode = $updateResult.ExitCode
if ($updateResult.Successful) {
$output.Successful = $true
} else {
$msg = "Update failed with exit code $($updateResult.ExitCode)"
$output.Notes += $msg
Stop-Function -Message $msg
return $output
}
$output.Log = $updateResult.stdout
} catch {
Stop-Function -Message "Upgrade failed" -ErrorRecord $_
$output.Notes += $_.Exception.Message
return $output
} finally {
## Cleanup temp
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Cleaning up extracted files from $spExtractPath"
try {
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Removing temporary files"
$null = Invoke-CommandWithFallBack @execParams -ScriptBlock {
if ($args[0] -like '*\dbatools_KB*_Extract*' -and (Test-Path $args[0])) {
Remove-Item -Recurse -Force -LiteralPath $args[0] -ErrorAction Stop
}
} -Raw -ArgumentList $spExtractPath
} catch {
$message = "Failed to cleanup temp folder on computer $($computer)`: $($_.Exception.Message)"
Write-Message -Level Verbose -Message $message
$output.Notes += $message
}
}
$output.Log = $updateResult.stdout
} catch {
Stop-Function -Message "Upgrade failed" -ErrorRecord $_
$output.Notes += $_.Exception.Message
return $output
} finally {
## Cleanup temp
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Cleaning up extracted files from $spExtractPath"
#double check if restart is needed
try {
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Removing temporary files"
$null = Invoke-CommandWithFallBack @execParams -ScriptBlock {
if ($args[0] -like '*\dbatools_KB*_Extract*' -and (Test-Path $args[0])) {
Remove-Item -Recurse -Force -LiteralPath $args[0] -ErrorAction Stop
}
} -Raw -ArgumentList $spExtractPath
$restartNeeded = Test-PendingReboot -ComputerName $computer -Credential $Credential -NoPendingRename:$NoPendingRenameCheck
} catch {
$message = "Failed to cleanup temp folder on computer $($computer)`: $($_.Exception.Message)"
Write-Message -Level Verbose -Message $message
$output.Notes += $message
$restartNeeded = $false
Stop-Function -Message "Failed to get reboot status from $computer" -ErrorRecord $_
}
}
#double check if restart is needed
try {
$restartNeeded = Test-PendingReboot -ComputerName $computer -Credential $Credential -NoPendingRename:$NoPendingRenameCheck
} catch {
$restartNeeded = $false
Stop-Function -Message "Failed to get reboot status from $computer" -ErrorRecord $_
}
if ($updateResult.ExitCode -eq 3010 -or $restartNeeded) {
if ($Restart) {
# Restart the computer
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Restarting computer $($computer) and waiting for it to come back online"
Write-Message -Level Verbose "Restarting computer $($computer) and waiting for it to come back online"
try {
$null = Restart-Computer @restartParams
$output.Restarted = $true
} catch {
Stop-Function -Message "Failed to restart computer $($computer)" -ErrorRecord $_
if ($updateResult.ExitCode -eq 3010 -or $restartNeeded) {
if ($Restart) {
# Restart the computer
Write-ProgressHelper -ExcludePercent -Activity $activity -Message "Restarting computer $($computer) and waiting for it to come back online"
Write-Message -Level Verbose "Restarting computer $($computer) and waiting for it to come back online"
try {
$null = Restart-Computer @restartParams
$output.Restarted = $true
} catch {
Stop-Function -Message "Failed to restart computer $($computer)" -ErrorRecord $_
$output.Notes += "Restart is required for computer $($computer) to finish the installation of SQL$($currentAction.MajorVersion)$($currentAction.TargetLevel)"
}
} else {
$output.Notes += "Restart is required for computer $($computer) to finish the installation of SQL$($currentAction.MajorVersion)$($currentAction.TargetLevel)"
}
} else {
$output.Notes += "Restart is required for computer $($computer) to finish the installation of SQL$($currentAction.MajorVersion)$($currentAction.TargetLevel)"
}
$output
}
$output
} finally {
Write-Progress -Activity $activity -Completed
}
}
Loading
Loading