Apologies if this question has already been answered on another thread, but I went looking for my specific query and I couldn't anything.
The issue I have is sending keystrokes to a Read-Host command. When I do this, nothing populates. Is this even possible?
$sendKeys = [System.Windows.Forms.SendKeys]
Sleep-Start -Seconds 1
$sendKeys::SendWait('serv01{ENTER}')
Read-Host -Prompt "Server Name"
Running this would only show the prompt and still waiting for input:
Server Name:
Please advise if this is possible.
CodePudding user response:
Your code:
DOES work in regular console windows, in Windows Terminal, and - assuming the keyboard focus is on the integrated terminal when the code is run - in Visual Studio Code.
does NOT work in the Windows PowerShell ISE.
However, given that the ISE is obsolescent, consider migrating to Visual Studio Code with its PowerShell extension.
As a workaround, consider the technique shown in this answer, which uses a custom
Read-Host
function that returns predefined responses.
Note: The workaround suggested for use in the ISE may actually be the preferable solution overall, given that sending keystrokes is inherently brittle (not fully robust).