|
| 1 | +function Add-DbaInstanceList { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Adds one or more SQL Server instances to the user-maintained autocomplete list. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Adds SQL Server instance names to a persistent list that pre-populates the tab completion |
| 8 | + cache for the -SqlInstance parameter across all dbatools commands. This allows users to |
| 9 | + have their frequently used instances available for autocomplete in their PowerShell |
| 10 | + terminal without needing to connect to them first. |
| 11 | +
|
| 12 | + The instance list is stored using the dbatools configuration system. Use -Register to |
| 13 | + persist the list across PowerShell sessions. |
| 14 | +
|
| 15 | + Instances can also be pre-loaded at module import time by setting the |
| 16 | + $env:DBATOOLS_KNOWN_INSTANCES environment variable to a comma-separated list of instance |
| 17 | + names in your PowerShell profile. |
| 18 | +
|
| 19 | + .PARAMETER SqlInstance |
| 20 | + The SQL Server instance name or names to add to the autocomplete list. |
| 21 | + Accepts pipeline input. |
| 22 | +
|
| 23 | + .PARAMETER Register |
| 24 | + Persists the instance list to disk so it is available in future PowerShell sessions. |
| 25 | + Without this switch, the list only exists for the current session. |
| 26 | +
|
| 27 | + .PARAMETER Scope |
| 28 | + Determines where the persistent configuration is stored when using -Register. |
| 29 | + UserDefault stores the setting for the current user only. |
| 30 | +
|
| 31 | + .OUTPUTS |
| 32 | + None |
| 33 | +
|
| 34 | + This command updates the autocomplete cache but does not output any objects to the |
| 35 | + pipeline. Use Get-DbaInstanceList to retrieve the configured instance names. |
| 36 | +
|
| 37 | + .NOTES |
| 38 | + Tags: TabCompletion, Autocomplete |
| 39 | + Author: the dbatools team + Claude |
| 40 | +
|
| 41 | + Website: https://dbatools.io |
| 42 | + Copyright: (c) 2018 by dbatools, licensed under MIT |
| 43 | + License: MIT https://opensource.org/licenses/MIT |
| 44 | +
|
| 45 | + .LINK |
| 46 | + https://dbatools.io/Add-DbaInstanceList |
| 47 | +
|
| 48 | + .EXAMPLE |
| 49 | + PS C:\> Add-DbaInstanceList -SqlInstance "sql01", "sql02\dev" |
| 50 | +
|
| 51 | + Adds sql01 and sql02\dev to the autocomplete instance list for the current session. |
| 52 | +
|
| 53 | + .EXAMPLE |
| 54 | + PS C:\> Add-DbaInstanceList -SqlInstance "sql01" -Register |
| 55 | +
|
| 56 | + Adds sql01 to the autocomplete instance list and persists it across PowerShell sessions. |
| 57 | +
|
| 58 | + .EXAMPLE |
| 59 | + PS C:\> "sql01", "sql02" | Add-DbaInstanceList -Register |
| 60 | +
|
| 61 | + Adds two instances to the list via pipeline and persists them across sessions. |
| 62 | + #> |
| 63 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] |
| 64 | + [CmdletBinding()] |
| 65 | + param ( |
| 66 | + [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] |
| 67 | + [string[]]$SqlInstance, |
| 68 | + [switch]$Register, |
| 69 | + [Dataplat.Dbatools.Configuration.ConfigScope]$Scope = [Dataplat.Dbatools.Configuration.ConfigScope]::UserDefault |
| 70 | + ) |
| 71 | + |
| 72 | + begin { |
| 73 | + $current = Get-DbatoolsConfigValue -FullName "TabExpansion.KnownInstances" -Fallback @() |
| 74 | + $toAdd = @() |
| 75 | + } |
| 76 | + |
| 77 | + process { |
| 78 | + foreach ($instance in $SqlInstance) { |
| 79 | + $lower = $instance.Trim().ToLowerInvariant() |
| 80 | + if (-not $lower) { continue } |
| 81 | + |
| 82 | + if ($current -notcontains $lower -and $toAdd -notcontains $lower) { |
| 83 | + $toAdd += $lower |
| 84 | + } |
| 85 | + |
| 86 | + # Update the TEPP cache immediately for this session |
| 87 | + if ([Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] -notcontains $lower) { |
| 88 | + [Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] += $lower |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + end { |
| 94 | + if ($toAdd.Count -gt 0) { |
| 95 | + $combined = @($current) + @($toAdd) |
| 96 | + Set-DbatoolsConfig -FullName "TabExpansion.KnownInstances" -Value $combined |
| 97 | + } |
| 98 | + if ($Register) { |
| 99 | + Register-DbatoolsConfig -FullName "TabExpansion.KnownInstances" -Scope $Scope |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments