in reply to NT RPC and Shutdown.

use Win32; Win32::InitiateSystemShutdown(...);

For documentation on the parameters see "perldoc Win32".

For rebooting the local machine, you have to enable some privileges. See Win32API::Registry for more details. Update, I only mentioned this last part because the "perldoc Win32" doesn't explain this bit but my Win32API::Registry documentation does and provides code for dealing with it. So I thought it might be important for some readers, even if not for the original noder.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: NT RPC and Shutdown.
by idnopheq (Chaplain) on Mar 14, 2001 at 07:36 UTC
    If you have rights or access to a userid with rights, use Win32::Lanman or Win32::NetResource to map to the remote machine's ipc$ share (w/o drive letter) as that userid (or do a net use \\remotehost\ipc$ * /user:[domain\]userid. If I remember right you must have 'Allow user to logon from the network' and 'Allow user to shutdown the system' rights. Then call
    Win32::InitiateSystemShutdown( $machine, $message, $count, $force, $reboot );

    Returns true (1) on success, which you will capture, of course.

    Remember:

    $machine should have either \\\\ or // prepended to the hostname/IP address or simply "" for localhost

    $message is a quoted text string to display in the pop-up on $machine

    $count is the number of seconds for a countdown to shutdown/reboot

    $force = 1 means applications will die in an undignified mannor, not saving data. 0 means apps will try to die gracefully, but may hang the request.

    $reboot = 1 the box will reboot versus a shutdown (0)

    HTH!
    Dex

    p.s. - there is a matching Win32::AbortSystemShutdown which, if sent before $count seconds from above, will cancel the shutdown.

      BTW, thanks for the added instructions. Here some amplifications, mostly taken from the Win32API::Registry documentation I mentioned.

      For this API, you don't need "//" nor "\\\\" on the front of the machine name (though they don't hurt either).

      $message is a quoted text string to display in the pop-up on $machine

      I find "quoted text string" misleading here. It just needs to be a Perl string (scalar).

      If "force" is false, then any applications running on the remote computer get a chance to prompt the remote user whether they want to save changes. Also, for any applications that do not exit quickly enough, the operating system will prompt the user whether they wish to wait longer for the application to exit or force it to exit now. At any of these prompts the user can press CANCEL to abort the shutdown but if no applications have unsaved data, they will likely all exit quickly and the shutdown will progress with the remote user having no option to cancel the shutdown.

      If "force" is true, all applications are told to exit immediately and so will not prompt the user even if there is unsaved data. Any applications that take too long to exit will be forcibly killed after a short time. The only way to abort the shutdown is to call AbortSystemShutdown() before the timeout expires and there is no way to abort the shutdown once it has begun.

              - tye (but my friends call me "Tye")
      there is a matching Win32::AbortSystemShutdown which, if sent before $count seconds from above, will cancel the shutdown

      Great, so I'll make a "self-destruct button" that causes a shutdown in a specified amount of time, with a nice big pTK countdown and a red button to abort it, like any good self-destruct sequence should have.

      =Blue
      ...you might be eaten by a grue...

        The NT shutdown already has a big countdown window (but no abort button), BTW. (:

                - tye (but my friends call me "Tye")