Jump to content

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. 

Link to comment
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

 

Link to comment
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
Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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
Link to comment
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

 

Link to comment
Share on other sites

  • 2 weeks later...

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

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
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
 Share

×
×
  • Create New...