Skip to content

Instantly share code, notes, and snippets.

@SQLvariant
Last active June 23, 2020 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SQLvariant/13d038ef42481f17168bb97276bcce33 to your computer and use it in GitHub Desktop.
Save SQLvariant/13d038ef42481f17168bb97276bcce33 to your computer and use it in GitHub Desktop.
The basic commands I always put in all of my PowerShell $Profile.CurrentUserAllHosts
Import-Module SqlServer;
cd $home\Documents\PoSh\Load\
. .\Get-History2.ps1;
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)"
)
}
}
};
<# We need an argument completer for -Workspace #>
Register-ArgumentCompleter -ParameterName Workspace -ScriptBlock {
(Get-PowerBIWorkspace).Name | ForEach-Object {
$CompletionText = $_
New-Object System.Management.Automation.CompletionResult (
$CompletionText,
$_,
'ParameterValue',
"$_ (PowerBIWorkspace)"
)
}
};
<# We need an argument completer for -WorkspaceID #>
Register-ArgumentCompleter -ParameterName WorkspaceID -ScriptBlock {
(Get-PowerBIWorkspace).Id | ForEach-Object {
$CompletionText = $_
New-Object System.Management.Automation.CompletionResult (
$CompletionText,
$_,
'ParameterValue',
"$_ (PowerBIWorkspaceID)"
)
}
};
<# When the Report Parameter is called -Name we do this #>
Register-ArgumentCompleter -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
#$global:__LastFakeBoundParameter = $fakeBoundParameter
if ($fakeBoundParameter.ContainsKey('WorkspaceId')) {
(Get-PowerBIReport -WorkspaceId $fakeBoundParameter.WorkspaceId).Name | ForEach-Object {
New-Object System.Management.Automation.CompletionResult (
$_,
$_,
'ParameterValue',
"$_ (Database)"
)
}
}
};
<# First we need an argument completer for -SourceWorkspaceName #>
Register-ArgumentCompleter -ParameterName SourceWorkspaceName -ScriptBlock {
(Get-PowerBIWorkspace).Name | ForEach-Object {
$CompletionText = $_
New-Object System.Management.Automation.CompletionResult (
$CompletionText,
$_,
'ParameterValue',
"$_ (SQLInstance)"
)
}
};
<# First we need an argument completer for -TargetWorkspaceName #>
Register-ArgumentCompleter -ParameterName TargetWorkspaceName -ScriptBlock {
(Get-PowerBIWorkspace).Name | ForEach-Object {
$CompletionText = $_
New-Object System.Management.Automation.CompletionResult (
$CompletionText,
$_,
'ParameterValue',
"$_ (SQLInstance)"
)
}
};
Start-Transcript -OutputDirectory "$($HOME)\Documents\Transcripts";
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