So I always like to show splatting PowerShell commands when I am presenting sessions or workshops and realised that I had not really blogged about it. (This blog is for @dbafromthecold who asked me to π )
What is Splatting?
Well you will know that when you call a PowerShell function you can use intellisense to get the parameters and sometimes the parameter values as well. This can leave you with a command that looks like this on the screen
Start-DbaMigration -Source $Source -Destination $Destination -BackupRestore -NetworkShare $Share -WithReplace -ReuseSourceFolderStructure -IncludeSupportDbs -NoAgentServer -NoAudits -NoResourceGovernor -NoSaRename -NoBackupDevices
It goes on and on and on and while it is easy to type once, it is not so easy to see which values have been chosen. It is also not so easy to change the values.
By Splatting the parameters it makes it much easier to read and also to alter. So instead of the above you can have
$startDbaMigrationSplat = @{ Source = $Source NetworkShare = $Share NoAgentServer = $true NoResourceGovernor = $true WithReplace = $true ReuseSourceFolderStructure = $true Destination = $Destination NoAudits = $true BackupRestore = $true NoSaRename = $true IncludeSupportDbs = $true NoBackupDevices = $true } Start-DbaMigration @startDbaMigrationSplat
This is much easier on the eye, but if you dont know what the parameters are (and are too lazy to use Get-Help – Hint You should always use Get-Help ) or like the convenience and efficiency of using the intellisense, this might feel like a backward step that slows your productivity in the cause of easy on the eye code.
Enter EditorServicesCommandSuite by SeeminglyScience for VS Code. Amongst the things it makes available to you is easy splatting and people are always impressed when I show it
Best thing i learned at #SqlSatIceland... #PowerShell "splatting"
— Jan Mulkens (@JanMulkens) March 10, 2018
Byebye lines of code i no longer need!
Thanks @sqldbawithbeard @cl @dbachecks pic.twitter.com/a3PCkAeaV5
You can install it from the PowerShell Gallery like all good modules using
Install-Module EditorServicesCommandSuite -Scope CurrentUser
and then add it to your VSCode PowerShell profile usually found at C:\Users\USERNAME\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1
# Place this in your VSCode profile Import-Module EditorServicesCommandSuite Import-EditorCommand -Module EditorServicesCommandSuite
and now creating a splat is as easy as this.
Write the command, leave the cursor on a parameter, hit F1 – Choose PowerShell : Show Additional Commands (or use a keyboard shortcut) type splat press enter. Done π
So very easy π
Happy Splatting π
I love that in code because I love splatting and vscode but there is a nice module for those still hanging on to ISE or other IDEs
https://www.powershellgallery.com/packages/SplatHelper/0.1
Pingback: VS Code And Splatting Powershell – Curated SQL
Pingback: powershell splatting – dbatools
Hi, this looks great – but after editing the profile and restarting vcode i get the following error – any idea?
Import-EditorCommand : The term ‘Import-EditorCommand’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At C:\Users\Tflatley\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:13 char:1
+ Import-EditorCommand -Module EditorServicesCommandSuite
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Import-EditorCommand:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\Tflatley> $PSVERSIONTABLE
Name Value
—- —–
PSVersion 5.1.17134.407
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.17134.407
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PS C:\Users\Tflatley> get-module EditorServicesCommandSuite
ModuleType Version Name ExportedCommands
———- ——- —- —————-
Script 0.4.0 EditorServicesCommandSuite {Add-CommandToManifest, Add-ModuleQualification, Add-PinvokeMethod, ConvertTo-FunctionDefinition…}
thanks
Hi, you need to add it to add it to your VSCode PowerShell profile usually found at C:\Users\USERNAME\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1
I have updated the post also
thanks for the update – i had only added it to powershell_profile initially and all of the modules get imported (including EditorServices)in vscode – i created the VS profile with the exact same parameters and still get the error – i also get the error in standard powershell – so, must be some config on my end – thanks for the help and sorry to bother you will this. much appreciated.
I cant get it to work with the new vs-code probably missing something
Do you have the module installed and the code in your profile?
Have you reloaded your VS Code afterwards? F1 – Reload Window
Have you placed your cursor on the function name?
Those should fix it. I definitely have it working in all VS Codes
even with windows 10 1903 update
it used to wortk before
Hmm, dont have 1903 yet. Let me get it and
see what is happening