375 lines
12 KiB
VB.net
375 lines
12 KiB
VB.net
Imports System.IO
|
||
Imports System.Management.Automation
|
||
Imports System.Management.Automation.Runspaces
|
||
Imports System.Security
|
||
Imports System.Diagnostics
|
||
Imports System.Text
|
||
Imports System.Web
|
||
Imports iTextSharp.text.pdf.codec
|
||
|
||
Public Class cExtProgramme
|
||
|
||
Public Shared Function startlink(URL As String, Optional ByVal p As String = "")
|
||
|
||
Try
|
||
If URL.Contains("http") Or URL.Contains("https") Then
|
||
If p = "" Then
|
||
Process.Start(URL)
|
||
ElseIf p = "edge" Then
|
||
Dim edge As New Process
|
||
edge.StartInfo.FileName = "msedge.exe"
|
||
edge.StartInfo.Arguments = " " & URL
|
||
edge.Start()
|
||
End If
|
||
ElseIf URL = "" Then
|
||
Exit Function
|
||
Else
|
||
Dim mstsc As New Process
|
||
mstsc.StartInfo.FileName = "mstsc.exe"
|
||
mstsc.StartInfo.Arguments = " /v: " & URL
|
||
mstsc.Start()
|
||
|
||
End If
|
||
|
||
' Process.Start("mstsc.exe", "/v: dc01")
|
||
Catch ex As Exception
|
||
'MsgBox(ex.Message)
|
||
|
||
End Try
|
||
|
||
End Function
|
||
|
||
|
||
Public Shared Function startputty(IP As String, Optional ByVal Port As Integer = 22)
|
||
Dim Benutzer, Passwort As String
|
||
Class1.GetUserPasswort(IP, Benutzer, Passwort)
|
||
Try
|
||
Dim putty As New Process
|
||
putty.StartInfo.FileName = "putty.exe"
|
||
If Benutzer = "" And Passwort = "" Then
|
||
putty.StartInfo.Arguments = IP & " -P " & Port
|
||
Else
|
||
putty.StartInfo.Arguments = IP & " -P " & Port & " -l " & Benutzer & " -pw " & Passwort
|
||
End If
|
||
putty.Start()
|
||
|
||
Catch ex As Exception
|
||
MsgBox(ex.Message)
|
||
|
||
End Try
|
||
End Function
|
||
|
||
Public Shared Function commandcmd(command As String)
|
||
|
||
Dim cmd As New Process
|
||
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
|
||
cmd.StartInfo.FileName = "cmd.exe"
|
||
cmd.StartInfo.Arguments = "/c " & command
|
||
cmd.Start()
|
||
|
||
End Function
|
||
|
||
Public Shared Function pingip(ip As String, Optional ByVal Argument As String = "")
|
||
Dim ping As New Process
|
||
'ping.StartInfo.FileName = "cmd.exe"
|
||
'ping.StartInfo.Arguments = " /k ping " & ip & Argument
|
||
ping.StartInfo.FileName = "powershell.exe"
|
||
ping.StartInfo.Arguments = " /noexit /c ping " & ip & Argument
|
||
ping.Start()
|
||
End Function
|
||
|
||
Public Shared Function RunRemotePSScript(ByVal connectTo As String, ByVal user As String, ByVal password As String, ByVal script As String, Optional ByVal ps1 As Boolean = False) As System.Collections.ObjectModel.Collection(Of PSObject)
|
||
Const SHELL_URI As String = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell"
|
||
Dim serverUri As New Uri("http://" & connectTo & ":5985/wsman")
|
||
|
||
Dim securepass As New SecureString
|
||
Dim c As Char
|
||
For Each c In password
|
||
securepass.AppendChar(c)
|
||
Next
|
||
Dim remotecred As New PSCredential(user, securepass)
|
||
|
||
Dim connectionInfo As New WSManConnectionInfo(serverUri, SHELL_URI, remotecred)
|
||
Dim myRunSpace As Runspace = RunspaceFactory.CreateRunspace(connectionInfo)
|
||
Dim psresult As New System.Collections.ObjectModel.Collection(Of PSObject)
|
||
myRunSpace.Open()
|
||
|
||
Dim psh As PowerShell = PowerShell.Create()
|
||
psh.Runspace = myRunSpace
|
||
|
||
If ps1 = False Then
|
||
psh.AddScript(script)
|
||
Else
|
||
psh.AddCommand(script)
|
||
End If
|
||
|
||
psh.Invoke(Nothing, psresult)
|
||
Return psresult
|
||
|
||
psh.Dispose()
|
||
myRunSpace.Close()
|
||
End Function
|
||
|
||
Public Shared Function msgsend(argumente As String, Optional ByVal ReturnOrder As Boolean = False)
|
||
Try
|
||
Dim msg As New Process
|
||
System.IO.File.WriteAllBytes("msg.exe", My.Resources.msg)
|
||
msg.StartInfo.FileName = "msg.exe"
|
||
'msg.StartInfo.WorkingDirectory = "C:\Windows\System32\"
|
||
msg.StartInfo.Arguments = argumente
|
||
If ReturnOrder = True Then Return "msg.exe " & argumente : Exit Function
|
||
msg.Start()
|
||
Catch ex As Exception
|
||
MsgBox(ex.Message)
|
||
End Try
|
||
End Function
|
||
|
||
Public Shared Function msg2server(nachricht As String, server As String)
|
||
Dim batfile As String = "msgsend.bat"
|
||
Dim args As String = """" & nachricht & """" & " " & server
|
||
' MsgBox(args)
|
||
|
||
Dim psi As New ProcessStartInfo()
|
||
psi.FileName = "cmd.exe"
|
||
psi.Arguments = "/c """ & batFile & " " & args & """"
|
||
psi.RedirectStandardOutput = True
|
||
psi.UseShellExecute = False
|
||
psi.CreateNoWindow = True
|
||
|
||
Try
|
||
Dim process As Process = Process.Start(psi)
|
||
Dim output As String = process.StandardOutput.ReadToEnd()
|
||
process.WaitForExit()
|
||
Console.WriteLine(output)
|
||
Catch ex As Exception
|
||
Console.WriteLine("Fehler beim Starten der Batch-Datei: " & ex.Message)
|
||
End Try
|
||
End Function
|
||
|
||
Public Shared Function StartTeamviewer(ID As String, Optional Passwort As String = "BmWr501956")
|
||
Dim p As String = ""
|
||
If Passwort.Length < 1 Then
|
||
Passwort = "BmWr501956"
|
||
End If
|
||
If File.Exists("C:\Program Files (x86)\TeamViewer\Teamviewer.exe") Then
|
||
p = "C:\Program Files (x86)\TeamViewer"
|
||
Else
|
||
p = "C:\Program Files\TeamViewer"
|
||
End If
|
||
Dim teamviewer As New Process
|
||
With teamviewer.StartInfo
|
||
.FileName = "Teamviewer.exe"
|
||
.WorkingDirectory = p
|
||
.Arguments = "-i " & ID & " -P " & Passwort
|
||
End With
|
||
Try
|
||
teamviewer.Start()
|
||
Catch ex As Exception
|
||
MsgBox(ex.Message)
|
||
End Try
|
||
End Function
|
||
|
||
|
||
|
||
Public Shared Function RunAnyDeskWithPassword(id As String, Optional password As String = "BmWr501956@4975!") As String
|
||
Dim output As New StringBuilder()
|
||
Dim err As New StringBuilder()
|
||
|
||
Try
|
||
Dim processInfo As New ProcessStartInfo("cmd.exe")
|
||
processInfo.RedirectStandardInput = True
|
||
processInfo.RedirectStandardOutput = True
|
||
processInfo.RedirectStandardError = True
|
||
processInfo.UseShellExecute = False
|
||
processInfo.CreateNoWindow = True
|
||
|
||
Using process As Process = Process.Start(processInfo)
|
||
Dim command As String = $"echo {password} | ""{Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)}\AnyDesk\AnyDesk-ad_d4025ddb.exe"" {id} --with-password"
|
||
process.StandardInput.WriteLine(command)
|
||
process.StandardInput.Close()
|
||
|
||
' output.Append(process.StandardOutput.ReadToEnd())
|
||
'err.Append(process.StandardError.ReadToEnd())
|
||
|
||
'process.WaitForExit()
|
||
End Using
|
||
|
||
If err.Length > 0 Then
|
||
Console.WriteLine("Fehler: " & err.ToString())
|
||
End If
|
||
|
||
Return output.ToString()
|
||
Catch ex As Exception
|
||
Console.WriteLine("Fehler bei der Ausführung: " & ex.Message)
|
||
Return String.Empty
|
||
End Try
|
||
End Function
|
||
|
||
|
||
Public Shared Function PipeStringToProgram(programPath As String, input As String) As String
|
||
Dim output As New StringBuilder()
|
||
Dim err As New StringBuilder()
|
||
|
||
Try
|
||
Dim startInfo As New ProcessStartInfo()
|
||
startInfo.FileName = programPath
|
||
startInfo.UseShellExecute = False
|
||
startInfo.RedirectStandardInput = True
|
||
startInfo.RedirectStandardOutput = True
|
||
startInfo.RedirectStandardError = True
|
||
startInfo.CreateNoWindow = False
|
||
|
||
Using process As New Process()
|
||
process.StartInfo = startInfo
|
||
process.Start()
|
||
|
||
' Write the input string to the standard input of the process
|
||
Using writer As StreamWriter = process.StandardInput
|
||
writer.WriteLine(input)
|
||
End Using
|
||
|
||
' Read the output and error streams
|
||
output.Append(process.StandardOutput.ReadToEnd())
|
||
err.Append(process.StandardError.ReadToEnd())
|
||
|
||
process.WaitForExit()
|
||
End Using
|
||
|
||
If err.Length > 0 Then
|
||
Console.WriteLine("Error: " & err.ToString())
|
||
End If
|
||
|
||
Return output.ToString()
|
||
Catch ex As Exception
|
||
Console.WriteLine("Error executing program: " & ex.Message)
|
||
Return ex.Message
|
||
End Try
|
||
End Function
|
||
|
||
Public Shared Function RDPConnect(host As String, Optional ByVal Username As String = "", Optional ByVal Password As String = "", Optional ByVal Fenster As Boolean = False, Optional ByVal admin As Boolean = False)
|
||
Dim mstsc As New Process
|
||
Dim AnmeldeDatenVorhanden As Boolean = False
|
||
Dim rdpASadmin As String = ""
|
||
If admin Then rdpASadmin = " /admin"
|
||
Dim FensterString As String = ""
|
||
|
||
If Fenster = True Then
|
||
FensterString = " /w:1920 /h:1080"
|
||
Else
|
||
FensterString = " /f"
|
||
End If
|
||
|
||
If host = "rdpintern.verag.ag" Then
|
||
commandcmd("cmdkey /delete:" & host)
|
||
commandcmd("cmdkey /delete:TERMSRV/" & host)
|
||
ElseIf host.StartsWith("TS") Then
|
||
rdpASadmin = " /admin"
|
||
End If
|
||
|
||
If Username.Length > 0 And Password.Length > 0 Then
|
||
AnmeldeDatenVorhanden = True
|
||
commandcmd("cmdkey /generic:" & host & " /user:" & Username & " /pass:" & Password)
|
||
Threading.Thread.Sleep(2000)
|
||
End If
|
||
|
||
''1 RDP Datei erstellen
|
||
'Dim rdpfile As String = RDPFileErstellen(host)
|
||
|
||
'mstsc.StartInfo.FileName = "mstsc.exe"
|
||
'mstsc.StartInfo.UseShellExecute = False
|
||
''mstsc.StartInfo.Arguments = " /v: " & host & rdpASadmin & FensterString
|
||
'mstsc.StartInfo.Arguments = " " & rdpfile
|
||
|
||
'2 direkt verbinden
|
||
|
||
mstsc.StartInfo.FileName = "mstsc.exe"
|
||
mstsc.StartInfo.UseShellExecute = False
|
||
mstsc.StartInfo.Arguments = " /v: " & host & rdpASadmin & FensterString
|
||
|
||
|
||
mstsc.Start()
|
||
|
||
If AnmeldeDatenVorhanden = True Then
|
||
Threading.Thread.Sleep(5000)
|
||
|
||
Dim Abmeldung As New Process
|
||
commandcmd("cmdkey /delete:" & host)
|
||
|
||
End If
|
||
|
||
|
||
|
||
End Function
|
||
|
||
Public Shared Function RDPFileErstellen(targetaddress As String) As String
|
||
|
||
' 1. Zieladresse (kann z. B. per Eingabe oder aus Konfig kommen)
|
||
'Dim targetAddress As String = "haas-vm.verag.ost.dmn" ' <-- Hier Ziel ändern oder dynamisch setzen
|
||
|
||
' 2. Pfad zum Startordner und zur RDP-Datei
|
||
Dim startPath As String = AppDomain.CurrentDomain.BaseDirectory
|
||
Dim rdpFilePath As String = Path.Combine(startPath, "RemoteConnection.rdp")
|
||
|
||
' 3. RDP-Inhalt mit Platzhalter für das Zielsystem
|
||
Dim rdpTemplate As String = "
|
||
screen mode id:i:1
|
||
desktopwidth:i:1920
|
||
desktopheight:i:1080
|
||
session bpp:i:32
|
||
winposstr:s:0,1,-2219,191,-283,1310
|
||
compression:i:1
|
||
prompt for credentials:i:0
|
||
keyboardhook:i:1
|
||
audiocapturemode:i:0
|
||
videoplaybackmode:i:1
|
||
connection type:i:7
|
||
networkautodetect:i:1
|
||
bandwidthautodetect:i:1
|
||
displayconnectionbar:i:1
|
||
enableworkspacereconnect:i:0
|
||
disable wallpaper:i:0
|
||
allow font smoothing:i:0
|
||
allow desktop composition:i:0
|
||
disable full window drag:i:1
|
||
disable menu anims:i:1
|
||
disable themes:i:0
|
||
disable cursor setting:i:0
|
||
bitmapcachepersistenable:i:1
|
||
full address:s:{TARGET}
|
||
audiomode:i:0
|
||
redirectprinters:i:1
|
||
redirectlocation:i:0
|
||
redirectcomports:i:0
|
||
redirectsmartcards:i:1
|
||
redirectwebauthn:i:1
|
||
redirectclipboard:i:1
|
||
redirectposdevices:i:0
|
||
autoreconnection enabled:i:1
|
||
authentication level:i:2
|
||
prompt for credentials:i:0
|
||
negotiate security layer:i:1
|
||
remoteapplicationmode:i:0
|
||
alternate shell:s:
|
||
shell working directory:s:
|
||
gatewayhostname:s:
|
||
gatewayusagemethod:i:4
|
||
gatewaycredentialssource:i:4
|
||
gatewayprofileusagemethod:i:0
|
||
promptcredentialonce:i:0
|
||
gatewaybrokeringtype:i:0
|
||
use redirection server name:i:0
|
||
rdgiskdcproxy:i:0
|
||
kdcproxyname:s:
|
||
enablerdsaadauth:i:0
|
||
use multimon:i:0".Trim()
|
||
|
||
' 4. {TARGET} durch tatsächliche Adresse ersetzen
|
||
Dim rdpContent As String = rdpTemplate.Replace("{TARGET}", targetaddress)
|
||
|
||
' 5. Datei schreiben
|
||
File.WriteAllText(rdpFilePath, rdpContent)
|
||
Return rdpFilePath
|
||
End Function
|
||
|
||
End Class
|