+ Post New Thread
Results 1 to 12 of 12
Windows 7 Thread, Script to query if process is running in Technical; Need a script which can query if a process is running (in this case mstsc.exe) and if not then start ...
  1. #1

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,810
    Thank Post
    272
    Thanked 1,135 Times in 1,031 Posts
    Rep Power
    349

    Script to query if process is running

    Need a script which can query if a process is running (in this case mstsc.exe) and if not then start it - i've used the filter on tasklist before but i can't qgrep on windows 7 as it doesn't work with it.

    Code:
    tasklist /FI "IMAGENAME eq mstsc.exe" | qgrep mstsc.exe
    if ERRORLEVEL 1 (start mstsc.exe C:\rdpfile\studentterminalserver.RDP /f /public) ELSE (goto:EOF)
    anybody got another script which will do the same? maybe powershell?

    its going to be run on a windows thin pc.

  2. #2

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Just doing it like this "should" work, but might want to test it

    Code:
    Set WshShell = WScript.CreateObject ("WScript.Shell")
    set service = GetObject ("winmgmts:")
    pFound = False
    
    for each Process in Service.InstancesOf ("Win32_Process")
    	If Process.Name = "mstsc.exe" then
              pFound = True
    	End If
    next
    
    If pFound = False then
    WshShell.Run ("pathblahblah")
    end if
    Steve

  3. #3

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,810
    Thank Post
    272
    Thanked 1,135 Times in 1,031 Posts
    Rep Power
    349
    it doesn't work! just errors!

  4. #4

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Quote Originally Posted by glennda View Post
    it doesn't work! just errors!
    Care to expand? :P On what? Error message?

    Steve

  5. #5

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,810
    Thank Post
    272
    Thanked 1,135 Times in 1,031 Posts
    Rep Power
    349
    the error is expected end on line 14 but if you put end on the end it then is expecting if

  6. #6

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    You sure it copied right? No error here. And you don't even have a line 14 on my script Can you paste what you're using.

    Steve

  7. #7

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,240
    Thank Post
    1,058
    Thanked 1,068 Times in 625 Posts
    Rep Power
    740
    Autoit:

    Code:
    If ProcessExists("notepad.exe") Then
        MsgBox(0, "Example", "Notepad is running.")
    EndIf

  8. #8

    Join Date
    Jan 2006
    Location
    Surburbia
    Posts
    2,178
    Thank Post
    74
    Thanked 307 Times in 243 Posts
    Rep Power
    115
    Assuming it exists, you can just use find for the same effect e.g.

    tasklist /FI "IMAGENAME eq mstsc.exe" | find /I "mstsc.exe"
    if ERRORLEVEL 1 (start mstsc.exe C:\rdpfile\studentterminalserver.RDP /f /public) ELSE (goto:EOF)

    There's also findstr that would work, albeit with another switch in front of the search string to say it's a literal.

  9. #9

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,810
    Thank Post
    272
    Thanked 1,135 Times in 1,031 Posts
    Rep Power
    349
    Quote Originally Posted by Steve21 View Post
    You sure it copied right? No error here. And you don't even have a line 14 on my script Can you paste what you're using.

    Steve
    Code:
    Set WshShell = WScript.CreateObject ("WScript.Shell")
    set service = GetObject ("winmgmts:")
    pFound = False
    
    for each Process in Service.InstancesOf ("Win32_Process")
    	If Process.Name = "mstsc.exe" then
              pFound = True
    	End If
    next
    
    If pFound = False then
    WshShell.Run ("mstsc.exe C:\rdpfile\studentterminalserver.rdp /f /public")

  10. #10

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    You're missing the endif

    Code:
    If pFound = False then
    WshShell.Run ("mstsc.exe C:\rdpfile\studentterminalserver.rdp /f /public")
    Should be:

    Code:
    If pFound = False then
    WshShell.Run ("mstsc.exe C:\rdpfile\studentterminalserver.rdp /f /public")
    End If
    Can you try that?

    Steve

  11. #11

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,810
    Thank Post
    272
    Thanked 1,135 Times in 1,031 Posts
    Rep Power
    349
    Quote Originally Posted by Steve21 View Post
    You're missing the endif

    Code:
    If pFound = False then
    WshShell.Run ("mstsc.exe C:\rdpfile\studentterminalserver.rdp /f /public")
    Should be:

    Code:
    If pFound = False then
    WshShell.Run ("mstsc.exe C:\rdpfile\studentterminalserver.rdp /f /public")
    End If
    Can you try that?

    Steve
    may bad - that works!

  12. #12

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Shiny shinies

    Steve

SHARE:
+ Post New Thread

Similar Threads

  1. script to check virtual machine is running
    By RabbieBurns in forum Scripts
    Replies: 4
    Last Post: 21st January 2010, 10:49 PM
  2. script to check if folder is present
    By ful56_uk in forum Scripts
    Replies: 7
    Last Post: 25th July 2009, 11:45 AM
  3. Script to check if webpage/Eportal is working?
    By burgemaster in forum Scripts
    Replies: 8
    Last Post: 12th June 2009, 02:06 PM
  4. Script to check if app is running ...
    By pooley in forum Windows
    Replies: 4
    Last Post: 5th January 2009, 11:31 AM
  5. Replies: 3
    Last Post: 1st March 2006, 12:08 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •