dbatools.io – blind-date: why don’t I see my changes?

Blind date: PowerShell & dbatools
Blind date: PowerShell & dbatools

Last month I have demonstrated how to contribute to the dbatools.io without touching PowerShell editor. This time let’s see how to do some PowerShell, shall we?

So exciting! However, you might find here why some stuff did not work quite well, as I found the hard way. You’ve been warned.

Invite dbatools for dinner (or tea)

Yes, it is very exciting, but you want to take things slowly? Do not want to install the PowerShell module on your computer yet? That is fine. Just have it for a while, without engaging too much. Simply go to dbatools.io GitHub repository and download the ZIP file of development branch:

After some time, depending on your connection speed you’ll see this fancy new ZIP file (at this point about 25MB big, 100MB unpacked) in your default download location (or wherever you’ve decided to save it):

After unpacking the file you will see the expected content. Nice, dbatools are in the house.

Here comes the PowerShell ISE

It is quite natural to use PowerShell ISE when one has no experience with PowerShell before. You’re not familiar with VS Code? Want to see just scripts and be able to run it right away, like you, did, in SQL Server Management Studio. Nothing easier.

Just look for the PowerShell ISE in your system and run it as an Administrator (CTRL+SHIFT+left-click the app might do the trick too). Here is the editor ran as Administrator:

We have a date

dbatools – present, PowerShell editor – present, let’s do it… slowly. Check if the module is installed – if it’s your first time it most likely won’t be there.

PS C:\Temp> Get-Module dbatools
PS C:\Temp>

No results – good 🙂

Unblock me, please

Let’s import dbatools from your local directory and…

PS C:\Temp> Import-Module '.\dbatools-development\dbatools.psd1'

…oh no… it screams at you, but you really want to contribute. Let’s unblock that file (and later read about the cmdlet on Microsoft Docs).

# Unblock the file downloaded from the internet
PS C:\Temp> Get-ChildItem -Recurse '.\dbatools-development' | Unblock-File

# Retry importing the module (note the -Force switch)
PS C:\Temp> Import-Module '.\dbatools-development\dbatools.psd1' -Force

# Check if the module got loaded
PS C:\Temp> Get-Module dbatools

<#
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0.111 dbatools {Attach-DbaDatabase, Detach-DbaDatabase…}
#>

Yes! Now we are talking.

Make some noise changes

Let’s see the shortest dbatools function – Measure-DbatoolsImport:

PS C:\Temp> (Get-Command Measure-DbatoolsImport).Definition
<#
[Sqlcollaborative.Dbatools.dbaSystem.DebugHost]::ImportTime
#>

Now, add a test comment only. Open in PowerShell ISE, edit and save.

PS C:\Temp> psEdit .\dbatools-development\functions\Measure-DbatoolsImport.ps1

Why? Why you don’t love me?

You’ve made the change in file, you saved it (right?), now let’s see it in the wild.

# Retry importing the module (note the -Force switch)
PS C:\Temp> Import-Module '.\dbatools-development\dbatools.psd1' -Force

# Check the definition... and be disappointed
PS C:\Temp> (Get-Command Measure-DbatoolsImport).Definition
<#
[Sqlcollaborative.Dbatools.dbaSystem.DebugHost]::ImportTime
#>

That was supposed to be easy… What did you miss?

It’s all about GIT

I bet you have missed the .git folder in your root directory. That’s how it works, you need to have that in order to see the changes you’ve made. Otherwise you’re loading functions from allcommands.ps1 (and you do not want to make changes there – it’s a huge file). Fix your setup:

# Check the definition... and be disappointed
PS C:\Temp> New-Item -Name .\dbatools-development\.git -ItemType Directory
<#
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 22/05/2020 22:47 .git
#>
# Retry importing the module (note the -Force switch)
PS C:\Temp> Import-Module '.\dbatools-development\dbatools.psd1' -Force

# Check the definition... and be HAPPY
PS C:\Temp> (Get-Command Measure-DbatoolsImport).Definition
<#
[Sqlcollaborative.Dbatools.dbaSystem.DebugHost]::ImportTime
# Test comment from Mikey to show that changes are possible.
#>


I will not tell how much time I have spent on this, but thanks to my colleague Jess Pomfret (blog|twitter) I saved some hair on my head before going mad.

Thanks for getting to this point. I hope you liked it and your life is a bit easier now. Time for some fun:

Blind date: PowerShell & dbatools
Blind date: PowerShell & dbatools

Thank you,
Mikey

Leave a Reply