#75591 - 2003-07-03 03:33 AM
Automatic shut down
|
Gaven
Starting to like KiXtart
Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
|
Here's the deal, I'm trying to write a script that will run as a scheduled task. When run, the script checks to see if anybody is logged on. If nobody is logged on, the computer shuts down. I am having no problem using WMI to determine if a user is logged on, my problem is that the shutdown command returns an error of 53. Note: I am testing with restart and not shutdown. Note 2: The task is running with a UserName that is a member of the Administrators group so I don't think the problem stems from an access error.
code:
$a=GetObject("winmgmts:\root\cimv2").ExecQuery("Select * from Win32_ComputerSystem") For Each $b In $a If $b.UserName="" $c=Shutdown('','',0,0,1) EndIf Next $a=0
|
|
Top
|
|
|
|
#75592 - 2003-07-03 04:13 AM
Re: Automatic shut down
|
Sealeopard
KiX Master
   
Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
|
Trycode:
$a=GetObject('winmgmts:{impersonationLevel=impersonate, (Shutdown)}!\\.\root\cimv2'). ExecQuery('Select * from Win32_ComputerSystem') ? 'Error '+@ERROR+' - '+@SERROR For Each $b In $a If $b.UserName="" $c=Shutdown('','',0,0,1) EndIf Next $a=0
However, for better error control, I'd rather first create the object to see whether the permissions are correct, then apply the WQL query on the object.
See also the FAQ Forum under fnWMILoggedIn() - Retrieves users/computers connected to a computer and fnWMIShutdown() - LogOff/reboot/shutdown a user/computer locally or remotely [ 03. July 2003, 15:06: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.
|
|
Top
|
|
|
|
#75608 - 2003-07-04 08:36 AM
Re: Automatic shut down
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Gaven,
Have you done some digging around the web on this?
Constructing a Moniker String
You may also get some information from Google Groups as well.
HTH,
Kent
|
|
Top
|
|
|
|
#75609 - 2003-07-04 10:52 PM
Re: Automatic shut down
|
Gaven
Starting to like KiXtart
Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
|
Are you kidding? I spend about 6 hours searching this site and over a day total in the MSDN. In total I have probably made over 100 attempts to find a working solution trying different methods. However, to answer your question more specifically, when I was still trying to use WMI to shutdown the computer (I gave up when it repeatedly returned the same result as the KiXtart Shutdown function) I made some attempts to use SWbemLocator instead of the monkier. However, I ran into a serious error when calling 'Win32Shutdown(2)' which causes KiXtart to abruptly terminate. As it does appear to be a moot point, I would prefer that people don't mess with this code and post responses to it but here is the code I attempted to shutdown the computer with using WMI and SWbemLocator:code:
$objLocator=CreateObject('WbemScripting.SWbemLocator') $objWBEM=$objLocator.ConnectServer(@Wksta,'root\CIMV2') $objWBEM.Security_.ImpersonationLevel=3 $objShutdown=$objWBEM.ExecQuery('Select * from Win32_OperatingSystem') For Each $objOpSys in $objShutdown $wmirestart=$objOpSys.Win32Shutdown(2) Next $objShutdown=0 $objWBEM=0 $objLocator=0
Has anybody out there tried that original code to see if it will run on another network? If you are willing to help me out, please read all the posts above before doing anything. [ 06. July 2003, 15:34: Message edited by: Gaven ]
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 1662 anonymous users online.
|
|
|