Jump to content

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 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

 

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

 

Link to post
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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jguinch
      Hello.
      I did create these few functions several months ago. I post here, if it can interest someone.
      These functions based on WMI queries allow you to manage printers : add / delete printer, driver, port, or obtain configuration, set default printer ... I let you discover it with the code.

       
      Here is the list of the available functions :
      _PrintMgr_AddLocalPort
      _PrintMgr_AddLPRPort
      _PrintMgr_AddPrinter
      _PrintMgr_AddPrinterDriver
      _PrintMgr_AddTCPIPPrinterPort
      _PrintMgr_AddWindowsPrinterConnection
      _PrintMgr_CancelAllJobs
      _PrintMgr_CancelPrintJob
      _PrintMgr_EnumPorts
      _PrintMgr_EnumPrinter
      _PrintMgr_EnumPrinterConfiguration
      _PrintMgr_EnumPrinterDriver
      _PrintMgr_EnumPrinterProperties
      _PrintMgr_EnumPrintJobs
      _PrintMgr_EnumTCPIPPrinterPort
      _PrintMgr_Pause
      _PrintMgr_PortExists
      _PrintMgr_PrinterExists
      _PrintMgr_PrinterSetComment
      _PrintMgr_PrinterSetDriver
      _PrintMgr_PrinterSetPort
      _PrintMgr_PrinterShare
      _PrintMgr_PrintTestPage
      _PrintMgr_RemoveLocalPort
      _PrintMgr_RemoveLPRPort
      _PrintMgr_RemovePrinter
      _PrintMgr_RemovePrinterDriver
      _PrintMgr_RemoveTCPIPPrinterPort
      _PrintMgr_RenamePrinter
      _PrintMgr_Resume
      _PrintMgr_SetDefaultPrinter
       
       
      And some examples :
      #include <Array.au3> #include "PrintMgr.au3" _Example() Func _Example() ; Remove a printer called "My old Lexmark printer" : _PrintMgr_RemovePrinter("My old Lexmark printer") ; Remove the driver called "Lexmark T640" : _PrintMgr_RemovePrinterDriver("Lexmark T640") ; Remove the TCP/IP printer port called "TCP/IP" _PrintMgr_RemoveTCPIPPrinterPort("MyOLDPrinterPort") ; Add a driver, called "Samsung ML-451x 501x Series", and driver inf file is ".\Samsung5010\sse2m.inf" _PrintMgr_AddPrinterDriver("Samsung ML-451x 501x Series", "Windows NT x86", @ScriptDir & "\Samsung5010", @ScriptDir & "\Samsung5010\sse2m.inf") ; Add a TCP/IP printer port, called "MyTCPIPPrinterPort", with IPAddress = 192.168.1.10 and Port = 9100 _PrintMgr_AddTCPIPPrinterPort("MyTCPIPPrinterPort", "192.168.1.10", 9100) ; Add a printer, give it the name "My Printer", use the driver called "Samsung ML-451x 501x Series" and the port called "MyTCPIPPrinterPort" _PrintMgr_AddPrinter("My Printer", "Samsung ML-451x 501x Series", "MyTCPIPPrinterPort") ; Set the printer called "My Printer" as default printer _PrintMgr_SetDefaultPrinter("My Printer") ; Connect to the shared printer "\\192.168.1.1\HPDeskjetColor") _PrintMgr_AddWindowsPrinterConnection("\\192.168.1.1\HPDeskjetColor") ; List all installed printers Local $aPrinterList = _PrintMgr_EnumPrinter() _ArrayDisplay($aPrinterList) ; List all printers configuration Local $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration() _ArrayDisplay($aPrinterConfig) ; List all installed printer drivers Local $aDriverList = _PrintMgr_EnumPrinterDriver() _ArrayDisplay($aDriverList) ; Retrieve the printer configuration for the printer called "Lexmark T640" $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration("Lexmark T640") _ArrayDisplay($aPrinterConfig) ; Add a local printer port (for a file output) _PrintMgr_AddLocalPort("c:\temp\output.pcl") ; Remove the local port _PrintMgr_RemoveLocalPort("c:\temp\output.pcl") ; Enum a print job Local $aJobList = _PrintMgr_EnumPrintJobs() _ArrayDisplay($aJobList) EndFunc ;==>_Example Download link :
      PrintMgr_Example.au3  PrintMgr.au3
       
    • By izmegna
      I am trying to auto login to web app that has the following HTML for the username, password and submit button:
      USERNAME:
      <input name="usernameField" tabindex="0" class="inp" id="usernameField" type="text" value="" message="FND_SSO_USER_NAME">
      Password:
      <input name="passwordField" tabindex="0" class="inp" id="passwordField" type="password" value="" message="FND_SSO_PASSWORD">
      Login:
      <button tabindex="0" class="OraButton left" style="padding-right: 6px; padding-left: 6px;" onclick="submitCredentials()" message="FND_SSO_LOGIN">Log In</button>
      Following is the AutoIT script I am using I am passing the username and password via cmd but it is not working, any suggestion?
       
      #include <IE.au3>
      Local $url ="https://www.Intra.edwa.com"
      Local $oIE =_IECreate($url)
      _IELoadWait($oIE)
      Local $oUser =_IEGetObjById($oIE,"usernameField")
      Local $oPass =_IEGetObjById($oIE,"passwordField")
      _IEFormElementSetValue($oUser, $CmdLine[1])
      _IEFormElementSetValue($oPass, $CmdLine[2])
      _IELoadWait($oIE)
      $oLinks = _IETagNameGetCollection($oIE, "input")
      For $oLink In $oLinks
          If String($oLink.type) = "submit" And String($oLink.value) = "Sign In" Then
                _IEAction($oLink, "click")
                ExitLoop
          EndIf
      Next
    • By hek
      Hey everyone,
      Was wondering how I would be able to implement this on a local computer instead of using connectserver? 
      Any suggestions or help would be appreciated. Thanks. 
    • By DirtyJohny
      Hi everyone.Need rewrite this function how in еxample.
      Original:
      #RequireAdmin #NoTrayIcon Opt("MustDeclareVars",1) Func _a() Local $sls=ObjGet("winmgmts:{impersonationLevel=impersonate," _ &"authenticationLevel=Pkt}!\\"& _ @ComputerName&'\root\wmi'),$lss=$sls.ExecQuery _ ('SELECT * FROM WmiMonitorID'), _ $lll,$sll,$sss="",$lsl,$lls,$i,$z For $z In $lss $lsl=$z.UserFriendlyName For $i=0 To Ubound($lsl)-1 if ($lsl[$i]) Then $lll&=Chr($lsl[$i]) Next $lls=$z.SerialNumberID For $i=0 To Ubound($lls)-1 if ($lls[$i]) Then $sll&=Chr($lls[$i]) Next $sss&="Model"&@TAB&@TAB&": "&$lll&@CR&"Serial Number"&@TAB&": "&$sll&@CR&@CR $lll="" $sll="" Next MsgBox(262144,'Monitors '&$lss.Count,$sss&" "&@CR) $lss=Null $sls=Null EndFunc _a() Example:
      Func _InfoPC() Local $ObjService = ObjGet('winmgmts:{impersonationLevel = impersonate}!\\' & @ComputerName & '\root\cimv2') Local $ObjMB = $ObjService.ExecQuery('SELECT * FROM Win32_BaseBoard', 'WQL', 0x30) If IsObj($ObjService) Then For $objItemMB In $ObjMB $sInfo &= @TAB & 'Motherboard: ...... ' & $objItemMB.Product & @CRLF I'm beginner in this sphere and need  you all speak easy and simply because i'm Russian.Thanks)
    • By shital
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** RunAs(test", @ComputerName, "testinng",2,"wmic product where ""name= '%notepadexamples%'"" call uninstall", @SystemDir & "\wbem", @SW_MAXIMIZE)  it not working 
       
×
×
  • Create New...