0

I am trying to write a Powershell script, and part of the script requires use of the clipboard. I've run into a very strange bug where when I use Read-Host below to scan in user input, when I run the script, it prints the "Starting..." line and then hangs indefinitely. As soon as I press another key, typically right arrow, the script prints the Read-Host prompt (the "There is already...") and scans in input like it should. If I press a letter key instead of right arrow, the same thing happens and the letter is counted as part of user input.

The extra strange part is, this problem happens only in this specific call of Read-Host. I use Read-Host 2 other times in the script and those calls work just fine, printing the prompt and scanning in input immediately.

---just a little more code above this point, mostly just variable declarations and such---
Write-Output "Starting vfont-inator... To stop early press and hold Ctrl+C"
[Console]::Out.Flush() 
$originalX = [System.Windows.Forms.Cursor]::Position.X
$originalY = [System.Windows.Forms.Cursor]::Position.Y

#move mouse to powershell window and click
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($originalX, $originalY)
[W.U32]::mouse_event(6,0,0,0,0);

#pause execution to give the user a chance to quit and save their clipboard before it is overwritten
$clipboard = Get-Clipboard
if($clipboard.Length -gt 0)
{   
   Read-Host "There is already something stored in the clipboard. This script requires using the clipboard to work, so it will overwrite anything already on the clipboard. Is this OK?`nPress Enter to overwrite the clipboard and continue, or Ctrl+C to quit the script."

}

Write-Output "Reached checkpoint 1: checked clipboard"
[Console]::Out.Flush() 
---more code below this point---

Things I've tried:

  • Assigning the output of Read-Host to a variable, like $input = Read-Host "There is..."
  • Adding | FT -AutoSize to the end of Read-Host, like I saw here
  • Sending a right key press automatically by adding the line [System.Windows.Forms.SendKeys]::SendWait("{RIGHT}") right below Read-Host
  • Adding the line [Console]::Out.Flush() right below Read-Host
  • Changing the inside of the if statement to:
Write-Output "There is..."
[Console]::Out.Flush()
Read-Host " "
  • Doing the above bulletpoint, but with Write-Host instead. This one I though for sure was gonna work, since printing to console and not a pipe is Write-Host's whole purpose. My script does not use any pipes, either.
  • Giving up, accepting this function call as cursed, and so copy/pasting one of the other Read-Host statements into this line instead. (which still didn't print, so I suppose this line number is just unholy desecrated ground, regardless of its content)

Would someone please help me see why my Read-Host statement hangs until I press a key before printing its prompt and asking for input, instead of just doing so automatically right away? Using a completely different way of scanning in input would be fine as well.

Thank you so much!

1 Answers1

1

I... I found the issue. I don't know how this could possibly affect it, but when I commented out the lines

#move mouse to powershell window and click
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($originalX, $originalY)
[W.U32]::mouse_event(6,0,0,0,0);

suddenly there is no more hang. I use these lines of code many many times throughout the script, and only here do they cause a problem. I have no idea what this happens or how this fixes it, but removing those two lines fixed it.

Apparently Powershell doesn't like being clicked on while running. I need a drink.