Powershell - rubber duck debugging with yourself
I find that performing rubber duck debugging is a great way to diagnose an issue with your code, usually a simple one.
With set-psdebug, you’re able to perform rubber duck debugging with yourself!
Long ago I discovered this great cmdlet, forgot about it, discovered it again, forgot it etc… As of now, it’s committed to memory :).
Today I fixed a simple issue with variable assignment, and set-psdebug really stepped up and saved me lots of time.
Issue:
I’d done the ol’ “set a variable then immediately set the same one” chessnut, and just had not seen the forest through the trees:
I’ve had to redact a bunch of info in the above code snippet, and won’t get into details about what’s required, and why.
Basically our API is expecting a particular request body, which was the same as a previously assigned variable. You can see that we set the “ordertime” object, only to set the “ordertime” variable with a date/time 1 day in the future.
I’ll let you run ‘man set-psdebug’ to find what it does, but here is what set-psdebug showed me:
We can see a few things happening above:
-
The ordertime variable getting assigned a hashtable containing particular values
-
Some other pieces of code are executed
-
Powershell runs the line of code which assigns the date cmdlet to the ordertime variable
-
Ordertime variable gets assigned the value of the get-date cmdlet
Once I stepped through what set-psdebug showed me, the error was obvious.