I'm trying to throw together a little .Net (Framework, v4.8) Console Application to check, and potentially update, registry keys on our domain computers. The code I've written work fine on my development machine (Windows 10) - both within Visual Studio and running stand-alone - but refuses to work on our projection servers.
The code is fairly simple - the problem line is:
var remotereg = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName, RegistryView.Default);
This always returns "null" (again, it's fine on my development machine).
Following the advice in this answer, I've wrapped the request in a NetworkConnection object to pass through credentials.
using (new NetworkConnection(@"\\" + machineName + @"\admin$", new NetworkCredential(@"mydomain\myaccount", "mypassword")))
{
var remotereg = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName, RegistryView.Default);
}
With this, I get the slightly-more helpful exception
The network name cannot be found
This is all running from an Administrator account, from a UAC'd Command Prompt (i.e. with Administrator permissions). From that very same prompt, I can ping
the machineName
I'm trying to access, and even
>net use \\machineName\admin$ /user:mydomain\myaccount
The command completed successfully
and
>dir \\machineName\admin$
Volume in drive \\machineName\admin$ has no label.
Volume Serial Number is C28A-5784
Directory of \\machineName\admin$
18/09/2022 21:44 <DIR> .
18/09/2022 21:44 <DIR> ..
22/08/2013 16:39 <DIR> ADFS
13/10/2019 11:24 <DIR> AppCompat
... both suggest that the server I'm running the code from can access the remote server fine - it can resolve the name, it's authenticated, and I can browse file and admin shares. What do I need to do to be able to use OpenRemoteBaseKey
?