-1

I first need to offer the disclaimer that I am barely a novice when it comes to scripting or coding, so I might be doing this all wrong in the first place. I'm running into an issue with a script I had written in powershell a while ago that is used on several hundred computers in my company daily to automate starting a certain app that everyone uses. So far everyone else hasn't had an issue except one machine. Normally when I run the script, it starts the services and cli for the program which opens in a new window. Then the script, still open behind that, sends all of the key commands to that cli window, pauses for a brief line of user input after which it closes that cli window, does a few other clean up tasks and closes out itself. The only difference on this one machine that's not working is the script won't pass the keystrokes to the cli window. If I put them in manually including the normal user input, it finishes the rest of the script just fine, but the cli window is just not receiving the keystroke input from the main script. The only part of the script that send the keystrokes to the cli window is a few lines of this: [System.Windows.Forms.SendKeys]::SendWait("Text{Enter}")

I have tried using the sleep command before and in between the key commands to make sure it's not going through the keystrokes to early. I've checked and windows had gone through an Update two days ago, but we would've heard about this issue right then if that was it. I'm at a loss for what the issue could be and would apreciate and help, tips, or direction to help figure this one out.

  • it is hard to tell whats going on when you don't share all of your script. thus my first question is: have you loaded the assembly beforehand `[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')` so you can use "SendKeys"? – Guenther Schmitz Aug 15 '20 at 05:13
  • I'm pretty sure, again sorry I'm still rather new to this, but I have this at the beginning. `Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop` And like I mentioned before, it's able to use SendKeys on all of the other machines, so maybe would there be an issue with it calling that command for some reason? – Jason Dashaer Aug 15 '20 at 12:04
  • I'm just really confused since the one machine it has an issue with is 1 of several hundred that are practically a carbon copy of each other, so it's been hard to find the differences that would cause this issue. I'm not sure if it's having an issue loading that command to be able to use it, or if it's not using it on the cli window correctly, or if it's a permissions issue somewhere. I'm not sure it'd be because of an update because there are all of the others that are up to date that are working fine. – Jason Dashaer Aug 15 '20 at 12:20
  • so to debug it generally works try a script like `Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop;Start-Process notepad;Start-Sleep -Seconds 5; [System.Windows.Forms.SendKeys]::SendWait("Text{Enter}")` on the machine in question to see if it doesn't work at all. I think it might be a focus/timing issue (cli window not in focus) – Guenther Schmitz Aug 15 '20 at 12:40
  • Ok, just tried that and it opened notepad fine, but gave no text input or keystrokes to notepad, and then closed out after and left the notepad window open. I forgot I had tried something similar by adding a sleep function just before the lines to send keys in and just experimented with making sure it was definitely selected and able to be inputted to and that there were no interruptions. But otherwise, no dice on that. Would that be more likely it's failing to pass the keystroke to the other window or that it's not loading the Sendkeys command in the first place? – Jason Dashaer Aug 15 '20 at 12:55
  • sounds odd .. maybe you can find the difference of loaded assemblies on the hosts `([System.AppDomain]::CurrentDomain.GetAssemblies()).count` compared to one (with the same OS/build)? to dig deeper follow https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/finding-loaded-assemblies – Guenther Schmitz Aug 15 '20 at 14:37
  • As for this [I am barely a novice when it comes to scripting or coding], that is fine, but it's best you get so ramp-up first to avoid confusion, frustrations, misconceptions, bad code, damaging/destroying your system or your enterprise. There are plenty of Youtube vides to get you going. ['Beginning PowerShell'](https://www.youtube.com/results?search_query=%27Beginning+PowerShell%27) – postanote Aug 16 '20 at 03:59
  • Remember SO has rules... [Provide MRE](https://stackoverflow.com/help/minimal-reproducible-example) --- [How to ask](https://stackoverflow.com/help/how-to-ask) --- [Don't ask](https://stackoverflow.com/help/dont-ask) --- [Proper Topic](https://stackoverflow.com/help/on-topic) – postanote Aug 16 '20 at 04:01

2 Answers2

0

Is the console window the active window when the keys are sent? The documentation for the class you are calling indicates that it sends the keys to the active window. Not only that, but the documentation also seems to indicate that there are some issues that developers have run into when using that class to sends keys. I would try using the autoit powershell module instead. Autoit is its own scripting language which specializes in automating windows processes and being capable of automating interactions with windows forms, but also comes bundled with its own powershell module that I think is exactly what you need, so I would download the portable "zip" package, extract the powershell module from "Autoitx", and that should help you accomplish what you need

  • Ok, good to know that it's finicky with others too. I was trying to avoid using any resources that weren't already preinstalled since that adds another variable to troubleshooting as well as originally adding that to over 400 machines would've been somewhat more cumbersome at first. I could update it now to be more reliable using the autoit module you're suggesting and roll it out more casually since we have a current working option. I don't currently have the most access to just spam an update out so I have to consider how long it would take to add that to all machines. Thanks for the help! – Jason Dashaer Aug 16 '20 at 23:15
  • Oh and I forgot to mention to your earlier thought; yes, the console window is the active window when everything is sent. We have run into the issue where someone will accidentally interrupt it by doing other things while it's running, but we partially fixed that by disabling quick edit mode and insert mode. I'm really interested in this module though and hope it'll be an easy enough update. – Jason Dashaer Aug 16 '20 at 23:22
0

PowerShell automation especially when using SendKeys can / is glitchy due to many varying reasons. Can you use SendKeys sure, but you have to know the environment it will be run in and the needed performance details. Hence your futzing/guessing with Sleep.

There are purpose-built tools to help.

Auto HotKey

'PowerShell auto hotkey'

or the UIAutomation tool

'PowerShell automating other applications'

postanote
  • 15,138
  • 2
  • 14
  • 25