;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       WMIShutdown() 
;; 
;;ACTION         Shuts down a computer (default is local) 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0  - 2008/03/10 
;; 
;;HISTORY        1.0  - 2008/03/10 - Initial Release 
;; 
;;SYNTAX         WMIShutdown([func] [, Force] [, Computer] [, AuthPtr]) 
;; 
;;PARAMETERS     Func - OPTIONAL - String 
;;               - A code or command-word from the following options: 
;;                 L  - Logoff 
;;                 S  - Shutdown 
;;                 R  - Reboot 
;; 
;;		 Force - OPTIONAL - Boolean 
;;               - A flag indicating that any open applications should be forcibly closed                 
;; 
;;		 Computer - OPTIONAL - String 
;;               - The Name of a computer to control 
;; 
;;               AuthPtr - OPTIONAL - Object 
;;               - The pre-authenticated WMI object pointer. 
;;                 Use WMIAuthenticate() udf to create the AuthPtr value. 
;;                 AuthPtr is not needed if user has admin rights. 
;; 
;;REMARKS        Default operation is shutdown, but can perform Reboot and Logoff as well 
;; 
;;RETURNS        Nothing 
;; 
;;DEPENDENCIES   WMI 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;; 
;;EXAMPLES       WMIShutdown( , 1, 'computer71') ; forcibly shutdown a remote system 
;;               WMIShutdown('reboot' , 1, 'computer71') ; forcibly reboot a remote system 
; 
Function WMIShutdown(OPTIONAL $_Function, OPTIONAL $_Force, OPTIONAL $_Computer, OPTIONAL $_pAuth)
 
  Dim $_objWMIService, $_colItems, $_objItem		; WMI object vars 
 
  ; Trim to single char, default to shutdown if not specified 
  $_Function = IIf($_Function = '', 'S', Left($_Function, 1))
  ; Set function code (-1=invalid, 0=logoff, 1=shutdown, 2=reboot) 
  $_Function = InStr('LSR', $_Function) - 1
 
  ; Verify proper function argument 
  If $_Function < 0
    Exit 87
  EndIf
 
  ; set force value 
  $_Force = InStr('1TY', Left($_Force + 'X', 1))	; handle string/empty Force argument 
  $_Force = IIf($_Force = 0, 0, 4)			; set force binary value as appropriate 
  $_Function = $_Function + $_Force			; add Force value to Function value 
 
  ; insure a properly formatted computer name, default to local computer is not specified 
  $_Computer = IIf(Not $_Computer, '.', Join(Split($_Computer,'\'),''))
 
  ; If a pre-authenticated WMI object pointer was provided, use it, otherwise create a new object pointer 
  If $_pAuth
    $_objWMIService = $_pAuth
  Else
    $_objWMIService = GetObject('winmgmts:{impersonationLevel=impersonate}!\\' + $_Computer + '\root\cimv2')
    If @ERROR Exit Val('&' + Right(DecToHex(@ERROR), 4)) EndIf
  EndIf
 
  $_colItems = $_objWMIService.ExecQuery("Select * from Win32_OperatingSystem where Primary=true")
  For Each $_objItem in $_colItems
    $WMIShutdown = $_objItem.Win32Shutdown($_Function)
  Next
 
  Exit 0
 
EndFunction