I am trying adminstrate a service on a different machine using a ServiceController.
var sc = new ServiceController(serviceName, machine);
Console.WriteLine(sc.Status);
As I need to use different credentials, I perform an impersonation using:
var tokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(userName, domainName, password,
LOGON32_LOGON_NEW_CREDENTIALS,
LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if (!returnValue)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
WindowsIdentity newId = new WindowsIdentity(tokenHandle);
impersonatedUser = newId.Impersonate();
Impersonation seems to work. But I keep receiving an InvalidOperationException:
System.InvalidOperationException: Cannot open MyService service on computer 'TargetMachine'.
---> System.ComponentModel.Win32Exception: Access is denied
My Workstation is on a domain, while the target machine participates in a workgroup.
Any idea about what I am missing here?