dbaclone icon indicating copy to clipboard operation
dbaclone copied to clipboard

Documentation on how to debug

Open blitzmann opened this issue 4 years ago • 9 comments

I would like to be able to debug the cmdlets so that I can have better insight into errors / posssible help contribute. I used to know how, but that knowledge has been lost to time. It would be nice if that information was available via a Developer Getting Started wiki page

blitzmann avatar Aug 20 '21 19:08 blitzmann

That is certainly possible. I will work on that to get something going but it has been very busy lately

sanderstad avatar Aug 20 '21 20:08 sanderstad

It is not true debugging but you can specify -Verbose which gives you a good indication of whats happening. It is what I have been using to identify and resolve issues. Could also add -WhatIf to prevent the module from making changes.

DatKyle avatar Aug 22 '21 10:08 DatKyle

It's something, and it's what I've resorted to before, but I'm looking for true debugging and how to run a local copy within VS Code without affecting any installed copy, so that I can make tweaks and put breakpoints, etc.

blitzmann avatar Aug 23 '21 19:08 blitzmann

I'm not sure how to help you with this because I debug it the same way by putting in debug messages where things possible go wrong and fix it that way. If you have an idea how to do that feel free to look into that and make a solution for that so that we can use improve debugging for the module.

sanderstad avatar Sep 09 '21 07:09 sanderstad

Even just some information on how you run this locally without having to install it, so that I can add print statements, run it and see what happens. But I'm running into issues there as well. Here's my steps so far:

  • Clone repo
  • Open Powershell in admin
  • Install dependancies:
    • Install-Module PSFramework
    • Install-Module dbatools
  • Import-Module .\dbaclone.psd1 -force

This produces

WARNING: [23:41:27][Get-DcnClone] The module setup has NOT yet successfully run. Please run 'Set-DcnConfiguration'
WARNING: [23:41:27][Get-DcnImage] The module setup has NOT yet successfully run. Please run 'Set-DcnConfiguration'
WARNING: [23:41:27][<Unknown>] The module is not yet configured. Please run Set-DcnConfiguration to make the neccesary changes
Exception calling "InvokeScript" with "4" argument(s): "You cannot call a method on a null-valued expression."
At C:\gitRoot\dbaclone\dbaclone.psm1:49 char:9
+     else { $ExecutionContext.InvokeCommand.InvokeScript($false, ([scr ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CmdletInvocationException

Any insight? I can compile any findings into a wiki page or a DEVELOPER.md file and PR it.

blitzmann avatar Sep 16 '21 04:09 blitzmann

I've done some digging and found an old Reddit post by yourself (@blitzmann): Reddit - VSCode: How to debug a function inside a module Based on the top comment about $doDotSource, I made a debug.ps1 file which seems to work:

# Sets $doDotSource to true.
$dbaclone_dotsourcemodule = $true;
Import-Module .\dbaclone.psd1 -Force

# Configure DbaClone to use a SQL Store
$username = "sa"
$password = ConvertTo-SecureString -AsPlainText "Pass@word!" -Force
$sqlCredential = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
$sqlInstance = "localhost"
Set-DcnConfiguration -InformationStore SQL -SqlInstance $sqlInstance -SqlCredential $sqlCredential

# Command to debug
Get-DcnImage

If your using VS code I used the below launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "PowerShell: Launch Current File",
            "type": "PowerShell",
            "request": "launch",
            "script": ".\\debug.ps1",
            "cwd": ""
        }
    ]
}

DatKyle avatar Sep 17 '21 13:09 DatKyle

@blitzmann have you had a chance to try out the above?

DatKyle avatar Sep 24 '21 12:09 DatKyle

@DatKyle I have not yet, but I'm looking forward to it. I plan to check it out on Friday when I can get back t this project

blitzmann avatar Oct 13 '21 16:10 blitzmann

@DatKyle sorry for the delay, I finally got a change to start looking into this stuff again. It does seem to work at first glance, would be nice if I could debug while issuing commands in the console, but this is a good start. I'll play around with it some more.

blitzmann avatar Jan 07 '22 19:01 blitzmann