1

I am trying to automate a few tedious tasks at my work. I do not have admin rights, so I cannot download/install anything. All I can use is Windows PowerShell or other native Windows programs.

Right now I'm running into the problem of getting PowerShell to pause itself until a program opens fully. I am trying to open a program on my local desktop, and have PwerShell sign in for me. I could use a -wait or start-sleep, but these computers are old. Sometimes they take 5 sec to open other times it takes 5 mins.

Is there away way to tell PowerShell to check if a program is fully loaded before continuing to run the script?

PowerShell V1.0 BTW

muzaffar
  • 1,706
  • 1
  • 15
  • 28
AutomateMyJob
  • 307
  • 1
  • 2
  • 9
  • You have to use native Win32 calls to get the state of another application. That means you'll need to use [PInvoke](http://stackoverflow.com/questions/11065026/get-window-state-of-another-process), which, in PowerShell means you need to [create a custom type](http://www.leeholmes.com/blog/2009/01/19/powershell-pinvoke-walkthrough/). I have no idea if PowerShell v1.0 can do any of this. I remember it had significant limitations. – Bacon Bits Sep 03 '15 at 20:09
  • Thanks. Wish I could just code the stuff in python. -.- – AutomateMyJob Sep 03 '15 at 21:36

1 Answers1

0

This was something that I was asking of myself just moments ago, and happened to come across your question. I realise that my response is rather late but thought that I would post something up in case if other people stumble across this question of yours. I did some digging around and I thing that the following code snippet may be of use to you:

$appProcess = Start-Process -FilePath "INSERT_PATH_TO_APP_EXE_HERE" -PassThru
$applicationStarted = $False
$stopCheckTime = (Get-Date).AddMinutes(1)

While (!$applicationStarted -and (Get-Date) -lt $stopCheckTime)
{
    Start-Sleep -Seconds 10
    $tempProc = Get-Process -Id $appProcess.Id
    $applicationStarted = $tempProc.MainWindowHandle -ne 0 -and ![string]::IsNullOrWhitespace($tempProc.MainWindowTitle)
}

if ($applicationStarted)
{
    # Do whatever you need to do once the application is launched
}
else
{
    # Log/raise error
}

To explain what is happening, when a user-interface application (e.g. Notepad) is launched, the MainWindowHandle pointer value of the process will be non-zero and typically the MainWindowTitle property of the process will have a string value in it (e.g. Notepad). When I initially launch the application using Start-Process I specify the PassThru flag so that I can immediately get the process ID, which then allows me to periodically check that the application is running.

With the application that I was using this code snippet for, the application takes a bit of time to launch (2 seconds or thereabouts) as it has to establish a connection to a local database during launch, as well as make some HTTP calls before the application is presented to the user. Hence why I have this check in a While loop whereby I'm checking every 10 seconds in a 1 minute period.