Petri.co.il forums Home Forums Start Page Forums Frequently Asked Questions FAQ Member List Members List
Go Back   Petri IT Knowledgebase Forums > Windows Scripting > General Scripting
Petri.co.il is happy to award RicklesP the title of Most Valuable Member !!!
Register Calendar Calendar Search Petri IT Knowledgebase Forums Search Todays Posts Today's Posts Mark Forums Read
Notices

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

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

this thread has 11 replies and has been viewed 6064 times

Closed Thread
 
Thread Tools Search this Thread Display Modes
  #1  
Old 27th February 2008, 17:02
davegbr davegbr is offline
Casual
Casual
 
 Join Date: Feb 2008
  6 month star 12 month star
 Posts: 20
 Reputation: davegbr is on a distinguished road (10)
Default 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  
Old 27th February 2008, 19:33
JakeG JakeG is offline
Casual
Casual
 
 Join Date: Feb 2008
  6 month star 12 month star
 Location: Philly Area
 Posts: 55
  Send a message via AIM to JakeG
 Reputation: JakeG is on a distinguished road (37)
Default 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)
  #3  
Old 27th February 2008, 20:32
themanwhowouldbeking's Avatar
themanwhowouldbeking themanwhowouldbeking is offline
Casual
It's not a coincidence
 
 Join Date: Jun 2007
  6 month star 12 month star
 Location: United Kingdom
 Posts: 73
 Send a message via ICQ to themanwhowouldbeking Send a message via AIM to themanwhowouldbeking Send a message via MSN to themanwhowouldbeking Send a message via Yahoo to themanwhowouldbeking
 Reputation: themanwhowouldbeking is on a distinguished road (23)
Default 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
  #4  
Old 27th February 2008, 23:01
JakeG JakeG is offline
Casual
Casual
 
 Join Date: Feb 2008
  6 month star 12 month star
 Location: Philly Area
 Posts: 55
  Send a message via AIM to JakeG
 Reputation: JakeG is on a distinguished road (37)
Default 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)
  #5  
Old 28th February 2008, 01:23
themanwhowouldbeking's Avatar
themanwhowouldbeking themanwhowouldbeking is offline
Casual
It's not a coincidence
 
 Join Date: Jun 2007
  6 month star 12 month star
 Location: United Kingdom
 Posts: 73
 Send a message via ICQ to themanwhowouldbeking Send a message via AIM to themanwhowouldbeking Send a message via MSN to themanwhowouldbeking Send a message via Yahoo to themanwhowouldbeking
 Reputation: themanwhowouldbeking is on a distinguished road (23)
Default 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.
__________________
MCSA 2000/2003

Last edited by themanwhowouldbeking; 28th February 2008 at 01:26..
  #6  
Old 28th February 2008, 10:50
davegbr davegbr is offline
Casual
Casual
 
 Join Date: Feb 2008
  6 month star 12 month star
 Posts: 20
 Reputation: davegbr is on a distinguished road (10)
Default 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
  #7  
Old 28th February 2008, 15:04
davegbr davegbr is offline
Casual
Casual
 
 Join Date: Feb 2008
  6 month star 12 month star
 Posts: 20
 Reputation: davegbr is on a distinguished road (10)
Default 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
  #8  
Old 28th February 2008, 15:16
themanwhowouldbeking's Avatar
themanwhowouldbeking themanwhowouldbeking is offline
Casual
It's not a coincidence
 
 Join Date: Jun 2007
  6 month star 12 month star
 Location: United Kingdom
 Posts: 73
 Send a message via ICQ to themanwhowouldbeking Send a message via AIM to themanwhowouldbeking Send a message via MSN to themanwhowouldbeking Send a message via Yahoo to themanwhowouldbeking
 Reputation: themanwhowouldbeking is on a distinguished road (23)
Default 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
  #9  
Old 29th February 2008, 00:25
Rems's Avatar
Rems Rems is offline
Moderator
 
 Join Date: Mar 2005
  6 month star 12 month star
 Location: NL
 Posts: 2,406
 Reputation: Rems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to behold (944)
Default Re: VBS script on a per user basis that shuts down the computer when i close ie 7

Quote:
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://www.petri.co.il/forums/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 at 13:26.. Reason: see -EDIT- || and see OPTIONALLY: in the script
  #10  
Old 29th February 2008, 17:14
davegbr davegbr is offline
Casual
Casual
 
 Join Date: Feb 2008
  6 month star 12 month star
 Posts: 20
 Reputation: davegbr is on a distinguished road (10)
Default Re: VBS script on a per user basis that shuts down the computer when i close ie 7

thanx rems i will try that
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can the end-user see the vbs login script? JDMils GPO 3 4th October 2007 00:10
Close running program via script and windows GPO Michael19ave General Scripting 2 20th July 2007 21:13
VBS error when running vbs script in a web page neo3114 General Scripting 2 21st February 2007 23:37
User Support VBS Script VortexICS General Scripting 4 10th June 2006 19:08
VBS - IF computer name begins with XXX, run script. AndyBoot General Scripting 2 1st March 2006 15:25


All times are GMT +3. The time now is 06:43.

Steel Blue 3.5.4 vBulletin Style ©2006 vBEnhanced
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
 

Valid XHTML 1.0!   Valid CSS!

Copyright 2005 Daniel Petri