I need to be able to read the values in a specific Registry Key from a list of Remote Computers. I can do this locally with the following code
using Microsoft.Win32;
RegistryKey rkey = Registry.LocalMachine;
RegistryKey rkeySoftware=rkey.OpenSubKey("Software");
RegistryKey rkeyVendor = rkeySoftware.OpenSubKey("VendorName");
RegistryKey rkeyVersions = rkeyVendor.OpenSubKey("Versions");
String[] ValueNames = rkeyVersions.GetValueNames();
foreach (string name in ValueNames)
{
MessageBox.Show(name + ": " + rkeyVersions.GetValue(name).ToString());
}
but I don't know how to get the same info for a Remote Computer. Am I even using the right approach or should I be looking at WMI or something else?
This is the solution I went with in the end:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// add a reference to Cassia (MIT license)
// https://code.google.com/p/cassia/
using Microsoft.Win32;
namespace RemoteRegistryRead2
{
class Program
{
static void Main(string[] args)
{
String domain = "theDomain";
String user = "theUserName";
String password = "thePassword";
String host = "machine-x11";
using (Cassia.UserImpersonationContext userContext = new Cassia.UserImpersonationContext(domain + "\\" + user, password))
{
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
System.Console.WriteLine("userName: " + userName);
RegistryKey baseKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, host);
RegistryKey key = baseKey.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName");
String computerName = key.GetValue("ComputerName").ToString();
Console.WriteLine(computerName);
}
}
}
}
Works like a charm :]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With