Announcement

Collapse
No announcement yet.

VBS script on a per user basis that shuts down the computer when i close ie 7

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

  • VBS script on a per user basis that shuts down the computer when i close ie 7

    Hi,

    We have a pc windows XP sp2 and when a user closes ie 7, we want the pc to switch off. I want to add this script to my user container so it only applies to this specific group of users.

    Anyone know a nice script, preferably in vbs?

    regards

    David

  • #2
    Re: VBS script on a per user basis that shuts down the computer when i close ie 7

    Script snippet to check for a running IE process:
    http://www.microsoft.com/technet/scr...6/hey0228.mspx (Thanks ScriptGuys!!)

    Then you can do a shell.run and invoke a shutdown which is shown at the end of the code displayed here, http://forums.techguy.org/software-d...ite-right.html
    Jake G

    Former Microsoft MVP - IIS ('02-'06)

    Comment


    • #3
      Re: VBS script on a per user basis that shuts down the computer when i close ie 7

      Below is a powershell script that binds to the iexplore.exe process once this process is ended shutdown.exe is invoked using the syntax shutdown.exe -s.

      Code:
      $b = 1
      do
      	{
      $a = get-process iexplore.exe
      $a.waitforexit()
      Invoke-Item shutdown.exe -s
      	}
      while ($b -eq 1)
      MCSA 2000/2003

      Comment


      • #4
        Re: VBS script on a per user basis that shuts down the computer when i close ie 7

        For my own knowledge here...

        Won't .waitforexit() loop itself? If so, what's the point of the $b while?
        Jake G

        Former Microsoft MVP - IIS ('02-'06)

        Comment


        • #5
          Re: VBS script on a per user basis that shuts down the computer when i close ie 7

          When you run the powershell script with waitforexit() method, the script waits for the process to stop, in this instance iexplore.exe, when the process is terminated the item invoked will run and the script will then stop. Which is actually I suppose what davegbr requires as then the shutdown process will be invoked, and will not require the powershell script to continue

          I use the powershell script as a monitoring tool for an application that is dependent on a java.exe process, however I require the invoked item to be run each time a process of java.exe is terminated. Therefore, a loop is created where the script will continue to run as long as the variable b$ is equal to 1 'do {} while ($b -eq 1)' and b$ will always be equal to 1 'b$ = 1'

          So I suppose after all that the following would be sufficient:

          Code:
          $a = get-process iexplore.exe
          $a.waitforexit()
          Invoke-Item shutdown.exe -s
          Also, another factor to consider is that the above will fail if more than one instance of iexplore.exe is running.
          Last edited by themanwhowouldbeking; 27th February 2008, 23:26.
          MCSA 2000/2003

          Comment


          • #6
            Re: VBS script on a per user basis that shuts down the computer when i close ie 7

            thanks i am gonna try this and let you all know

            Comment


            • #7
              Re: VBS script on a per user basis that shuts down the computer when i close ie 7

              The following works it doesn't shutdown but restarts the pc which is ok: However there is one prob remaining and maybe wit ha registry hack i can accomplish this. Does anyone know how i can run teh shutdown.exe command under limited user rights, without providing credentials in teh script?? That would solve everything.

              strComputer = "."

              Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
              Set objShell = CreateObject("Wscript.Shell")

              Do While True
              Set colProcesses = objWMIService.ExecQuery _
              ("Select * from Win32_Process Where Name = 'iexplore.exe'")
              If colProcesses.Count = 0 Then
              objShell.Run "shutdown.exe -r -t 0"
              End If
              Loop

              Comment


              • #8
                Re: VBS script on a per user basis that shuts down the computer when i close ie 7

                Could you not run the visual basic script as a scheduled task when the workstation starts using credentials to shutdown the workstation and grant that user object ' SeShutdownPrivilege'.
                MCSA 2000/2003

                Comment


                • #9
                  Re: VBS script on a per user basis that shuts down the computer when i close ie 7

                  Originally posted by davegbr View Post
                  The following works it doesn't shutdown but restarts the pc which is ok: However there is one prob remaining and maybe wit ha registry hack i can accomplish this. Does anyone know how i can run teh shutdown.exe command under limited user rights, without providing credentials in teh script?? That would solve everything.

                  strComputer = "."

                  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
                  Set objShell = CreateObject("Wscript.Shell")

                  Do While True
                  Set colProcesses = objWMIService.ExecQuery _
                  ("Select * from Win32_Process Where Name = 'iexplore.exe'")
                  If colProcesses.Count = 0 Then
                  objShell.Run "shutdown.exe -r -t 0"
                  End If
                  Loop
                  Do not use the command-line tool shutdown.exe to shutdown the computer.
                  The user need "Shut down the system" as wel as "Remote Shutdown" privileges for using this tool. You do not want users on the network to have the latter privilege.

                  Instead you can use WMI (see "Privileges"), and use the Win32Shutdown Method of the Win32_OperatingSystem Class.

                  btw
                  Your script has a pretty active Loop, this can slowdown the computer a lot.
                  I would just trace the shutdown events of iexplore.exe proccess

                  here is an example;

                  Code:
                  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                  '  VBScript: 
                  '    "shutdown the computer when all instances of one process are ended" 
                  '
                  '  Author: Remco Simons [NL]  2008
                  '
                  '  ( http://forums.petri.com/showthread.php?t=21964 ) 
                  '
                  ' extra: you can also set a max time [intTimeOut] for the session
                  '
                  ' Usage:
                  ' for example, you can copy this vbscript or, a shortcut of this vbscript
                  ' to the user's 'startup folder'.
                  '
                  ' Once started, this script keep running - so it can trace certain process events -.
                  ' That why you canNot use this script as a logon script executed by a gpo, but you can
                  ' use a logon-script to copy this vbscript-file to the user's startupfolder under her/his
                  ' profile folder. (see also 'OPTIONALLY' at the end of this script)
                  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                  
                  strProc = "iexplore.exe"
                  
                  intTimeOut =  null  
                        '  null  <--disabled, and e.g. 3 hours--> 6e4*(180)
                  
                  Set objWMIService = GetObject("winmgmts:" _
                   & "{impersonationLevel=impersonate}!\\.\root\cimv2")
                  
                  Set colProcessStopTrace = objWMIService.ExecNotificationQuery _
                   ("SELECT * FROM Win32_ProcessStopTrace Within 3 " _
                        & "Where ProcessName = '" & strProc & "'")
                  
                  on error resume next
                  
                  Do
                    Set objLatestEvent = colProcessStopTrace.NextEvent(intTimeOut)
                  
                    If Err.number <> 0 Then
                      Set oShell = CreateObject("WScript.Shell")
                      oShell.Popup "Session Time is over", _
                               10, "Auto close within 10 seconds!", 0+48+4096
                      ShutDown "Shutdown_Force"
                      Err.Clear
                      Exit Do
                    ElseIf objWMIService.ExecQuery _
                     ("Select * from Win32_Process Where " _
                     & "Name = '" & strProc & "'").count = 0 Then
                      ShutDown "Shutdown_Force"
                      Exit Do
                    End If
                  Loop
                  
                  Set objLatestEvent = Nothing
                  Set colProcessStopTrace = Nothing
                  
                  wscript.quit(0)
                  
                  
                  Sub ShutDown(sAction)
                     '''''''''''''''''''''''''''''''''''''''''''''''''''
                     ' http://forums.techarena.in/showthread.php?t=25249
                     ' Parameter options:
                     '   Use "PowerOff"       for a poweroff
                     '   Use "PowerOff_Force" for a forced poweroff
                     '   Use "Shutdown"       for a shutdown
                     '   Use "Shutdown_Force" for a forced shutdown
                     '   Use "Reboot"         for a reboot
                     '   Use "Reboot_Force"   for a forced reboot
                     '   Use "LogOff"         for a logoff
                     '   Use "LogOff_Force"   for a forced logoff
                     '''''''''''''''''''''''''''''''''''''''''''''''''''
                     Const EWX_LOGOFF   = 0
                     Const EWX_REBOOT   = 2
                     Const EWX_SHUTDOWN = 1
                     Const EWX_POWEROFF = 8
                     Const EWX_FORCE    = 4
                     Select Case LCase(sAction)
                       Case "logoff"
                         iCmd = EWX_LOGOFF
                       Case "logoff_force"
                         iCmd = EWX_LOGOFF + EWX_FORCE
                       Case "shutdown"
                         iCmd = EWX_SHUTDOWN
                       Case "shutdown_force"
                         iCmd = EWX_SHUTDOWN + EWX_FORCE
                       Case "reboot"
                         iCmd = EWX_REBOOT
                       Case "reboot_force"
                         iCmd = EWX_REBOOT + EWX_FORCE
                       Case "poweroff"
                         iCmd = EWX_POWEROFF
                       Case "poweroff_force"
                         iCmd = EWX_POWEROFF + EWX_FORCE
                       Case Else
                         MsgBox "Error: invalid input parameter!", _
                           vbExclamation + vbSystemModal, "ShutDown"
                         iCmd = Empty
                     End Select
                     
                     If not iCmd = "" then
                        On Error Resume Next
                        Set oWMI = GetObject("winmgmts:" _
                           & "{impersonationLevel=impersonate,(Shutdown)}!" _
                           & "\\.\root\cimv2")
                  
                        Set colOperatingSystems = oWMI.ExecQuery _
                           ("Select * from Win32_OperatingSystem")
                  
                        For Each oOS in colOperatingSystems
                           oOS.Win32Shutdown(iCmd)
                        Next
                  
                        Set colOperatingSystems = Nothing
                     End If
                  End Sub
                  
                  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                  ' OPTIONALLY:
                  '  you could add a few lines of code to this vbscript, just before the do-loop starts,
                  '  so it can remove it self from the user's startup folder once the script is loaded
                  '  that is if you planned to use a logon-script to copy this file to the startup folder every logon.
                  '
                  '  Additionally: 
                  '   This same vbscript can be modified to perform as a two step script, so it can start and close 
                  '   as a logonscript too..
                  '   - During logon it checks it own run path
                  '    code: Set WSHShell = CreateObject("WScript.Shell")
                  '    code: ScriptPath = LCase(WScript.ScriptFullName)
                  '    code: StartupPath = LCase(wshshell.SpecialFolders("Startup")) 
                  '    code: Scriptname = LCase(WScript.ScriptName)
                  '    code: If NOT ScriptPath = (StartupPath & "\" & Scriptname) Then
                  '      -> if it is not the user'startup folder then,
                  '     It proceed with the logonscript section of the script:
                  '     the useraccount is checked (for its group membership), to know whether it should copy 
                  '     or delete (if exist) it self from the user's startup folder. 
                  '     Then the logonscript part ends the script.
                  '    code: wscript.quit
                  '   - During autorun it checks it own run path
                  '    code: Else
                  '      -> if it is the user'startup folder then,
                  '    code: End If
                  '     It proceed with the autorun section of the script:
                  '     The script removes itself again from the user's start up folder, but does not end the script! 
                  '     The process trace starts.
                  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                  note!
                  Users are able to end the 'wscript.exe' process of this script once it is stated.
                  Or, if the script autoruns from the user's startup folder, he/she can prevent the execution by pressing the Shift-key during logon.

                  \Rems


                  - EDIT -
                  You can test the script by copy it to the user's startup folder on the local computer.
                  The script above is as it is now, is not jet ready to use as logon script executed by a gpo. That is because logonscripts must start and also finish during logon to proceed the user session.
                  It needs extra code. And also, it must perform some kind of a useraccount check.

                  ( Or maybe to prevent users from stopping the process manually, it might (?) be possible
                  to run this vbsript as a service (by using SRVANY.EXE)
                  note: after using instsrv.exe to install the script as a service,
                  you must also edit some registry Entries in the 'Parameters' subkey
                  of the new service key, found under hklm\SYSTEM\CurrentControlSet\Services.
                  --> "Application"="C:\WINDOWS\system32\cscript.exe "
                  --> "AppParameters"="c:\scriptdirectory\scriptname.vbs "
                  --> "AppDirectory"="c:\scriptdirectory"
                  [not tested]
                  )
                  Last edited by Rems; 29th February 2008, 11:26. Reason: see -EDIT- || and see OPTIONALLY: in the script

                  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


                  • #10
                    Re: VBS script on a per user basis that shuts down the computer when i close ie 7

                    thanx rems i will try that

                    Comment


                    • #11
                      Re: VBS script on a per user basis that shuts down the computer when i close ie 7

                      REms thank you your script was less strenuous on the computer thx a lot!

                      Comment


                      • #12
                        Re: VBS script on a per user basis that shuts down the computer when i close ie 7

                        Thank you David, we always most appreciate OP's feedback.

                        Glad I could help.
                        \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

                        Working...
                        X