Skip to content

Commit

Permalink
Install-DbaFirstResponderKit - New parameter to be more flexible (dat…
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjordan authored May 14, 2021
1 parent 2d819e5 commit 8306765
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
49 changes: 44 additions & 5 deletions functions/Install-DbaFirstResponderKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ function Install-DbaFirstResponderKit {
Specifies the path to a local file to install FRK from. This *should* be the zip file as distributed by the maintainers.
If this parameter is not specified, the latest version will be downloaded and installed from https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit
.PARAMETER OnlyScript
Specifies the name(s) of the script(s) to run for installation. Wildcards are permitted.
This way only part of the First Responder Kit can be installed.
Using one of the three official Install-* scripts (Install-All-Scripts.sql, Install-Core-Blitz-No-Query-Store.sql, Install-Core-Blitz-With-Query-Store.sql) is possible this way.
Even removing the First Responder Kit is possible by using the official Uninstall.sql.
.PARAMETER Force
If this switch is enabled, the FRK will be downloaded from the internet even if previously cached.
Expand Down Expand Up @@ -90,6 +96,21 @@ function Install-DbaFirstResponderKit {
PS C:\> Install-DbaFirstResponderKit -SqlInstance sql2016 -Branch dev
Installs the dev branch version of the FRK in the master database on sql2016 instance.
.EXAMPLE
PS C:\> Install-DbaFirstResponderKit -SqlInstance sql2016 -OnlyScript sp_Blitz.sql, sp_BlitzWho.sql, SqlServerVersions.sql
Installs only the procedures sp_Blitz and sp_BlitzWho and the table SqlServerVersions by running the corresponding scripts.
.EXAMPLE
PS C:\> Install-DbaFirstResponderKit -SqlInstance sql2016 -OnlyScript Install-Core-Blitz-No-Query-Store.sql
Installs only part of the First Responder Kit by running the official install script.
.EXAMPLE
PS C:\> Install-DbaFirstResponderKit -SqlInstance sql2016 -OnlyScript Uninstall.sql
Uninstalls the First Responder Kit by running the official uninstall script.
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Medium")]
param (
Expand All @@ -100,6 +121,12 @@ function Install-DbaFirstResponderKit {
[string]$Branch = "main",
[object]$Database = "master",
[string]$LocalFile,
[ValidateSet('Install-All-Scripts.sql', 'Install-Core-Blitz-No-Query-Store.sql', 'Install-Core-Blitz-With-Query-Store.sql',
'sp_Blitz.sql', 'sp_BlitzFirst.sql', 'sp_BlitzIndex.sql', 'sp_BlitzCache.sql', 'sp_BlitzWho.sql', 'sp_BlitzQueryStore.sql',
'sp_BlitzAnalysis.sql', 'sp_BlitzBackups.sql', 'sp_BlitzInMemoryOLTP.sql', 'sp_BlitzLock.sql',
'sp_AllNightLog.sql', 'sp_AllNightLog_Setup.sql', 'sp_DatabaseRestore.sql', 'sp_ineachdb.sql',
'SqlServerVersions.sql', 'Uninstall.sql')]
[string[]]$OnlyScript,
[switch]$Force,
[switch]$EnableException
)
Expand Down Expand Up @@ -180,9 +207,23 @@ function Install-DbaFirstResponderKit {
} else {
$null = New-Item -Path $LocalCachedCopy -ItemType Container
}
Copy-Item -Path "$zipFolder\sp_*.sql" -Destination $LocalCachedCopy
Copy-Item -Path "$zipFolder\SqlServerVersions.sql" -Destination $LocalCachedCopy
Copy-Item -Path "$zipFolder\*.sql" -Destination $LocalCachedCopy
}
}

if ($OnlyScript) {
$sqlScripts = @()
foreach ($script in $OnlyScript) {
$sqlScript = Get-ChildItem $LocalCachedCopy -Filter $script
if ($sqlScript) {
$sqlScripts += $sqlScript
} else {
Write-Message -Level Warning -Message "Script $script not found in $LocalCachedCopy, skipping."
}
}
} else {
$sqlScripts = Get-ChildItem $LocalCachedCopy -Filter "sp_*.sql"
$sqlScripts += Get-ChildItem $LocalCachedCopy -Filter "SqlServerVersions.sql"
}
}

Expand All @@ -192,7 +233,7 @@ function Install-DbaFirstResponderKit {
foreach ($instance in $SqlInstance) {
if ($PSCmdlet.ShouldProcess($instance, "Connecting to $instance")) {
try {
$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential
$server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential -NonPooledConnection
} catch {
Stop-Function -Message "Error occurred while establishing connection to $instance" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}
Expand All @@ -203,8 +244,6 @@ function Install-DbaFirstResponderKit {
$allprocedures = ($server.Query($allprocedures_query, $Database)).Name

# Install/Update each FRK stored procedure
$sqlScripts = Get-ChildItem $LocalCachedCopy -Filter "sp_*.sql"
$sqlScripts += Get-ChildItem $LocalCachedCopy -Filter "SqlServerVersions.sql"
foreach ($script in $sqlScripts) {
$scriptName = $script.Name
$scriptError = $false
Expand Down
6 changes: 3 additions & 3 deletions tests/Install-DbaFirstResponderKit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[array]$params = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($CommandName, 'Function')).Parameters.Keys
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Branch', 'Database', 'LocalFile', 'Force', 'EnableException'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Branch', 'Database', 'LocalFile', 'OnlyScript', 'Force', 'EnableException'

It "Should only contain our specific parameters" {
Compare-Object -ReferenceObject $knownParameters -DifferenceObject $params | Should -BeNullOrEmpty
Expand Down Expand Up @@ -45,7 +45,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
}
It "Shows status of Error" {
$folder = Join-Path (Get-DbatoolsConfigValue -FullName Path.DbatoolsData) -Child "SQL-Server-First-Responder-Kit-main"
$sqlScript = (Get-ChildItem $folder | Select-Object -First 1).FullName
$sqlScript = (Get-ChildItem $folder -Filter "sp_*.sql" | Select-Object -First 1).FullName
Add-Content $sqlScript (New-Guid).ToString()
$result = Install-DbaFirstResponderKit -SqlInstance $script:instance2 -Database $database -Verbose:$false
$result[0].Status -eq "Error" | Should -Be $true
Expand Down Expand Up @@ -88,7 +88,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
}
It "Shows status of Error" {
$folder = Join-Path (Get-DbatoolsConfigValue -FullName Path.DbatoolsData) -Child "SQL-Server-First-Responder-Kit-main"
$sqlScript = (Get-ChildItem $folder | Select-Object -First 1).FullName
$sqlScript = (Get-ChildItem $folder -Filter "sp_*.sql" | Select-Object -First 1).FullName
Add-Content $sqlScript (New-Guid).ToString()
$result = Install-DbaFirstResponderKit -SqlInstance $script:instance3 -Database $database
$result[0].Status -eq "Error" | Should -Be $true
Expand Down

0 comments on commit 8306765

Please sign in to comment.