Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Turn Off Computer

34 views
Skip to first unread message

Gary''s Student

unread,
Jan 27, 2009, 10:58:10 AM1/27/09
to
What command would I issue from VBScript to turn off my computer?
--
Gary''s Student - gsnu2007xx

mycow

unread,
Jan 27, 2009, 11:08:06 AM1/27/09
to

"Gary''s Student" <GarysS...@discussions.microsoft.com> wrote in message
news:4CD7F646-2B92-4834...@microsoft.com...

> What command would I issue from VBScript to turn off my computer?
> --
> Gary''s Student - gsnu2007xx

I've used this command successfully.. Look up the Win32Shutdown Function
for all Possible parameters.. (BTW: You must have Shutdown Privileges to run
this script..)

'----Code Start----
Set oWmi=GetObject("winmgmts:{(shutdown)}")
For Each oPC In oWmi.ExecQuery("Select * from Win32_OperatingSystem")
oPC.Win32Shutdown 6
Next
'----Code End----


Richard Mueller [MVP]

unread,
Jan 27, 2009, 11:08:32 AM1/27/09
to

"Gary''s Student" <GarysS...@discussions.microsoft.com> wrote in message
news:4CD7F646-2B92-4834...@microsoft.com...
> What command would I issue from VBScript to turn off my computer?
> --
> Gary''s Student - gsnu2007xx

This example from the Script Center shows how to shutdown:

http://www.microsoft.com/technet/scriptcenter/scripts/desktop/state/dmstvb08.mspx

If the computer supports it, you can power off. See this link:

http://www.microsoft.com/technet/scriptcenter/guide/sas_cpm_jleo.mspx

Navigate in the TOC to the left in the page to code examples:

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


Gary''s Student

unread,
Jan 27, 2009, 11:37:01 AM1/27/09
to
Thank you
--
Gary''s Student - gsnu200829

Gary''s Student

unread,
Jan 27, 2009, 11:38:06 AM1/27/09
to
Thank you
--
Gary''s Student - gsnu200829

mr_unreliable

unread,
Jan 27, 2009, 11:43:07 AM1/27/09
to
Gary''s Student wrote:
> What command would I issue from VBScript to turn off my computer?

hi Gary,

You could try the wmi shutdown method of the win32_OperatingSystem
class, but upon careful reading it appears that will only "prepare"
your system for shutdown, i.e., close and unload all the apps.

afaik, if you want to power off, you will have to (ugh, gross!)
call a system api, more specifically the ExitWindowsEx api,
with the EWX_POWEROFF flag set.

http://msdn.microsoft.com/en-us/library/aa376868(VS.85).aspx

There are various means of calling system api's from script,
for example, dynawrap (see boilerplate below).

--- <"air code"> ---
Const EWX_FORCE = &H4
Const EWX_POWEROFF = &H8

' instantiate dynawrap...
Dim oDW : Set oDW = CreateObject("DynamicWrapper")
oDW.Register "USER32", "ExitWindowsEx", "i=ll", "r=1", "f=s"

Call oDW.ExitWindowsEx(EWX_POWEROFF Or EWX_FORCE, 0))
--- </"air code"> ---

And I must say for anything newer than win98, you may need to
adjust your permissions to call this api...

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but, no guarantee
the answers will be applicable to the questions)

--- <DynaWrap Boilerplate> ---
It is possible to declare-and-call an api from script,
but you must use a third-party control to do so,
or else write one yourself.

It has already been correctly pointed out that there
is no api-capability in "pure" script.

If you are willing to use a third-party control, then
one such control, called "DynaWrap", can be found on
Guenter Born's website (note: Guenter refers to it as
"DynaCall"). Here is the link to it:

http://people.freenet.de/gborn/WSHBazaar/WSHDynaCall.htm

- or -

http://ourworld.compuserve.com/homepages/Guenter_Born/WSHBazaar/WSHDynaCall.htm

On that page you will find a download for the control,
plus some code samples.

Note: you may find additional sample code by searching
the archives of the wsh and vbscript ng's.

Note also: DynaWrap does have its limitations. There are
certain things it can't do. For example, you can't call
api's "by ordinal". (I had previously asserted that you
can't use DynaWrap to call api's which take typedefs as
parameters, but I have been proven wrong on that --
although it took me about two weeks to figure out how it
was done). Regardless, Dynawrap will work for most of
"the usual suspects".

And finally, DynaWrap doesn't work entirely as advertised.
For example, it is supposed to allow for the declaration of
several api definitions in one instance of itself. I could
never get that to work (in win9x -- but others have shown
this to work as advertised under winNT). With win98, you
will need a new instance of DynaWrap for every api, or else
re-instantiate the object for every api. Someday I'm going
to learn enough c++ to fix that...

Another possibility (for calling api's from script is XNeat:

http://www.xneat.com/doc/call_win32_api.html

One other thing. While the above discussion is oriented to
calling the system api's, contained in the user32, kernel32,
gdi32 and other system dll's, you could use DynaWrap (and
other similiar utilities) to call the exported interfaces
to ANY dll.

--- </DynaWrap Boilerplate> ---

mr_unreliable

unread,
Jan 27, 2009, 11:48:58 AM1/27/09
to
apparently richard and mycow have more recent versions
of wmi than I have, jw

Gary''s Student

unread,
Jan 27, 2009, 12:21:01 PM1/27/09
to
Thank you.

I appreciate your taking the time to suply all the details.
--
Gary''s Student - gsnu200829

BigDaddyJim

unread,
Feb 8, 2009, 8:38:47 PM2/8/09
to
On Jan 27, 11:21 am, Gary''s Student
> >http://ourworld.compuserve.com/homepages/Guenter_Born/WSHBazaar/WSHDy...
> > --- </DynaWrap Boilerplate> ---- Hide quoted text -
>
> - Show quoted text -

What about the RUN command from WSH?

Set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "%comspec% /C shutdown -s -t 0 -f", 2, True

Admittedly, not a true VBScript solution, but works every time.

Jim

0 new messages