Entwickler-Ecke

Netzwerk - Windows-Dienste remote steuern


3marci - Di 24.05.11 11:07
Titel: Windows-Dienste remote steuern
Hallo Community,
ich würde ganz gerne per Csharp Script einen Dienst auf einem anderen PC ein/ausschalten.
Leider komme ich mit dem Script nicht wirklich vorran... hoffe ihr könnt mir weiterhelfen.

Hier ist das Script das eine Verbindung zum anderen PC aufbauen soll:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern bool LogonUser(string userName, string domain, string password, int logonType, int logonProvider, ref IntPtr accessToken);

string sMachine = "192.168.11.109";
string sService = "FSMA";
string sUsername = "dom1\\Administrator";
string sPassword = Settings.GetPassword();

const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
IntPtr accessToken = IntPtr.Zero;
bool success = LogonUser(sUsername, sMachine, sPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref accessToken);
if (!success)
{
  MessageBox.Show("geht net\n" + Marshal.GetLastWin32Error());
}
else
{
  WindowsIdentity identity = new WindowsIdentity(accessToken);
  WindowsImpersonationContext impContext = identity.Impersonate();
  
  ServiceController sc2 = new ServiceController(sService, sMachine);
  MessageBox.Show(sc2.Status.ToString());
  sc2 = null;

  impContext.Undo();
}


Der Wert success steht bei mir immer auf false.
Habt ihr eine Idee wie ich die Verbindung richtig herstelle bzw. was an dem Script falsch ist?


Yogu - Di 24.05.11 18:56

Hallo,

LogonUser im MSDN [http://msdn.microsoft.com/en-us/library/aa378184%28v=vs.85%29.aspx]:
The LogonUser function attempts to log a user on to the local computer. The local computer is the computer from which LogonUser was called. You cannot use LogonUser to log on to a remote computer.

So wie ich das verstanden habe, versuchst du genau das.

Edit: Du könntest dir mal Windows Management Instrumentation (WMI) [http://msdn.microsoft.com/en-us/library/aa394582%28v=vs.85%29.aspx] anschauen.

Grüße,
Yogu