Jump to content
hhsecond

Reading time from remote machine using WMI

Recommended Posts

Hi folks,

I am struggling to read time from a remote machine. By initial probing i could find it is possible by WMI object and connectserver (if we need to user password, yes here i need to use another username and password):(

Please help me to sort this out. Any suggestions would be appreciated. 

Share this post


Link to post
Share on other sites

this is my current code which is not working.

$strComputer = "computername"
$user = "username"
$pass = "password"
$objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objSWbemLocator.ConnectServer($strComputer, "root\cimv2", $user, $pass)


$colItems = $objWMIService.ExecQuery("Select * From Win32_LocalTime")

For $objItem in $colItems
    $strTime = $objItem.Hour & ":" & $objItem.Minute & ":" & $objItem.Second
   MsgBox(0,"date",$strTime)
Next

 

Share this post


Link to post
Share on other sites

Your code works like a charm on my network.

Do you get any errors in SciTE? Do you start your computername with "\\" or without? (only works without \\ on my computer)

Share this post


Link to post
Share on other sites

Your code works like a charm on my network.

Do you get any errors in SciTE? Do you start your computername with "\\" or without? (only works without \\ on my computer)

this is my error, and for localhost or "." , it is working 

" ==> Variable must be of type "Object".:
$colItems = $objWMIService.ExecQuery("Select * From Win32_LocalTime")
$colItems = $objWMIService^ ERROR

 

Edited by hhsecond

Share this post


Link to post
Share on other sites

So you need to do some error checking to ensure your locator can connect to the server. I would start off with something like this:

$strComputer = "computername"
$user = "username"
$pass = "password"

    If Ping($strComputer) Then
         $objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
         $objWMIService = $objSWbemLocator.ConnectServer($strComputer, "root\cimv2", $user, $pass)
             If Not @error Then $colItems = $objWMIService.ExecQuery("Select * From Win32_LocalTime")
                If $colItems.Count > 0 Then
                   For $objItem in $colItems
                      $strTime = $objItem.Hour & ":" & $objItem.Minute & ":" & $objItem.Second
                      MsgBox(0,"date",$strTime)
                   Next
                EndIf
    Else
        MsgBox(0, "Error", "Could not ping machine!")
    EndIf

 

Edited by JLogan3o13

Share this post


Link to post
Share on other sites

So you need to do some error checking to ensure your locator can connect to the server. I would start off with something like this:

$strComputer = "computername"
$user = "username"
$pass = "password"

    If Ping($strComputer) Then
         $objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
         $objWMIService = $objSWbemLocator.ConnectServer($strComputer, "root\cimv2", $user, $pass)
             If Not @error Then $colItems = $objWMIService.ExecQuery("Select * From Win32_LocalTime")
                If $colItems.Count > 0 Then
                   For $objItem in $colItems
                      $strTime = $objItem.Hour & ":" & $objItem.Minute & ":" & $objItem.Second
                      MsgBox(0,"date",$strTime)
                   Next
                EndIf
    Else
        MsgBox(0, "Error", "Could not ping machine!")
    EndIf

 

the same error, Is this some permission issue?

Seems like i am able to ping

: ==> Variable must be of type "Object".:
If $colItems.Count > 0 Then
If $colItems^ ERROR

 

Edited by hhsecond

Share this post


Link to post
Share on other sites

Hi guys,

I got a work around for this requirement.

But still i would like to have the solution for the above situation. If somebody can help, please....

 

Here is the work around that i find to get the time from remote machine.

net time is a cmd command to find time in remote machine. I used a UDF to get the cmd out put of this command and stringsplit does the rest part for me.

#include <Constants.au3>

;**********************************************************************************
;Function created by hhsecond - net time command will run in cmd
;it will give a long array, splitstring used to split that based on the delimter.
;uncheck each msgbox will give the output of each split string and cmd out put well
;**********************************************************************************
Func TimeCalc($Computer)
   $Cmd_out = _GetDOSOutput("net time \\" & $Computer)
   ;MsgBox(0,"cmd",$cmd_out)
   $Cmd_array = StringSplit($Cmd_out, "is ", 1)
   ;MsgBox(0,"array",$cmd_array[2])
   $Cmd_array2 = StringSplit($Cmd_array[2], @CRLF, 1)
   ;MsgBox(0,"array",$cmd_array2[1])
   $Cmd_array3 = StringSplit($Cmd_array2[1], " ", 1)
   ;MsgBox(0,"array",$cmd_array3[2])
   Return $Cmd_array3[2]
EndFunc



;*****************************************************************
;User defined Function from autoit for getting output from cmd
;*****************************************************************
Func _GetDOSOutput($sCommand)
    Local $iPID, $sOutput = ""

    $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    While 1
        $sOutput &= StdoutRead($iPID, False, False)
        If @error Then
            ExitLoop
        EndIf
        Sleep(10)
    WEnd
    Return $sOutput
EndFunc   ;==>_GetDOSOutput

 

Share this post


Link to post
Share on other sites

about your workaround, since the  word "is" doesn't appear in the output of non english OS, this little modification should allow to use your workaround also on non english systems.
Function returns an array with each part of the date/time string splitted in each element of the returned array,
according to "locale" system setting, elements [0] and [1] of the array may contain day and month or viceversa.

#include <Constants.au3>
#include <array.au3> ; just to show result
$Computer = @ComputerName
$aTime = TimeCalc($Computer)
_ArrayDisplay($aTime)

Func TimeCalc($Computer)
    $Cmd_out = _GetDOSOutput("net time \\" & $Computer)
    Return StringSplit(StringMid($Cmd_out, StringInStr($Cmd_out, @CR) - 19, 19), ": /", 2)
EndFunc   ;==>TimeCalc

;*****************************************************************
;User defined Function from autoit for getting output from cmd
;*****************************************************************
Func _GetDOSOutput($sCommand)
    Local $iPID, $sOutput = ""

    $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    While 1
        $sOutput &= StdoutRead($iPID, False, False)
        If @error Then
            ExitLoop
        EndIf
        Sleep(10)
    WEnd
    Return $sOutput
EndFunc   ;==>_GetDOSOutput

 

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • By Colduction
      Hi AutoIt scriptwriters! 
      In some of windows operating systems, i saw that "Administrator" and "Guest" username in different languages are "Administradore" and "Gast" and etc. 
      How to make a simple script to find Administrator and Guest username in different language in these systems?
    • By jresine
      Hello, is it possible to know via a script or command, to have the percentage of disk usage of a process? thank you in advance.
      ps: see image

    • By nacerbaaziz
      hello all, and welcome to this tool
      the NB-Password_generator is a small tool which allow you to create a strong passwords
      with this tool you can create a random passwords using :
      1. capital letters
      2. small letters
      3. numbers
      4. symbols
      be sure that you can check any option that  you want and uncheck what you don't want to use
      this tool allow you to create a password from 6 letters to 150 lettersNB-Password_generator.zip
      at the end please accept my greetings
      am waiting for your commants
    • By argumentum
      ..was looking for UserName from SessionID so I put this together
      #include <Debug.au3> ; for _DebugArrayDisplay() ; all you can get from _WTSQuerySessionInformation() Global $i = _WTSQuerySessionInformation(-1, 4) ; current user's SessionId Global $__a_WTS_INFO_CLASS = StringSplit("WTSInitialProgram,WTSApplicationName,WTSWorkingDirectory,WTSOEMId,WTSSessionId,WTSUserName," & _ "WTSWinStationName,WTSDomainName,WTSConnectState,WTSClientBuildNumber,WTSClientName,WTSClientDirectory,WTSClientProductId,WTSClientHardwareId," & _ "WTSClientAddress,WTSClientDisplay,WTSClientProtocolType,WTSIdleTime,WTSLogonTime,WTSIncomingBytes,WTSOutgoingBytes,WTSIncomingFrames," & _ "WTSOutgoingFrames,WTSClientInfo,WTSSessionInfo,WTSSessionInfoEx,WTSConfigInfo,WTSValidationInfo,WTSSessionAddressV4,WTSIsRemoteSession", ",", 2) For $n = 0 To UBound($__a_WTS_INFO_CLASS) -1 ConsoleWrite($n & @TAB & StringLeft($__a_WTS_INFO_CLASS[$n] & ' ________________', 24) & " " & _WTSQuerySessionInformation($i, $n, 1) & @CRLF) Next Global $a = ListUserSessions() _DebugArrayDisplay($a, "ListUserSessions()") Func ListUserSessions() ; mod. of https://www.autoitscript.com/forum/topic/139774-dllcall-and-returned-pointers/?do=findComment&comment=980850 Local $_Self_SessionId = _WTSQuerySessionInformation(-1, 4) ; -1 = current user ; 4 = WTSSessionId Local Enum $e_IsSelf_SessionId, $e_SessionName, $e_UserName, $e_SessionId, $e_StateName, $e_StateInt, $e_ClientName, $e_ClientIp, $e_Domain, $e_UBound Local Const $tagWTS_SESSION_INFO = 'dword SessionId;ptr WinStationName;uint State' Local $aResult = DllCall('wtsapi32.dll', 'int', 'WTSEnumerateSessionsW', 'ptr', 0, 'dword', 0, 'dword', 1, 'ptr*', 0, 'dword*', 0) If @error Or $aResult[0] = 0 Then Return SetError(1, 0, "") ; https://docs.microsoft.com/en-us/windows/desktop/api/wtsapi32/ne-wtsapi32-_wts_connectstate_class Local $aConnectionState = StringSplit("Active,Connected,ConnectQuery,Shadow,Disconnected,Idle,Listen,Reset,Down,Init", ",", 2) Local $tInfo, $Offset = 0, $c = 0, $aReturn[$aResult[5] + 1][$e_UBound] ; $e_UBound is the last enumerator, just to determine the size of the array $aReturn[0][$e_SessionId] = "ID" $aReturn[0][$e_SessionName] = "SessionName" $aReturn[0][$e_StateInt] = "StateInt" $aReturn[0][$e_StateName] = "State" $aReturn[0][$e_UserName] = "UserName" $aReturn[0][$e_ClientName] = "ClientName" $aReturn[0][$e_ClientIp] = "ClientIp" $aReturn[0][$e_Domain] = "Domain" For $i = 1 To $aResult[5] $tInfo = DllStructCreate($tagWTS_SESSION_INFO, $aResult[4] + $Offset) $Offset += DllStructGetSize($tInfo) $c += 1 $aReturn[$c][$e_SessionId] = DllStructGetData($tInfo, 'SessionId') $aReturn[$c][$e_SessionName] = DllStructGetData(DllStructCreate('wchar[1024]', DllStructGetData($tInfo, 'WinStationName')), 1) $aReturn[$c][$e_StateInt] = DllStructGetData($tInfo, 'State') If UBound($aConnectionState) > $aReturn[$c][$e_StateInt] Then $aReturn[$c][$e_StateName] = $aConnectionState[$aReturn[$c][$e_StateInt]] $aReturn[$c][$e_UserName] = _WTSQuerySessionInformation($aReturn[$c][$e_SessionId], 5) ; WTSUserName $aReturn[$c][$e_ClientName] = _WTSQuerySessionInformation($aReturn[$c][$e_SessionId], 10) ; WTSClientName $aReturn[$c][$e_ClientIp] = _WTSQuerySessionInformation($aReturn[$c][$e_SessionId], 14) ; WTSClientAddress $aReturn[$c][$e_Domain] = _WTSQuerySessionInformation($aReturn[$c][$e_SessionId], 7) ; WTSDomainName $aReturn[0][$e_IsSelf_SessionId] = $c If $_Self_SessionId = $aReturn[$c][$e_SessionId] Then $aReturn[$c][$e_IsSelf_SessionId] = 1 Else $aReturn[$c][$e_IsSelf_SessionId] = 0 EndIf Next DllCall('wtsapi32.dll', 'none', 'WTSFreeMemory', 'ptr', $aResult[4]) Return $aReturn EndFunc ;==>ListUserSessions Func _WTSQuerySessionInformation($SessionId, $WTSInfoClass = 10, $iReturnAsIs = 0) ; mod. of https://www.autoitscript.com/forum/topic/134679-get-hostname-of-the-client-connected-to-the-terminalserver-session/ Local $aResult = DllCall("Wtsapi32.dll", "int", "WTSQuerySessionInformation", "Ptr", 0, "int", $SessionId, "int", $WTSInfoClass, "ptr*", 0, "dword*", 0) If @error Or $aResult[0] = 0 Then Return SetError(1, 0, "") Local $ip = DllStructGetData(DllStructCreate("byte[" & $aResult[5] & "]", $aResult[4]), 1) DllCall("Wtsapi32.dll", "int", "WTSFreeMemory", "ptr", $aResult[4]) If $iReturnAsIs Then Return $ip Switch $WTSInfoClass ; https://docs.microsoft.com/en-us/windows/desktop/api/wtsapi32/ns-wtsapi32-_wts_client_address Case 4 ; WTSSessionId Return Int('0x' & StringTrimRight(StringReverse($ip), 3)) Case 14 ; WTSClientAddress If Not (Int(StringLeft($ip, 4)) = 2) Then ; IPv4 $ip = "" Else $ip = Dec(StringMid($ip, 15, 2)) & '.' & Dec(StringMid($ip, 17, 2)) & '.' & Dec(StringMid($ip, 19, 2)) & '.' & Dec(StringMid($ip, 21, 2)) EndIf EndSwitch Return StringReplace(BinaryToString($ip), Chr(0), "") EndFunc ;==>_GetWTSClientName  
    • By Dwalfware
      HI
      Another head scratched
      Anyone solve how to get the IPV6 from WMI?
      I followed the usual SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID='" & GUICtrlRead($cboAdapters) & "'
      get the index 
      port it to 
      Local $query = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 )
      All i can see is IPv4 addresses
      My question is is there another place the IPv6 information is hiding? I configured my LAN with IPV6 and my wireless with IPv4 so I can see what shows.
      Thanks in Advance.
      PS. I did try google
       
×
×
  • Create New...