squid808 squid808 - 1 month ago 19
C# Question

Invoking Read-Host cmdlet from within C# Custom Cmdlet

I'm writing a set of Powershell Cmdlets that require Oauth2 authentication. I currently am able to get to the point where a new browser window opens and the user copies out the Access Authentication Code to pass back to the cmdlets. However, this means I have to have a separate cmdlet that now accepts the Auth Code as input.

Is there any way to invoke the Read-Host? I've tried the instructions but despite being listed, ReadHostCommand isn't showing up under the Microsoft.PowerShell.Commands namespace, but something like WhereObjectCommand is.

I've tried Console.ReadLine() but it is just ignored when running the Cmdlet.

Answer

The ReadHostCommand is in the Microsoft.PowerShell.Commands namespace but you have to reference the Microsoft.PowerShell.Commands.Utility.dll assembly.