Befehle auf Terminalserver ausführen

  • VB.NET

Es gibt 14 Antworten in diesem Thema. Der letzte Beitrag () ist von nemesis.

    Befehle auf Terminalserver ausführen

    Hallo,
    ich möchte mir ein kleines Tool schreiben, mit dem ich auf einem Terminalserver bestimmte scripts ausführen kann.
    Ich habe dazu diesen Artikel in der MSDN gefunden, aber er bearbeitet nur die Ausführung auf dem lokalen System.

    Geht das auch auf einem anderen System? Wie muss hier dann vorgegangen werden?

    Vielen Dank!
    SecureString??

    Was genau hat das mit Remoteausführung von Scripts zu tun?
    Was ist denn genau dein Ziel ?
    Ich vermute, WMI dürfte dir weiterhelfen.

    Ansonsten kann ich noch PSExec empfehlen.

    Gruss Mono
    Das ist meine Signatur und sie wird wunderbar sein!
    Also, etwas detaillierter:
    Ich möchte z.B. von meinem Client aus auf einem Terminalserver die Anmeldung (CHANGE LOGON) deaktivieren können, oder deren Status abfragen.
    Weiters habe ich auf einem Terminalserver ein Script liegen, das mir Dateien Kopiert und verschiebt, dies würde ich auch gerne übers Netzwerk von einem Programm aus aufrufen können.
    Also, ich glaube das hat mich etwas weitergebracht, danke Renati!

    jetzt habe ich:

    Quellcode

    1. Function Start_Script() As String
    2. Dim Process As New Process()
    3. ' Get the path that stores user documents.
    4. Dim myDesktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
    5. Process.StartInfo.FileName = myDesktopPath + "\Konsole.bat"
    6. Process.StartInfo.UseShellExecute = True
    7. Process.StartInfo.CreateNoWindow = False
    8. Label1.Text = Process.StartInfo.FileName
    9. Process.Start()
    10. End Function

    das führt das Script auf der lokalen Maschiene aus, aber wie ich das jetzt mittels WMI auf dem Server machen kann, das habe ich noch nicht gefunden.

    edit:

    ich glaube ich habs:

    VB.NET-Quellcode

    1. Sub Start_Process(ByVal strComputer As String, ByVal strProcess As String, ByVal UserName As String, ByVal Password As String)
    2. Dim processBatch As ManagementClass = New ManagementClass("Win32_Process")
    3. Dim inParams As ManagementBaseObject = processBatch.GetMethodParameters("Create")
    4. Dim msc As ManagementScope
    5. inParams("CurrentDirectory") = Nothing
    6. inParams("CommandLine") = strProcess
    7. Dim co As ConnectionOptions = New ConnectionOptions()
    8. co.Username = "user"
    9. co.Password = "password"
    10. Try
    11. If (strComputer = System.Environment.MachineName) Then
    12. msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2")
    13. Else
    14. msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
    15. End If
    16. msc.Connect()
    17. processBatch.Scope = msc
    18. Dim meyhodoptions As InvokeMethodOptions = New InvokeMethodOptions(Nothing, System.TimeSpan.MaxValue)
    19. Dim outParamas As ManagementBaseObject = Nothing
    20. outParamas = processBatch.InvokeMethod("Create", inParams, Nothing)
    21. Catch ex As Exception
    22. End Try
    23. End Sub

    muss ich gleichmorgen mal testen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „nemesis“ ()

    mein vorschlag als Protokoll wäre ssh (ebenfalls unter Windows möglich)
    sshwindows.sourceforge.net/

    damit sollte sich das ganze relativ einfach und sicher gestalten lassen
    Ok, nun habe ich ein paar Codezeilen gefunden, mit denen sich der gewünschte Befehl ausführen lasst. Aber, wie kann ich das nun auf dem Remote-Rechner machen?

    VB.NET-Quellcode

    1. Dim p As New System.Diagnostics.Process
    2. p.StartInfo.FileName = "change.exe"
    3. p.StartInfo.Arguments = "logon /query"
    4. p.StartInfo.RedirectStandardOutput = True
    5. p.StartInfo.UseShellExecute = False
    6. p.StartInfo.CreateNoWindow = False
    7. p.Start()
    8. p.WaitForExit()
    9. Dim sResult As String = p.StandardOutput.ReadToEnd
    10. TextBox1.Text = sResult

    so sollte die Ausgabe eigentlich in der Textbox erscheinen, wenn ich das mit einem ping versuche, dann funktioniert das auch, der oping erscheint in der Textbox. Mit "change logon" funktioniert es nicht.
    Auch weiss ich nicht, wie sich das so auf dem remote-rechner ausführen lasst.

    Mit diesem Code lässt sich ein Prozess auf einer entfernten Maschine starten:

    VB.NET-Quellcode

    1. Sub Start_Process(ByVal strComputer As String, ByVal strProcess As String, ByVal UserName As String, ByVal Password As String)
    2. Dim processBatch As ManagementClass = New ManagementClass("Win32_Process")
    3. Dim inParams As ManagementBaseObject = processBatch.GetMethodParameters("Create")
    4. Dim msc As ManagementScope
    5. inParams("CurrentDirectory") = Nothing
    6. inParams("CommandLine") = strProcess
    7. Dim co As ConnectionOptions = New ConnectionOptions()
    8. co.Username = UserName
    9. co.Password = Password
    10. Try
    11. If (strComputer = System.Environment.MachineName) Then
    12. msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2")
    13. Else
    14. msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2", co)
    15. End If
    16. msc.Connect()
    17. processBatch.Scope = msc
    18. Dim meyhodoptions As InvokeMethodOptions = New InvokeMethodOptions(Nothing, System.TimeSpan.MaxValue)
    19. Dim outParamas As ManagementBaseObject = Nothing
    20. outParamas = processBatch.InvokeMethod("Create", inParams, Nothing)
    21. Catch ex As Exception
    22. End Try
    23. End Sub

    Aber, wie bekomm ich hier wieder den obigen code rein?

    vielen Dank!
    Ok, ich habe noch einen anderen Lösungsansatz gefunden: PsExec aus den Sysinternales

    VB.NET-Quellcode

    1. Dim p As New System.Diagnostics.Process
    2. p.StartInfo.FileName = "H:\Software\Sysinternales\ps-Tools\PsExec.exe "
    3. p.StartInfo.Arguments = "\\W2K3srv change logon /query"
    4. MsgBox(p.StartInfo.FileName & p.StartInfo.Arguments)
    5. p.StartInfo.RedirectStandardOutput = True
    6. p.StartInfo.UseShellExecute = False
    7. 'p.StartInfo.CreateNoWindow = False
    8. p.Start()
    9. p.WaitForExit()
    10. Dim sResult As String = p.StandardOutput.ReadToEnd
    11. TextBox1.Text = sResult

    Aber, so öffnet er nur kurz eine konsole und schliesst diese gleich wieder. sdie standardausgabe erfolgt wohl auf StandardError, aber in diesem erhalte ich nur die Ausgabe von psexec, dass change logon aufgerufen wurde ist nicht ersichtlich.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „nemesis“ ()