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?