Author Posts

March 26, 2017 at 9:06 pm

I've been working on this script and it works on my PC but I am positive it can be broken if the timing is changed slightly.

The first problem is I can't figure out how to sendkeys to the program in order to make it start running and simultaneously keep the program from closing via the /w or | out-null commands. When I try to add the out-null lines the script will still close the app before it is done running.

The 2nd issue is that I have inserted pauses in order to navigate the apps via sendkey commands but obviously this is gonna cause a problem if the app run speed varies even slightly.

So yeah..... I am sure there are other problems that will arise but these are the two I can see right now. Any advice would be greatly appreciated. Thank you.

& start "C:\program.lnk"
sleep 1
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('C:\program.lnk')
Sleep 1
$wshell.SendKeys('{right}')
Sleep 1
$wshell.SendKeys('~')
Sleep 1
$wshell.SendKeys('{right}')
Sleep 1
$wshell.SendKeys('~')
sleep 5
get-process program | %{ $_.closemainwindow() }
& start "C:\program.lnk"
Sleep 3
$wshell.SendKeys('~')
sleep 5
get-process program | %{ $_.closemainwindow() }
$wshell.SendKeys('{right}')
Sleep 3
$wshell.SendKeys('~')
& start "C:\program.lnk"
sleep 3
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('C:\program.lnk')
sleep 1
$wshell = New-Object -ComObject wscript.shell;
$wshell.SendKeys('{tab}')
sleep 1
$wshell.SendKeys('{tab}')
sleep 1
$wshell.SendKeys('{tab}')
sleep 1
$wshell.SendKeys('{tab}')
sleep 1
$wshell.SendKeys('~')
sleep 1
$wshell.SendKeys('~')
sleep 200
get-process program | %{ $_.closemainwindow() }
sleep 2
& start "C:\program.lnk"
$wshell = New-Object -ComObject wscript.shell;
sleep 10
$wshell.SendKeys('~')
sleep 10
$wshell.SendKeys('~')

March 27, 2017 at 10:38 am

Hi, ultimately what are you trying to achieve with this script ? Do you want to stop an application from being closed ?

March 27, 2017 at 2:17 pm

For this case, I would say use the right tool for the Job. While it can be done with PowerShell, this is a better scenario for AutoIT.

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting.

April 6, 2017 at 3:06 pm

Hmmm ... I think that's not what Powershell is made for. Either the program you want to control remotely has an API you can use or you should use a better fitting tool to remotely control the GUI interface of a program like AutoIT or AutoHotKey. They are made for that initially.