Autor Beitrag
3marci
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 61
Erhaltene Danke: 5

Windows 7 / Kubuntu 11.04
C# / vb.net / php / progress (VS 2010 Express / SharpDevelop / NetBeans / proAlpha)
BeitragVerfasst: Di 24.05.11 11:07 
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:
ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Di 24.05.11 18:56 
Hallo,

LogonUser im MSDN:
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) anschauen.

Grüße,
Yogu