Announcement

Collapse
No announcement yet.

Issue with RDP and VBScript

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Issue with RDP and VBScript

    This is my first post so, first of all, hi everybody.


    I've scheduled a VBScript to run every few minutes to check for the presence
    of a process running.
    There must be ONLY 1 INSTANCE of this process executing and this instance
    must be executed INSIDE THE LOCAL SESSION of a Windows 2003 server.

    When the script executes, it must check for the presence of this process.
    If it fails to find an instance it throws a new instance.
    If it finds just one instance inside the local session it must do nothing.
    If it find more than one instance it must kill non local instances (the
    server can be accessed at the same time by a couple of people by means of RDP)
    My assumption is that local session has always session id=0, while RDP
    session #1 and RDP session #2 have session id 1 and 2.
    Unfortunately, this assumption is far from being always true.

    Sometimes local console has session id=1 so script fails; some others RDP
    session #2 has session id=4 and so forth.

    Is there a way to discriminate between local session and RDP session without
    the chance to make a mistake?

    Before attaching the code I wanna tell that it would be great if the script
    could also realize if a process is blocked and take care of relaunch it.

    The code follows:
    Moderator Edit: Added [Code]-tags around the code, corrected unexpected line breaks
    Code:
     
    Option Explicit
    
    Dim objWMIService, objProcess, colProcess
    Dim ActiveAcquisitionsConsole, ActiveAcquisitionsSession1, ActiveAcquisitionsSession2
    
    Dim objWMIServiceNewInstance, objProcessNewInstance
    Dim strShellNewInstance, objProgramNewInstance, strComputerNewInstance, strExeNewInstance
    
    Dim objWMIService1, objProcess1, colProcess1
    Dim objWMIService2, objProcess2, colProcess2
    
    
    Dim strComputer, strAcquisitionProcess
    strComputer = "."
    strAcquisitionProcess = "'MySoftware.exe'"
    
    Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" _
      & strComputer & "\root\cimv2")
    
    
    'Search for MySoftware.exe instances inside the local console
    Set colProcess = objWMIService.ExecQuery _
      ("Select * from Win32_Process Where Name = " & strAcquisitionProcess )
    
    
    
    ActiveAcquisitionsConsole=0
    
    For Each objProcess in colProcess
      ActiveAcquisitionsConsole= ActiveAcquisitionsConsole + 1
    Next
    
    
    Set objWMIService1 = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" _
      & strComputer & "\root\cimv2")
    
    ' Search for MySoftware.exe instances inside
    ' the first RDP session  (assuming this session id=1)
    Set colProcess1 = objWMIService1.ExecQuery("Select Name from " _
      & "Win32_Process Where Name='MySoftware.exe' and SessionID = 1" )
    
    
    ActiveAcquisitionsSession1= 0
    For Each objProcess1 in colProcess1
      ActiveAcquisitionsSession1= ActiveAcquisitionsSession1 + 1
    Next
    
    
    ' Kill every instance of MySoftware.exe inside
    ' the first RDP session  (assuming this session id=1)
    For Each objProcess1 in colProcess1
      objProcess1.Terminate()
    Next
    
    
    Set objWMIService2 = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" _
      & strComputer & "\root\cimv2")
    
    ' Search for MySoftware.exe instances inside
    ' the second RDP session  (assuming this session id=2)
    
    Set colProcess2 = objWMIService2.ExecQuery("Select Name from " _
      & "Win32_Process Where Name='MySoftware.exe' and SessionID = 2" )
    
    
    ActiveAcquisitionsSession2= 0
    For Each objProcess2 in colProcess2
      ActiveAcquisitionsSession2= ActiveAcquisitionsSession2 + 1
    Next
    
    ' Kill every instance of MySoftware.exe inside
    ' the second RDP session  (assuming this session id=2)
    For Each objProcess2 in colProcess2
      objProcess2.Terminate()
    Next
    
    
    'No instances of MySoftware.exe on local console -> let's launch an instance
    if ActiveAcquisitionsConsole = 0 Then
    
      strComputerNewInstance = "."
      strExeNewInstance = "MyPathFolder\MySoftware.exe"
      ' Connect to WMI
      set objWMIServiceNewInstance = getobject("winmgmts://"_
        & strComputerNewInstance & "/root/cimv2")
    
    
      Set objProcessNewInstance = objWMIServiceNewInstance.Get("Win32_Process")
      Set objProgramNewInstance = objProcessNewInstance.Methods_( _
        "Create").InParameters.SpawnInstance_
      objProgramNewInstance.CommandLine = strExeNewInstance
    
    
      Set strShellNewInstance = objWMIServiceNewInstance.ExecMethod( _
        "Win32_Process", "Create", objProgramNewInstance)
    
    End if
    
    WScript.Quit
    
    Any help will be welcome.
    Last edited by Rems; 24th August 2009, 12:17.

  • #2
    Re: Issue with RDP and VBScript

    Any suggestion?

    Comment


    • #3
      Re: Issue with RDP and VBScript

      What are you trying to achieve anyway? I mean, why is it important to have just a single process running on local session?

      You could implement the program as service. Or ask the original developer to implement the limitations.

      -vP

      Comment


      • #4
        Re: Issue with RDP and VBScript

        Originally posted by vonPryz View Post
        What are you trying to achieve anyway? I mean, why is it important to have just a single process running on local session?

        You could implement the program as service. Or ask the original developer to implement the limitations.

        -vP
        I've already explained what I'm trying to do.

        Console session -> always 1 instance of this process must run here. Any instance? yes: ignore it, no: relaunch the process

        Remote Session #any -> no instance of this process should run here. Any instance? yes: kill it, no: ignore it

        I've talked with the developer. He said neither he cannot implement the program as as a service not can put this limitation

        Comment

        Working...
        X