Read registry from a remote computer

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 822 Views - Last Post: 26 October 2017 - 08:56 AM Rate Topic: -----

#1 mcorben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-October 17

Read registry from a remote computer

Posted 17 October 2017 - 06:33 AM

Hi,
I'm new to the forum. I'm also kind of new to VB.net. What I'm trying to do is write a program that will read the registry and collect some information.
The key I need to read is in HKLM\SOFTWARE\,<custom key>. I need to read it from a remote PC on a company network. I did search the web and I found lots of examples but I need one that will read the key with a username and password. See, I would be running the program with a standard user's credentials but have the program use elevated credentials. I have found no examples on the net whatsoever.
My company stores build information in the registry and I need to read it to display it. Does anyone have a example that shows how to incorporate the username and password to read a remote registry on a network?

Is This A Good Question/Topic? 0
  • +

Replies To: Read registry from a remote computer

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13488
  • View blog
  • Posts: 53,879
  • Joined: 12-June 08

Re: Read registry from a remote computer

Posted 17 October 2017 - 06:43 AM

Isn't this something Active Directory, or another sysops tool, would handle more effectively?
Was This Post Helpful? 0
  • +
  • -

#3 mcorben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-October 17

Re: Read registry from a remote computer

Posted 19 October 2017 - 12:42 PM

Not when you are writing a app that uses it.
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,679
  • Joined: 01-February 13

Re: Read registry from a remote computer

Posted 19 October 2017 - 01:58 PM

There are a few ways to do it. The first is to use the RegistryKey.OpenRemoteBaseKey Method. However, note that the msdn documents say that you need to make sure the remote registry service is running on the machines. There is an example at that link and strewed all over the internet.

The other is to use WMI as shown Here in the 3rd post down and Here in the last post. If you look, they use the ConnectionOptions Class to supply the password and username.
Was This Post Helpful? 1
  • +
  • -

#5 mcorben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-October 17

Re: Read registry from a remote computer

Posted 20 October 2017 - 01:03 PM

Thanks for the links. Some of them were C code but I did find one that looked interesting though. It's not quite working yet after I changed the location to HKLM and the keys. The output should be a computer name. Any ideas from here?

By the way, I love you custom controls, I just looked at them briefly but they look great.

Const HKEY_LOCAL_MACHINE = &H80000002

        Dim RemotePCHostname As String
        RemotePCHostname = "T66131"
        Dim options As New ConnectionOptions
        options.Impersonation = ImpersonationLevel.Impersonate
        options.EnablePrivileges = True
        options.Username = "companyname\user"
        options.Password = "password"

        Dim myScope As New ManagementScope("\\" & RemotePCHostname & "\root\default", options)
        Dim mypath As New ManagementPath("StdRegProv")
        Dim mc As New ManagementClass(myScope, mypath, Nothing)

        Dim inParams As ManagementBaseObject = mc.GetMethodParameters("GetDWORDValue")
        inParams("hDefKey") = UInt32.Parse(HKEY_LOCAL_MACHINE, System.Globalization.NumberStyles.HexNumber) 'RegistryHive.LocalMachine
        inParams("sSubKeyName") = "SOFTWARE\Vanguard\BuildProcess"
        inParams("sValueName") = "ComputerName"

        Dim outParams As ManagementBaseObject = mc.InvokeMethod("GetStringValue", inParams, Nothing)

        If (outParams("ReturnValue").ToString() = "0") Then
            MessageBox.Show(outParams("sValue").ToString())

        Else
            MessageBox.Show("Error retrieving value : " + outParams("ReturnValue").ToString())
        End If


Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,679
  • Joined: 01-February 13

Re: Read registry from a remote computer

Posted 20 October 2017 - 03:01 PM

Quote

Thanks for the links. Some of them were C code but I did find one that looked interesting though.

I forgot to mention that the one was in C# and that you can use a free online C# to VB converter such as the Telerik Code Converter to convert it to VB.Net code.

Quote

It's not quite working yet after I changed the location to HKLM and the keys. The output should be a computer name. Any ideas from here?

If it is a String type that is in the registry key value, then in the line below you should be getting the method parameters for a String type instead of a DWORD type.
Dim inParams As ManagementBaseObject = mc.GetMethodParameters("GetStringValue")

Here is a list of the strings that you would use in the method above to get different types of registry value types....
    GetBinaryValue - reads regisry value of BINARY type
    GetDWORDValue - reads registry value of DWORD type
    GetExpandedStringValue - reads registry value of EXPANDED STRING type
    GetMultiStringValue - reads registry value of MULTI STRING type
    GetStringValue - reads registry value of STRING type



Also, the line below needs to be changed too. They used that line in the second link's example because they had the HKEY_LOCAL_MACHINE declared as a String. The UInt32.Parse method converts that String to an unsigned integer. However, you have the HKEY_LOCAL_MACHINE declared as an Integer type already.
inParams("hDefKey") = UInt32.Parse(HKEY_LOCAL_MACHINE, System.Globalization.NumberStyles.HexNumber)

You can try just changing that line to this...
inParams("hDefKey") = HKEY_LOCAL_MACHINE


Quote

By the way, I love you custom controls, I just looked at them briefly but they look great.

Thanks. Some are good and some are just... well... bla. I have not worked on them in a while to update them with newer/better ideas and methods. 8)
Was This Post Helpful? 0
  • +
  • -

#7 mcorben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-October 17

Re: Read registry from a remote computer

Posted 23 October 2017 - 07:06 AM

Sorry, I'm new to this. It looks like it is still getting stuck. but I think we are almost there.
Const HKEY_LOCAL_MACHINE = &H80000002

        Dim RemotePCHostname As String
        RemotePCHostname = "T93271"
        Dim options As New ConnectionOptions
        options.Impersonation = ImpersonationLevel.Impersonate
        options.EnablePrivileges = True
        options.Username = "Sunguard\username"
        options.Password = "Password"

        Dim myScope As New ManagementScope("\\" & RemotePCHostname & "\root\default", options)
        Dim mypath As New ManagementPath("StdRegProv")
        Dim mc As New ManagementClass(myScope, mypath, Nothing)

        Dim inParams As ManagementBaseObject = mc.GetMethodParameters("GetStringValue")
        
        rem stopping here
        inParams("hDefKey") = HKEY_LOCAL_MACHINE
        inParams("sSubKeyName") = "SOFTWARE\Sunguard\BuildProcess"
        inParams("sValueName") = "ComputerName"

        Dim outParams As ManagementBaseObject = mc.InvokeMethod("GetStringValue", inParams, Nothing)

        If (outParams("ReturnValue").ToString() = "0") Then
            MessageBox.Show(outParams("sValue").ToString())

        Else
            MessageBox.Show("Error retrieving value : " + outParams("ReturnValue").ToString())

        End If

    


Was This Post Helpful? 0
  • +
  • -

#8 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,679
  • Joined: 01-February 13

Re: Read registry from a remote computer

Posted 23 October 2017 - 05:02 PM

Well, unfortunately i don't have my home network set up at this time to do any testing. I have just been going by searching and reading.

So, what is it doing? Is it throwing an exception? I see you have a comment in your code (rem stopping here) but, not sure what you mean by that. If you get an exception then you should post the exact exception message it is giving you.
Was This Post Helpful? 0
  • +
  • -

#9 mcorben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-October 17

Re: Read registry from a remote computer

Posted 24 October 2017 - 06:03 AM

It's stopping at [code][/ inParams("hDefKey") = HKEY_LOCAL_MACHINE /code]
There is no exception or error at all, it just hangs there with a blank form.

The really funny thing is, if I take that line out altogether, the whole programs works.
Not sure why, not sure where it is getting HKEY_LOCAL_MACHINE from?
Was This Post Helpful? 0
  • +
  • -

#10 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,679
  • Joined: 01-February 13

Re: Read registry from a remote computer

Posted 24 October 2017 - 07:32 AM

Quote

The really funny thing is, if I take that line out altogether, the whole programs works.
Not sure why, not sure where it is getting HKEY_LOCAL_MACHINE from?


After looking at the msdn documents for the GetStringValue method of the StdRegProv class it says that the HKEY_LOCAL_MACHINE is the default key for the hDefKey parameter. So, if you don't set it to something else, that is the key it uses. That would explain why it knows to use the HKEY_LOCAL_MACHINE.

Why it hangs there if you do use it is a bit of a mystery but, knowing it uses the key you need by default and it works without specifying it, i would say you are pretty safe to just ignore that line and let it use the default.
Was This Post Helpful? 1
  • +
  • -

#11 mcorben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-October 17

Re: Read registry from a remote computer

Posted 24 October 2017 - 07:56 AM

Thanks for all your help.
Was This Post Helpful? 0
  • +
  • -

#12 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,679
  • Joined: 01-February 13

Re: Read registry from a remote computer

Posted 24 October 2017 - 08:06 AM

You're Welcome. 8)
Was This Post Helpful? 0
  • +
  • -

#13 mcorben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-October 17

Re: Read registry from a remote computer

Posted 24 October 2017 - 10:50 AM

One last question. If I wanted to do multiple registry reads in the same hive, how could I do that? For instance we are pulling BuildTitle key. I want to pull another key there and have it in sValue like the other one.

Dim myScope As New ManagementScope("\\" & RemotePCHostname & "\root\default", options)
        Dim mypath As New ManagementPath("StdRegProv")
        Dim mc As New ManagementClass(myScope, mypath, Nothing)

        Dim inParams As ManagementBaseObject = mc.GetMethodParameters("GetStringValue")
        inParams("sSubKeyName") = "SOFTWARE\Xguard\BuildProcess"
        inParams("sValueName") = "BuildTitle"

        Dim outParams As ManagementBaseObject = mc.InvokeMethod("GetStringValue", inParams, Nothing)

        MessageBox.Show(outParams("sValue").ToString())


Was This Post Helpful? 0
  • +
  • -

#14 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,679
  • Joined: 01-February 13

Re: Read registry from a remote computer

Posted 24 October 2017 - 12:11 PM

I don't see a way to return 2 different key values using the current methods without just calling Invoke a second time to retrieve the second key value. For example, if it is in the same subkey and it is just 2 different values from that key that you want to retrieve....
        Dim myScope As New ManagementScope("\\" & RemotePCHostname & "\root\default", options)
        Dim mypath As New ManagementPath("StdRegProv")
        Dim mc As New ManagementClass(myScope, mypath, Nothing)

        Dim inParams As ManagementBaseObject = mc.GetMethodParameters("GetStringValue")

        inParams("sSubKeyName") = "SOFTWARE\Electronic Arts\3D Data\Card16"
        inParams("sValueName") = "Name"

        Dim outParams As ManagementBaseObject = mc.InvokeMethod("GetStringValue", inParams, Nothing)

        If CInt(outParams("ReturnValue")) = 0 Then
            Label1.Text = outParams("sValue").ToString
        Else
            Label1.Text = "Error retrieving value : " & outParams("ReturnValue").ToString
        End If

        'reuse the same inparams, just change the sValueName parameter and invoke the method again
        inParams("sValueName") = "Group"
        outParams = mc.InvokeMethod("GetStringValue", inParams, Nothing)
        If CInt(outParams("ReturnValue")) = 0 Then
            Label2.Text = outParams("sValue").ToString
        Else
            Label2.Text = "Error retrieving value : " & outParams("ReturnValue").ToString
        End If

        'don't forget to dispose these when you are done using them
        inParams.Dispose()
        outParams.Dispose()
        mc.Dispose()


Was This Post Helpful? 1
  • +
  • -

#15 mcorben  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-October 17

Re: Read registry from a remote computer

Posted 25 October 2017 - 05:09 AM

That's just what I needed, Thanks again, it work perfectly.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2