Jump to content

Solved - Another 'read registry on remote pc' thread... but different


Recommended Posts

Folks,

As a network admin and computer tech I need (would like) to be able to read the registry of pc's on our LAN. This is all fine and dandy IF the remote pc shares the local user name and password as the local pc, but not otherwise.

I thought I could use WMI to do it but using the test program below, I can't make it work, despite a LOT of time and effort.

If there's another way to do it without installing a dll or other software and without PSExec then I'd love to hear about it.

Regards,

4Eyes

Global $objSWbemLocator, $objWMI, $objReg1, $objReg2, $objMyError
Global $strCPUName, $strComputer, $strUsername, $strPassword, $strPhysicalRAM, $strComputerModel

    $objMyError = ObjEvent("AutoIt.Error", "MyErrFunc")                             ; Install a custom error handler

    $strComputer = "192.168.1.100"      ; IP address of PC to test. Can spec pc by host name or IP address. Local can be "." also.
    ;$strUsername = "administrator"     ; Set credentials here for remote pc if different to local pc
    ;$strPassword = "password"          ; Interesting, if credentials for remote pc are in fact the same as this, p/w spec'ed here is ignored... Hmmm again!?
    $strUsername = ""                   ; Don't supply credentials for local pc
    $strPassword = ""

    ; Set up WMI stuff for simple WMI queries
    $objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
    If Not IsObj($objSWbemLocator) Then
        MsgBox(0, "Error", "Error: failed to create $objSWbemLocator")
        Exit
    EndIf

    ; This is for WMI root\cimv2 domain which is all but AV and registry queries
    $objWMI = $objSWbemLocator.ConnectServer($strComputer, "root\cimv2", $strUsername, $strPassword)    ; This works for remote pc if pass actual credentials and works for local if pass null credentials
    If Not IsObj($objWMI) Then
        MsgBox(0, "Error", "Error: failed to create $objWMI")
        Exit
    EndIf

    $objWMI.Security_.ImpersonationLevel = 3

    ; Step 1 - Use WMI to read ram
    GetRAM()
    MyConsoleWrite("Step 1 - $strPhysicalRAM = " & $strPhysicalRAM & "MB")  ; Works fine

    ; Step 2 - use WMI to read computer model
    GetComputerModel()
    MyConsoleWrite("Step 2 - $strComputerModel = " & $strComputerModel)     ; Works fine

    ; Step 3 - Use WMI to do RegReads - Next line doesn't try to use credentials and so does not do remote regreads if credentials are different
    ; ... but does work if the remote pc and this pc share the same credentials
    $objReg1 = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\default:StdRegProv")   ; This is the usual
    If IsObj($objReg1) Then
        $objReg1.Security_.ImpersonationLevel = 3
        $objReg1.GetStringValue(0x80000002, "HARDWARE\DESCRIPTION\System\CentralProcessor\0\", "ProcessorNameString", $strCPUName)
    Else
        MsgBox(0, "Error", "Error: failed to create $objReg1")
        ;Exit
    EndIf

    MyConsoleWrite("Step 3 - $strCPUName = " & $strCPUName & @CRLF)

    ; Step 4 - Try to read remote reg with appropriate credentials.
    $strCPUName = "failed!"                                                 ; Just to ensure doesn't show previous result
    $objReg2 = $objSWbemLocator.ConnectServer($strComputer, "\root\default:StdRegProv", $strUsername, $strPassword) ; This creates the object, but can't read reg values with it
    If IsObj($objReg2) Then
        ;$objReg2.Security_.ImpersonationLevel = 3                          ; Not sure if I should use this, but makes no difference
        $objReg2.GetStringValue(0x80000002, "HARDWARE\DESCRIPTION\System\CentralProcessor\0\", "ProcessorNameString", $strCPUName)
    Else
        MsgBox(0, "Error", "Error: failed to create $objReg2")
        Exit
    EndIf

    MyConsoleWrite("Step 4 - $strCPUName = " & $strCPUName & @CRLF)

;*********************************

Func GetRAM()

    Local $colItems = ''

    $colItems = $objWMI.ExecQuery("select * from Win32_ComputerSystem")

    If IsObj($colItems) Then
        For $objItem In $colItems
            $strPhysicalRAM = Int($objItem.TotalPhysicalMemory  / (1024 * 1024)) ;  (NB value is in bytes)
        Next
    EndIf

EndFunc     ; End of Func GetRAM()

;*********************************

Func GetComputerModel()

    Local $colItems = ''

    $colItems = $objWMI.ExecQuery("select * from Win32_ComputerSystem", "WQL")

    If IsObj($colItems) Then
        For $objItem In $colItems
            $strComputerModel = $objItem.Model
        Next
    EndIf

    If $strComputerModel = "" Then $strComputerModel = "n/a"

EndFunc     ; End of Func GetComputerModel()

;*********************************

Func MyConsoleWrite($String)

    If Not @Compiled Then ConsoleWrite($String & @CRLF)

EndFunc     ; End of Func MyConsoleWrite()

;*********************************

; This is the WMI error handler. Doesn't need to do anything except be here, else script will crash on error.

Func MyErrFunc()

    MyConsoleWrite("Hmmm... caught a WMI error")

Endfunc     ; End of Func MyErrFunc()

;*********************************

EDIT

As luck would have it, once I got this test prog together and then re-formulated a Google query I found the answer in 10 minutes. For those that may be interested have a look at: http://forums.techarena.in/windows-server-help/893327.htm

There is a little vbs code there that I've translated to au3 and it works! Typical...it's Friday the 13th and I spent a day and a half on this!

$strComputer = "192.168.1.100"
$strUser = "administrator"
$strPassword = "password"

$HKEY_LOCAL_MACHINE = 0x80000002
$strKeyPath = "HARDWARE\DESCRIPTION\System\CentralProcessor\0\"
$strEntryName = "ProcessorNameString"
$strValue = ""

$objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
$objSWbemServices = $objSWbemLocator.ConnectServer($strComputer, "root\default", $strUser, $strPassword)

$objReg = $objSWbemServices.Get("StdRegProv")
$objReg.GetStringValue($HKEY_LOCAL_MACHINE, $strKeyPath, $strEntryName, $strValue)
ConsoleWrite("strValue = " & $strValue & @CRLF)
Edited by 4Eyes
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...