Announcement

Collapse
No announcement yet.

VB Script objshell.run wait issue

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

  • VB Script objshell.run wait issue

    Hey all,

    I'm having some trouble with a VB script I'm working on.

    The script along with a hack to the desk will, open a URL that will kick off a VB script, that will then parse the link, report on the activity to a MS SQL Server, open a RDP session, and then once the session is close, report that to MS SQL as well. That is the general Idea.

    The issue I'm having is that I can't get the script to wait for the RDP session to close before it reports the closure to the database.

    So what should happen is Logon -> Log to SQL -> Open RDP -> Wait for RDP to close -> Log to SQL -> end program.

    Here is the code:
    Code:
    'Insert a record into SQL/Kitchen Sink and then close immediately.
    timestamp = now
    adoname.open strSQLConnection
    adostr="usp_RDPInsert '" & rusername & "','" & rcomputername & "','" & timestamp & "','" & rdpname & "','" & rdpserver & "', " & consolemode 
    adoname.execute(adostr)
    adoname.Close
    
    'Open RDP
    intreturn = WshShell.Run("%systemroot%\system32\mstsc.exe " & "/v: " & rdpserver, 1, TRUE)
    msgbox(intreturn)
    
    'Update record in SQL/Kitchen Sink to show the closing of the connection 
    adoname.open strSQLConnection
    adostr="usp_RDPUpdate '" & now & "','" & rusername & "','" & rcomputername & "','" & timestamp & "','" & rdpname & "'"
    adoname.execute(adostr)
    msgbox(adostr)
    adoname.Close
    So what I would expect to happen is the WshShell.Run(Command,1,TRUE) to cause the program to wait until the Running COmmand exits then continue. Instead it doesn't, I've been testing this with the msgbox(adostr).

    Any suggestions?

    Robert
    blog: randomtechminutia.wordpress.com

  • #2
    Re: VB Script objshell.run wait issue

    In this sample the script starts Windows Calculator and "wait" until the process is closed. The PID of the started process is monitored.

    Code:
    strProc = "calc.exe"
    
    strCMDline = strProc & " /v: " & rdpserver
    
    strComputer = "."
    Set objSWbemServices = GetObject _
      ("winmgmts:{impersonationLevel=impersonate}!\\"  _
      & strComputer & "\root\cimv2")
    
    ' start the the application
    errReturn = objSWbemServices.Get("win32_process").Create _
       (strCMDline, null, null, intProcessID)
    
    ' Initiate monitoring this process
    intTimeOut = -1  '--> no timeout.  or....,
    ' intTimeOut = 6e4*(360)  '--> '360 minutes' monitoring time_out
    
    Set objSWbemEventSource = objSWbemServices.ExecNotificationQuery _
        ("Select * From __InstanceDeletionEvent " _ 
        & "Within 2 Where TargetInstance ISA 'Win32_Process'" _
        & "AND TargetInstance.ProcessID = '" & intProcessID & "'")
    Set objSWbemServices = Nothing  
    
    If errReturn = 0 then
    
    ' notify me when process is up-'n-running
      With CreateObject("Wscript.Shell")
        While .AppActivate(intProcessID) = FALSE
        Wend
       .popup Now() & " process " & strProc & " is succesfully started",2
      End With'
    
      On Error Resume Next
    
      Do Until IsStopped = True
        Set objSWbemObject = objSWbemEventSource.NextEvent(intTimeOut)
        If Err.number <> 0 Then
         WScript.Echo Err.Description
         Err.Clear : Exit Do
    
        Else
         If objSWbemObject.TargetInstance.ProcessID _
          = intProcessID Then IsStopped = True
        End If
      Loop
    
      On Error goto 0
    
       If IsStopped = True Then
          WScript.Echo Now()," process:", strProc,"is terminated"
       End If
    
    Else
      WScript.Echo Now()," There was a problem starting process: ",strProc
    End If
    
    wscript.quit
    /Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: VB Script objshell.run wait issue

      This was exactly what I needed. I found similar code over at the Scripting Guys website as well and was able to recreate this.

      Thanks a lot for your help!

      Robert

      Comment

      Working...
      X