Page 1 of 2 12>
Topic Options
#75591 - 2003-07-03 03:33 AM Automatic shut down
Gaven Offline
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 Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Try
code:
$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
#75593 - 2003-07-03 05:10 AM Re: Automatic shut down
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
I appreciate the effort seal but that does help me. As I said, I am NOT having a problem with using WMI to see if a person is logged on. I am having a problem with the shutdown command. The value of $c becomes 53, help me get it to 0.
Top
#75594 - 2003-07-03 05:15 AM Re: Automatic shut down
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Have you tried hardcoding the "computer" parm just as a test ?

-Shawn

Top
#75595 - 2003-07-03 09:02 AM Re: Automatic shut down
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
what os you run it on?
what is your kixtart version (echo the @kix macro from the script)?
_________________________
!

download KiXnet

Top
#75596 - 2003-07-03 10:12 AM Re: Automatic shut down
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
The command works on Windows 98 and does not on XP. KiXtart version 4.20. I will try hardcoding the computer name. Anybody know some WMI code to shutdown/restart the local computer?
Top
#75597 - 2003-07-03 10:17 AM Re: Automatic shut down
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
sure we have such here too...
but try it again with hardcoded.
actually, wasn't this the function that shouldn't work on win9x?
_________________________
!

download KiXnet

Top
#75598 - 2003-07-03 11:31 AM Re: Automatic shut down
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
Specifying the computer still leave me with an error code 53 and no, it DOES work on Windows 98 and NOT on Windows XP - go figure.
Top
#75599 - 2003-07-03 11:38 AM Re: Automatic shut down
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
next I would suspect problem with network settings.
dunno how kix internals work but say if it puts localsystem name to the field if left blank and some weirdness going on in that machine, it can fail.

did you try this only on one xp?

boys, have you used this on xp?
_________________________
!

download KiXnet

Top
#75600 - 2003-07-03 01:22 PM Re: Automatic shut down
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
I found out that when I add Domain/Administrators to the local Administrators group on the computer the error code now becomes 21. I also tried using WMI to shut down the computer instead of the Shutdown command and I got the same error. This would indicate that the problem has to do with Windows and not KiXtart. I have only tested this on my test domain, which only has 1 XP computer. I suppose I should try it on the real network and see if the problem changes. Is anybody aware of any specific aspects of the system policy which may be causing this problem?
Top
#75601 - 2003-07-03 03:11 PM Re: Automatic shut down
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Have you tested any of the Shutdown...() UDFs instead of using your homegrown code? Do they encounter the same problem?

Also, is a screensaver running on the computer that you try to shutdown, or is the computer locked by any chance?

finally, in order to shutdown a computer the user account being used needs to have the SeShutdown right which can be revoked in the 'Local Security Settings' screen. However, then at least my Shutdown UDF would give you a failiure code as it checks for the correct permission to shutdown a computer.
_________________________
There are two types of vessels, submarines and targets.

Top
#75602 - 2003-07-03 07:38 PM Re: Automatic shut down
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
So I decided to start thinking and I used @Serror instead of @Error to get a better idea of the problem. Apparently Error Code 21 is "Device not ready." Any idea what device it might be speaking of? To answer your question, yes, I tried using the UDF that will shutdown/restart/logoff a computer (local or remote) via WMI and recieved the same error code. I'm begining to think it has something to do with the test workstation's odd set up.
Top
#75603 - 2003-07-03 08:04 PM Re: Automatic shut down
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Does the shutdown.exe from the resource kit or KiXtart's build-in SHUTDOWN() experience the same problems?
_________________________
There are two types of vessels, submarines and targets.

Top
#75604 - 2003-07-03 09:25 PM Re: Automatic shut down
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
how come the domain-admins were not in local-admins?
have you manually removed it?
_________________________
!

download KiXnet

Top
#75605 - 2003-07-04 04:51 AM Re: Automatic shut down
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
I have tried shutdown.exe and have had no success. Not only does shutdown.exe not work as part of the script, but scheduled alone it doesn't execute when no user is logged on. I'm still getting the error code 21, device not ready. I have attempted the following so far: Using KiXtart, WMI, and shutdown.exe to issue the command along with trying computer name vs. no computer name and force vs. no force as options. I'm fairly out of ideas. Can somebody else try using the code in the first post and see if it works on an XP computer. It's real simple: 1) make a script with the code above. 2) Create a task in the schedular to run the script in a minute (allow yourself enough time to log off). 3) Log off and wait until execution time. If it worked, the computer will reboot. If not, it won't.

Note: I run Windows NT 4.0 Servers w/ SP6a

[ 04. July 2003, 04:54: Message edited by: Gaven ]

Top
#75606 - 2003-07-04 05:00 AM Re: Automatic shut down
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
ok, me willing to give it a try ... but just for sake of simplicity, really, all we need to put into the script is the shutdown() function right ? no need for the wmi stuff, correct ?

-Shawn

Top
#75607 - 2003-07-04 05:10 AM Re: Automatic shut down
Gaven Offline
Starting to like KiXtart
*

Registered: 2003-06-19
Posts: 103
Loc: Washinton DC, USA
Just make a kix file from the 7 lines of code in the first post. The little bit of WMI is necessary to see if a user is logged on. Does anybody out there know how to set the SE_Shutdown privilege?
Top
#75608 - 2003-07-04 08:36 AM Re: Automatic shut down
Kdyer Offline
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
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#75609 - 2003-07-04 10:52 PM Re: Automatic shut down
Gaven Offline
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
#75610 - 2003-07-07 03:29 PM Re: Automatic shut down
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Now, is this happening on only a single computer? Have tried re-installing Windows?

I have no problems shutting down and loggin off users from remote computers. As a matter of fact, we are using these KiXtart scripts within a custom client-server data acquisition system where the controller client has to shutdown the server as the server has no input devices (keyboard, mouse, touchscreen) except custom data acquisition electronics, sensors, and electrodes.
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1198 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.08 seconds in which 0.032 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org