;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       WMIAuthentication() 
;; 
;;ACTION         Authenticates against a local or remote WMI provider 
;; 
;;AUTHOR         Jens Meyer 
;; 
;;CONTRIBUTORS    
;; 
;;VERSION        1.2 - 2003/05/26 
;;                removed console output 
;;               1.0 - 2002/12/05 
;; 
;;HISTORY        ver - date - comment 
;; 
;;SYNTAX         WMIAuthentication([sComputerName, sUserName, sPassword, sNameSpace]) 
;; 
;;PARAMETERS     sComputerName - Optional - String 
;;               - Value containing the target computers name, defaults to local computer 
;; 
;;               sUserName - Optional - String 
;;               - Username when connection to a remote system, defaults to current username 
;; 
;;               sPassword - Optional - String 
;;               - Password when connecting to a remote system, defaults to current password 
;; 
;;               sNameSpace - Optional - String 
;;               - contains the namespace to connect to, defaults to root\CIMV2 
;; 
;;REMARKS        none 
;; 
;;RETURNS        WBEM object if successful, otherwise error code 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K3, W2K8, W2K12 
;; 
;;EXAMPLES       $objWBEM=WMIAuthentication('WORKSTATION','username','password','root\CIMV2') 
; 
function WMIAuthentication(optional $sComputerName, optional $sUserName, optional $sPassword, optional $sNameSpace)
  dim $objLocator, $objWBEM
 
  $sNameSpace=trim($sNameSpace)
  if $sNameSpace=''
    $sNameSpace = 'root\CIMV2'
  endif
 
  ; check to see whether we're connecting to a local or remote computer 
  $sComputerName=trim($sComputerName)
  select
  case $sComputerName=@WKSTA
    $sComputerName='.'
  case $sComputerName
  case 1
    $sComputerName='.'
  endselect
 
  select
  case $sUserName and $sComputerName<>'.'
    ; create locator object for connection to a remote computer 
    $objLocator = CreateObject('WbemScripting.SWbemLocator')
    if @ERROR
      exit @ERROR
    endif
 
    ; create an credentialed (username/password provided) connection to a remote computer 
    $objWBEM=$objLocator.ConnectServer($sComputerName,$sNameSpace,$sUserName,$sPassword)
    if @ERROR
      exit @ERROR
    endif
 
    ; set the impersonation level 
    $objWBEM.Security_.ImpersonationLevel = 3
    if @ERROR
      exit @ERROR
    endif
 
  case 1
    ;set the impersonation level 
    $objWBEM=GetObject('winmgmts:{impersonationLevel=impersonate}!\\'+$sComputerName+'\'+$sNameSpace)
    if @ERROR
      exit @ERROR
    endif
  endselect
 
  $WMIAuthentication=$objWBEM
  exit @ERROR
endfunction