break on $=SetOption( "NoVarsInStrings", "ON" ) $=SetOption( "NoMacrosInStrings", "ON" ) $=SetOption( "Explicit", "ON" ) if not IsDeclared( $strComputer ) global $strComputer $strComputer = @WKSTA endif if not IsDeclared( $strUser ) global $strUser endif if not IsDeclared( $strPassword ) global $strPassword endif dim $objWMI ;-- Connect to WMI -- $objWMI = WMIConnectEx( "root\cimv2", "authenticationLevel=PktPrivacy,impersonationlevel=impersonate" $strcomputer, $strUser, $strPassword ) if @error "unable to connect to WMI" ? quit endif ;-- enum interactive session -- $=EnumUserConnected( $objWMI, 2 , ". Interactive user account" ) ;-- enum remote interactive session -- $=EnumUserConnected( $objWMI, 10 , ". Remote user account" ) ;-- enum all sessions (to verify) -- $=EnumUserConnected( $objWMI, , ". enum all sessions" ) exit function EnumUserConnected( $objWMI, optional $LogonType, optional $strLogonType ) dim $colSessions, $objSession, $colList, $objItem, $SessionQuery $SessionQuery = "Select * from Win32_LogonSession" if vartype( $LogonType)<>0 $SessionQuery = $SessionQuery + " Where LogonType = "+$LogonType endif $strLogonType ? $colSessions = $objWMI.ExecQuery( $SessionQuery ) if $colSessions.Count = 0 ;-- No interactive session found " No user found" ? else ;-- Interactive session found -- For Each $objSession in $colSessions ;-- Show session start time -- " . session : " $objSession.LogonID " (" $objSession.LogonType ")" ? " . Start Time : " $objSession.StartTime ? ;-- enum session information -- $colList = $objWMI.ExecQuery( "Associators of {Win32_LogonSession.LogonId=" + $objSession.LogonId + "} " + "Where AssocClass=Win32_LoggedOnUser Role=Dependent" ) ;-- Show user info -- For Each $objItem in $colList ;-- Show session start time -- " User : " $objItem.Name ? " FullName : " $objItem.FullName ? " Domain : " $objItem.Domain ? Next Next endif ? endfunction ;------------------------------------------------------------------------------- ; how to create a "moniker" for WMI. see url below ; http://www.secretswindows.com/index.php?rubrique=scripts&ssrubrique=WMI&ID=145&page=./scripts/wmi/scriptingapi.htm ; securitySetting {[authenticationLevel=authenticationValue][, impersonationLevel=impersonationValue][, authority=authorityValue][, ([privilegeName],[!][privilegeName])]} ;------------------------------------------------------------------------------- Function WMIConnectEx(Optional $namespace, Optional $SecuritySettings, Optional $strcomputer, optional $username, optional $password ) dim $objWMIService if vartype($namespace)=0 $namespace = "root\cimv2" endif if not $namespace $namespace = "root\cimv2" endif if left($namespace,1)="\" $namespace = substr($namespace,2) endif if vartype($SecuritySettings)=0 $SecuritySettings = "impersonationlevel=impersonate" endif if not $SecuritySettings $SecuritySettings = "impersonationlevel=impersonate" endif if vartype($strcomputer)=0 $strcomputer = "." endif if not $strcomputer $strcomputer = "." endif if $strcomputer=@WKSTA $strcomputer = "." endif $strcomputer = trim($strcomputer) if not $strcomputer $strcomputer = "." endif if InStr($strcomputer,'\') $strcomputer=Right($strcomputer,InStrRev($strcomputer,'\')) endif if $username and $password and ($strcomputer<>'.') dim $objLocator ;-- create locator object for connection to a remote computer -- $objLocator = CreateObject('WbemScripting.SWbemLocator') If @ERROR Exit Val("&"+Right(DecToHex(@ERROR),4)) EndIf ;-- create a (credentialed, if username/password provided) connection to a remote computer -- $objWMIService=$objLocator.ConnectServer($strcomputer, $namespace, $username, $password) If @ERROR Exit Val("&"+Right(DecToHex(@ERROR),4)) EndIf ;-- set the impersonation level -- $objWMIService.Security_.ImpersonationLevel = 3 If @ERROR Exit Val("&"+Right(DecToHex(@ERROR),4)) EndIf else ;-- set the impersonation level and make sure we have security permissions -- $objWMIService=GetObject('winmgmts:{'+$SecuritySettings+'}!\\'+$strcomputer+'\'+$namespace) If @ERROR Exit Val("&"+Right(DecToHex(@ERROR),4)) EndIf endif $WMIConnectEx = $objWMIService EndFunction