Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The basic commands I always put in all of my PowerShell $Profile.CurrentUserAllHosts
Import-Module SqlServer;
cd C:\temp;
<# First we need an argument completer for -ServerInstance #>
Register-ArgumentCompleter -ParameterName ServerInstance -ScriptBlock {
(dir -Recurse SQLSERVER:\SQLRegistration\'Database Engine Server Group'\ |
?{ $_.Mode -ne 'd'} |
Group-Object ServerName).Name | ForEach-Object {
$CompletionText = $_
New-Object System.Management.Automation.CompletionResult (
$CompletionText,
$_,
'ParameterValue',
"$_ (SQLInstance)"
)
}
};
<# When the Database Parameter is called -Name we do this #>
Register-ArgumentCompleter -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
#$global:__LastFakeBoundParameter = $fakeBoundParameter
if ($fakeBoundParameter.ContainsKey('ServerInstance')) {
(dir "SQLSERVER:\SQL\$($fakeBoundParameter.ServerInstance)\Databases").Name | ForEach-Object {
New-Object System.Management.Automation.CompletionResult (
$_,
$_,
'ParameterValue',
"$_ (Database)"
)
}
}
};
<# When the Parameter is called -Database we do this to provide choices #>
Register-ArgumentCompleter -ParameterName Database -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
if ($fakeBoundParameter.ContainsKey('ServerInstance')) {
(Get-SqlDatabase -ServerInstance $($fakeBoundParameter.ServerInstance)).Name | ForEach-Object {
$CompletionText = $_
New-Object System.Management.Automation.CompletionResult (
$CompletionText,
$_,
'ParameterValue',
"$_ (Database)"
)
}
}
};
<# When the Parameter is called -Database we do this #>
Register-ArgumentCompleter -ParameterName DatabaseName -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
if ($fakeBoundParameter.ContainsKey('ServerInstance')) {
(Get-SqlDatabase -ServerInstance $($fakeBoundParameter.ServerInstance)).Name | ForEach-Object {
$CompletionText = $_
New-Object System.Management.Automation.CompletionResult (
$CompletionText,
$_,
'ParameterValue',
"$_ (Database)"
)
}
}
};
Start-Transcript -OutputDirectory "$($HOME)\Documents\Transcripts";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.