Skip to content

Blog

Debugging ConvertTo-Json

PowerShell terminal showing a duplicate key error when using ConvertTo-Json

My co-worker, Jared, who regularly uses MilestonePSTools and builds tools with it brought me an interesting problem yesterday - an apparent compatibility issue between our SDK and the ConvertTo-Json cmdlet from the built-in Microsoft.PowerShell.Utility module. If you've ever struggled with the error "ConvertTo-Json : An item with the same key has already been added.", follow along as I share our debugging process until we finally discovered the root cause. While the details in this post involve the Milestone SDK and the MilestonePSTools module, the root cause and the process of finding it is relevant to anyone using .NET assemblies or 3rd party modules with PowerShell.

Argument Transformation Attributes

Introduction

Argument transformation attributes make it possible to offer your users some flexibility in how they supply values for parameters. I've started to use these in the MilestonePSTools module to make it possible to provide a name instead of a strongly typed object like a [RecordingServer] or a [Role], while still making it clear in the Get-Help documentation what the expected object type is, and without polluting functions with object transformation code.

Your users deserve argument completers

Introduction

One of the things I love about PowerShell is the focus on usability and discoverability. The PowerShell team, and the community, have invested so much into reducing friction and accelerating your workflow. Argument completers are one of the tools available to you, and you should consider adding them to your projects if you aren't doing so already.

Securely Reading Passwords from the Console

If you've ever written a console application which requires the user to enter sensitive information like a password or a token, you might have wrestled with concerns of exposing the password in plain text within the console window.