scheduled task


  1. Posts : 52
    windows 10 Pro
       #1

    scheduled task


    I have a scheduled task that writes to a open window. Using the following script. This task has been running for years. All of a sudden the task runs but does not write to the window. If I manually start the scheduled task it works. Is it a problem with not waiting ling enough? Or is it something that has changed in the way things work. Using Windows 20h2.
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.AppActivate "Utility1"
    WScript.Sleep 1000
    WshShell.SendKeys ".\utility shutdown 23"
    WshShell.SendKeys "{Enter}"
    WScript.quit
      My Computer


  2. Posts : 16,965
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    The first thing I'd try would be temporarily increasing the sleep time to an excessive 5000 just to see if that made any difference.

    What made you put in that sleep line in the first place?
    What made you choose 1000?

    Is the app activated at all?

    Denis
      My Computer


  3. Posts : 52
    windows 10 Pro
    Thread Starter
       #3

    Originally I didn't have the sleep statement. I did a search and found an example of what I was doing. The code had a sleep statement. So I thought that I would try it. As I said this code has been working of years without the sleep. The application is a powershell window named Utility1. The utility is a powershell script the suspend the system at a specific time.
      My Computer


  4. Posts : 16,965
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    If you manually go to Utility1 then type in
    .\utility shutdown 23
    does it respond correctly?


    There has been no announced change and I have not seen any indications of any change in my Task scheduler.

    Your test [manually running the task] indicates that it is not a vbs, TS or PS problem.
    - You are evidently running a vbs script from TS to write in a window "Utiliy1" that is already running.
    - I also run vbs scripts from TS and mine have continued working as they always have.
    - None of the vbs scripts I run from TS use AppActivate but I have one that I run manually from a shortcut that does and it still works as it has always done [last used only a few days ago].
    - Personally, I'd still try a longer delay [as I suggested before]. It's grasping at straws to some extent but I have made other vbs scripts work doing it.
    - I also sometimes replace my intended vbs script with a simple MsgBox "This vbs script starts, if nothing else" to confirm that the TS task calls it correctly but I think you are beyond that.
    - I have nothing sensible to suggest to solve the problem.

    But what about switching to an alternative method of Sleeping / Hibernating the computer from TS that does not have to talk to another open utility window?
    - [Hibernate] shutdown.exe /h
    - [S3 Sleep] ---\NirCmd.exe standby
    I gave up on Rundll32-based suggestions for Sleep and, as you can see, I use NiSoft NirCmd.
    These are both reliable methods.


    Denis
    Last edited by Try3; 04 Nov 2021 at 10:09.
      My Computer


  5. Posts : 52
    windows 10 Pro
    Thread Starter
       #5

    The utility script does more than shutdown the system. No matter when the script is run it will wait until the specified time before starting to shutdown. Once it starts the shutdown process. The system maybe recording TV programs. So it waits until all recording is done. It then renames all of the recorded programs from that day. Then start the VAP program to gather information about commercials. Then moves the vprj files created by the VAP program to the final folder folder then suspends to memory.
      My Computer


  6. Posts : 1,201
    11 Home
       #6

    Instead of specifying the window title in AppActivate, the following code retrieves the Process ID of each process named Utility1.exe and uses that to work around the known problem of AppActivate no longer working as it should on Windows 10. As for the WScript.Sleep 1000 it causes the script to wait for one second to allow the active window to become ready to accept keyoard input after the window has just been been activated.

    Code:
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * from Win32_Process WHERE Name = 'Utility1.exe'") 
    If colItems.Count = 0 Then
        WScript.Echo "Process not found"
        WScript.Quit
    End If
    Set WshShell = WScript.CreateObject("WScript.Shell")
    For Each objProcess in colItems
        With WshShell
            .AppActivate(objProcess.ProcessId)
             WScript.Sleep 1000
            .SendKeys ".\utility shutdown 23"
            .SendKeys "{Enter}"  
        End With      
        Exit For
    Next
      My Computers


  7. Posts : 52
    windows 10 Pro
    Thread Starter
       #7

    Not sure this code will work on my system. There is no process named utility1.exe. Utility1 is the title of the powershell window. In task manager the windows powershell has two sub processes. One called Console Window Host the other called windows Powershell. Not sure if I changed the select statement to powershell would be correct.
    But thanks for the response. I will look into the possibility that this may fix my problem. I have two systems running this code generally only one fails at night.
    From your post this is a known problem?
      My Computer


  8. Posts : 1,201
    11 Home
       #8

    Yeah, the problem is that AppActivate no longer accepts a window title. It still does accept a process name, though, but the process name that you are looking for is powershell.exe so you won't be able to use the code I posted in my previous reply, at least not if you have multiple powershell windows open. Then you also need a way to specify the window title for it to be able to pick the correct window of course. Assuming you have opened no more than just a single powershell window that is titled Utility1, this version should work:
    Code:
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Function ShowWindow(name, title)
      With WshShell
        ShowWindow = .Run("PowerShell -Ex Bypass -Com ""Add-Type -M '" & _
          "[DllImport(\""user32.dll\"")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);" & _
          "[DllImport(\""user32.dll\"")] public static extern int SetForegroundWindow(IntPtr hWnd);' -Name PS -Names WindowsAPI;" & _
          "$hWnd = (Get-Process " & name & ").Where({$_.MainWindowTitle -Match '" & title & "'}, 1).MainWindowHandle;" & _
          "[WindowsAPI.PS]::ShowWindow($hWnd, 4);[WindowsAPI.PS]::SetForegroundWindow($hWnd);""", 0, True) = 0
      End With
    End Function
    If Not ShowWindow("powershell", "Utility1") Then
      MsgBox "Utility1 not found", vbCritical
      WScript.Quit
    End If
    WShShell.SendKeys ".\utility shutdown 23{Enter}"
    Last edited by hdmi; 06 Nov 2021 at 13:49.
      My Computers


  9. Posts : 52
    windows 10 Pro
    Thread Starter
       #9

    Interesting code. Difficult to to follow for a novice. Will try the code tomorrow.
    Thanks.
    PS not sure why you say that windows AppActivate no longer working. It seems to work over 50% of the time for me.
      My Computer


  10. Posts : 1,201
    11 Home
       #10

    What I meant to say was that AppActivate produces inconsistent results if using it by specifying a window title. You can specify a process ID instead of a window title. (Process ID, not process name, sorry I wrote that wrong.) If, for example, I open one or two textfiles in notepad and I also open one empty notepad, then if I change Utility1 in your code to Untitled - Notepad (Naamloos - Kladblok on my Dutch speaking Windows), then it should activate the empty notepad and send the keys to it, right? As it turns out, it just never works, at least for me it doesn't. A quick search on the internet confirms that even the experienced scripting guys have been having similar troubles with AppActivate over the past several years or maybe longer. Sometimes it just works, but sometimes it refuses to work, and, no one AFAIK seems to have a clear in-depth explanation for this odd type of behavior. So, finally I gave up and took the powershell route out of this strange mess from Mickeysoft. You can try it yourself, by changing the If statement, like so:
    Code:
    If Not ShowWindow("notepad", "Untitled") Then
    Please let me know results as for which script activates the empty notepad correctly for you and which one doesn't.

    - - - Updated - - -

    I did some further testing. If the notepad window that I want to be activated is minimized before it gets activated via WshShell.AppActivate, then the window doesn't accept the keystrokes from SendKeys. However, if the first keystroke from SendKeys is {Enter}, all subsequent keystrokes from SendKeys will be accepted just fine. No empty line will be inserted at the beginning, so then it works perfect. Only problem, if the notepad window is NOT minimized before it gets activated in this manner, then an empty line does get inserted at the beginning. That is, excepting if I leave out this same {Enter} keystroke from SendKeys, in which case the AppActivate works perfect again. So, in a nutshell, using AppActivate before SendKeys can produce different results depending on how the task that owns the window behaves, and potentially also depending on whether the window in question is minimized.
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 15:24.
Find Us




Windows 10 Forums