Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

logoff script to close a program gracefully before user logs out not working

Posted on 2007-10-20
14
Medium Priority
?
2,488 Views
Last Modified: 2013-11-21
I have written a script to close a program gracefully when a users logs off of my terminal server so that it doe not generate errors in its database. I have set the system to execute this script during logoff. The problem is that the logoff sequence actually closes the program before my script has a chance to run.

I need a way to run a logout script that is triggered by a logoff event but runs before the logoff proccess continues. With some reseach I found this microsoft link which references a gpo option to "Run logoff scripts synchronously"

http://technet2.microsoft.com/windowsserver/en/library/32571e90-831b-4e88-8aa7-d6999f291c6d1033.mspx?mfr=true

If anyone has found microsofts missing GPO let me know. (good job MS ;))

I am really stuck an appeciate any Ideas people have.

Darkstriiker69
0
Comment
Question by:Darkstriker69
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 58

Expert Comment

by:Pete Long
ID: 20114981
0
 
LVL 5

Author Comment

by:Darkstriker69
ID: 20115282
Thanks for the link Pete, It actually looks like the windows server 2000 version of the link I provided in the question. I did not see any additional information on where to find "Run logoff scripts synchronously" or how to overcome my issue in another fashion.

Let me know if I have missed something.

Darkstriker69
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20116885
Try this article.....it talks about waiting for UserInit to finish first....
http://www.microsoft.com/technet/scriptcenter/topics/gp/extension2.mspx

Regards,

Rob.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 5

Author Comment

by:Darkstriker69
ID: 20117945
Again, A good link. It gave me alot of information about the UserInit process, but I didnt see any information about how to change the UnerInit scripting settings.

Let me know if I missed that information.

I am really looking for a usable setting change or script alteration to overcome my problem

Thanks,

Darkstriker69
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20118535
Can you post your code to understand more?"
0
 
LVL 5

Author Comment

by:Darkstriker69
ID: 20118539
pauseperiod = 1000 ' time in milliseconds
set objShell = CreateObject ("WScript.Shell")

If  objShell.AppActivate("CATS") Then
    objShell.SendKeys "{ESC}"
    WScript.Sleep pauseperiod
    objShell.SendKeys "{ESC}"
    WScript.Sleep pauseperiod
    objShell.SendKeys "{ESC}"
    WScript.Sleep pauseperiod
    objShell.SendKeys "{ESC}"
    WScript.Sleep pauseperiod
    objShell.SendKeys "{ESC}"
    WScript.Sleep pauseperiod
    objShell.SendKeys "Y"
    WScript.Sleep pauseperiod
    objShell.SendKeys "{ENTER}"
    WScript.Sleep pauseperiod

End If

If objShell.AppActivate("CATS") Then
    objShell.SendKeys "{ESC}"
    WScript.Sleep pauseperiod
    objShell.SendKeys "Y"
    WScript.Sleep pauseperiod
    objShell.SendKeys "{ENTER}"
End If
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20118549
What does this script actually do? Is this script used to close a program before logoff or can you explain a bit more about the scenario?
0
 
LVL 5

Author Comment

by:Darkstriker69
ID: 20118568
Yes, thats pretty much exactly it.

The script litterally brings a program that uses the name "CATS" into the forground if it is running, then types "ESC" into the program enough times to bring up the exit menu of the program and then types the charactor "Y" into the program to exit it gracefully. The script itself works perfectly when run manually. It just executes after windows closes the program if its executed as a logoff script.

So, I figure I need to either run the script before the logoff takes place but it still needs to be triggered by a logoff event. Apperently this is a tall order.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20119338
Hmmmm, just quickly....I'm thinking that instead of using the ordinary logoff.....create a batch file, that runs this script, then have either the Shutdown or Logoff DOS command run after that, so that *you* initiate the logoff......

You could even prompt wheter to log off or shutdown.....

'=================
pauseperiod = 1000 ' time in milliseconds
set objShell = CreateObject ("WScript.Shell")

strMessage = "Please enter your option:" & VbCrLf & _
      "1: Logoff" & VbCrLf & _
      "2: Restart the computer" & VbCrLf & _
      "3: Shutdown the computer" & VbCrLf & _
      "4: Cancel"

intResponse = 0
While intResponse < 1 And intResponse > 4
      intResponse = InputBox(strMessage, "What do you want to do?")
Wend

' Now a valid entry has been entered, but we don't need to check
' for 4 being selected....just do nothing if it is 4.
If intResponse >= 1 And intResponse <= 3 Then

      If  objShell.AppActivate("CATS") Then
          objShell.SendKeys "{ESC}"
          WScript.Sleep pauseperiod
          objShell.SendKeys "{ESC}"
          WScript.Sleep pauseperiod
          objShell.SendKeys "{ESC}"
          WScript.Sleep pauseperiod
          objShell.SendKeys "{ESC}"
          WScript.Sleep pauseperiod
          objShell.SendKeys "{ESC}"
          WScript.Sleep pauseperiod
          objShell.SendKeys "Y"
          WScript.Sleep pauseperiod
          objShell.SendKeys "{ENTER}"
          WScript.Sleep pauseperiod

      End If
      
      If objShell.AppActivate("CATS") Then
          objShell.SendKeys "{ESC}"
          WScript.Sleep pauseperiod
          objShell.SendKeys "Y"
          WScript.Sleep pauseperiod
          objShell.SendKeys "{ENTER}"
      End If
      
      Select Case intResponse
            Case 1
                  strCommand = "logoff"
            Case 2
                  strCommand = "shutdown /r"
            Case 3
                  strCommand = "shutdown /s"
      End Select
      
      Set objShell = CreateObject("WScript.Shell")
      objShell.Run strCommand, 0, False
      
End If
'=================

Regards,

Rob.
0
 
LVL 5

Author Comment

by:Darkstriker69
ID: 20119431
I think were getting closer, but the Idea here is that with terminal server you want to set a timout for inactive sessions. So when the timeout is reached, the system logs off the inactive user. This unfortunatly means the logoff event will be triggered by the system and not an actual user.

Is there a way to use the form_unload class or something simmilar from VBScript?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 1500 total points
ID: 20119676
Is this CATS program a custom VB6 program?  If so, then yes, you can try something like this...I can't remember exactly which event catches that, but maybe....

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'Unload modes
'0 The user has chosen the Close command from the Control-menu box on the form.
'1 The Unload method has been initiated from code.
'2 Windows is logging off.
'3 Task Manager is closing the application.
'4 An MDI child form is closing because the MDI form is closing.
Select Case UnloadMode
Case 2
'Windows is logging off. System initiated close
'Do your stuff here
Case Else
Debug.Print "Unload mode: " & CStr(UnloadMode)
End Select
End Sub


Regards,

Rob.
0
 
LVL 5

Author Comment

by:Darkstriker69
ID: 20119719
CATS is a existing DOS program my client is currently using.
But, you may be close to the solution I need. I have considered writing a VB 6 program that would open CATS and detect if there was a lofoff event, stops the logoff event, exits CATS then runs its own logoff.

Unfortunatly I am not a programmer so I am struggling mightely

Is there any way of detecting a windows logoff through vbscript, so that I could detect a logoff starting and quickly run my commands?
0
 
LVL 5

Author Comment

by:Darkstriker69
ID: 20128209
Well, After bangng my head against the wall until what was left of my brain fell out (my, thats graphic) I have wrote a short VB 2005 program to do what I need to do.



Public Class CatsLoad

    Private Sub CatsLoad_BackgroundImageLayoutChanged(ByVal sender As Object, ByVal e As_ System.EventArgs) Handles Me.BackgroundImageLayoutChanged

    End Sub

    Private Sub CatsLoad_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        Dim objshell = CreateObject("WScript.Shell")
        Dim pauseperiod As Long = 50 ' time in milliseconds


        If objshell.AppActivate("CATS") Then
            System.Threading.Thread.Sleep(pauseperiod)
            objshell.SendKeys("{ESC}")
            System.Threading.Thread.Sleep(pauseperiod)
            objshell.SendKeys("{ESC}")
            System.Threading.Thread.Sleep(pauseperiod)
            objshell.SendKeys("{ESC}")
            System.Threading.Thread.Sleep(pauseperiod)
            objshell.SendKeys("{ESC}")
            System.Threading.Thread.Sleep(pauseperiod)
            objshell.SendKeys("{ESC}")
            System.Threading.Thread.Sleep(pauseperiod)
            objshell.SendKeys("Y")
            System.Threading.Thread.Sleep(pauseperiod)
            objshell.SendKeys("{ENTER}")

        End If

        If objshell.AppActivate("CATS") Then
            objshell.SendKeys("{ESC}")
            System.Threading.Thread.Sleep(pauseperiod)
            objshell.SendKeys("Y")
            System.Threading.Thread.Sleep(pauseperiod)
            objshell.SendKeys("{ENTER}")

        End If

    End Sub

    Private Sub CatsLoad_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        System.Diagnostics.Process.Start("CATS")

        Me.Width = 3
        Me.Height = 3

    End Sub

End Class



You have to make sure this program stays the top windows open so that it closes first duing logoff. This is easily accomplished with the VB design tab.

Because it was VB 2005 I guess the Form_QueryUnload didnt exist anymore but Rob definitely
got me on the right track so I am awarding the answer to him.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20128229
Oh yeah!  I was thinking of the same thing, but wouldn't have caught on to making it have the focus first....very well done!  Nice one.

Regards,

Rob.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question