Re: Help needed with a js command



jic wrote:
>
> Sinus Logarithme wrote:
> > Dave Anderson wrote:
> > >
> > > Sinus Logarithme wrote:
> > > > In windows 2000 sp4, the following command, in a .js file,
> > > > brings up the shutdown dialog:
> > > > (new ActiveXObject("Shell.Application")).ShutdownWindows();
> > > >
> > > > I'd like to restart windows, no questions asked. Is it
> > > > possible to modify that command to do the job ?
> > >
> > > Consider the command-line way:
> > >
> > > C:\WINDOWS\system32\shutdown.exe /r
> > >
> > > Anything you can work with there? Note that it is possible to reboot a
> > > remote machine this way (shutdown /? for more).
> > >
> > > --
> > > Dave Anderson
> > >
> > > Unsolicited commercial email will be read at a cost of $500 per message. Use
> > > of this email address implies consent to these terms. Please do not contact
> > > me directly or ask me to contact you directly for assistance. If your
> > > question is worth asking, it's worth posting.
> >
> >
> > Windows didn't install shutdown.exe in my system. And
> > although I found a couple of substitutes on the web, I
> > prefer the 'script' solution. I am almost done with it, as I
> > said in my new post, above. Thanks for your time.
>
> Hey, when you get it done and working, it would be nice to post it
> right back here so that other folks, like myself, could enjoy it and,
> perhaps, use it.
>
> thanks,
>
> jic


jic,

The original script allowed to shutdown any computer in the
network. But, for some reason, it can't find my second
computer, and I just need to reboot one computer, anyway. So
I modified the script in order to pass to it exactly one
parameter. It acts on the local computer: "."

I must say that I don't understand the workings of the
script. All the credit goes to the original author, in the
post you directed me to.

Every line begins with at least one space.
--- The script -----------------------
'For use in Windows 2000
'One parameter needed:
' 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

Set oArgs = WScript.Arguments

If oArgs.Count = 0 Then
WScript.Echo "Parameter: PowerOff, Shutdown, Reboot,
LogOff, with or without the suffix _Force"
WScript.Quit()
Else
ShutDown ".", oArgs(0)
End If

Sub ShutDown(sNode, sAction)
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Const EWX_POWEROFF = 8

On Error Resume Next
Set oWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Shutdown)}!\\" _
& sNode & "\root\cimv2")

Set colOperatingSystems = oWMI.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each obj in colOperatingSystems
Set oOS = obj : Exit For
Next
If Err.Number <> 0 Then
WScript.Echo "Could not connect to " & sNode
Exit Sub
End If
sAction = LCase(sAction)

Select Case 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
WScript.Echo "Parameter error"
WScript.Quit()
End Select
oOS.Win32shutdown iCmd
End Sub
--- End of the script --------------
As you can see, I had to you use WScript.Quit() twice.

In fact, I'll mainly use part of that script as a conclusion
to a set of tasks executed by a Basic program. The program
will write a script on the fly and then execute it:

t$ = CHR$(34)
OPEN "shutd.vbs" FOR OUTPUT AS #1
PRINT #1, "Set oWMI = GetObject("; t$; "winmgmts:"; t$; "
_"
PRINT #1, " & "; t$;
"{impersonationLevel=impersonate,(Shutdown)}!\\"; t$; " _"
PRINT #1, " & "; t$; "."; t$; " & "; t$; "\root\cimv2";
t$; ")"
PRINT #1, "Set colOperatingSystems = oWMI.ExecQuery _"
PRINT #1, " ("; t$; "Select * from Win32_OperatingSystem";
t$; ")"
PRINT #1, "For Each obj in colOperatingSystems : Set oOS =
obj : Exit For : Next"
PRINT #1, "oOs.Win32shutdown 6"
CLOSE 1
SHELL ("start shutd.vbs")

Resulting content of shutd.vbs:
Set oWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Shutdown)}!\\" _
& "." & "\root\cimv2")
Set colOperatingSystems = oWMI.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each obj in colOperatingSystems : Set oOS = obj : Exit
For : Next
oOs.Win32shutdown 6
.



Relevant Pages

  • RE: Finetuning: Remote Shutdown with WMI, some errors occur.
    ... Also we would like that a workstation that's locked is also shutdown by this ... script to shutdown all approx. ... Dim strBase, strFilter, strAttributes, strQuery, objRecordSet ... Const OpenAsDefault = -2 ...
    (microsoft.public.windows.server.scripting)
  • Re: Script to reboot group of PCs
    ... TRY: Shutdown /? ... /m = specify machine to shutdown ... >> Where machines.txt is a list of machine names or IP's you want to reboot. ... >> Use of any included script samples are subject to the terms specified at ...
    (microsoft.public.windows.server.scripting)
  • Finetuning: Remote Shutdown with WMI, some errors occur.
    ... script to shutdown all approx. ... Modified script is found below. ... Dim strBase, strFilter, strAttributes, strQuery, objRecordSet ... Const OpenAsDefault = -2 ...
    (microsoft.public.windows.server.scripting)
  • Re: remote shutdown, not logged in
    ... power off the local computer. ... See further down in the script for more shutdown/reboot ... ' Use "Shutdown" for a shutdown ... Const EWX_SHUTDOWN = 1 ...
    (microsoft.public.windowsxp.security_admin)
  • Re: shutdown/reboot suggestion
    ... I have such a script, I put it in /bin/require_hostname and symlinked ... shutdown, halt, reboot, fastboot, and fasthalt to this script: ... have inadvertently shutdown or rebooted the wrong machine. ... These are all distinct from my term window color scheme for my local host. ...
    (freebsd-questions)