- 2025-10-05 1.1.5 Nueva funcion EjecutarScript

This commit is contained in:
2025-10-05 12:38:25 +02:00
parent d822d4e579
commit 2cefca5f8b
2 changed files with 54 additions and 2 deletions

View File

@@ -1,8 +1,59 @@
Public Class Sistema
Imports System.IO
Imports System.Diagnostics
Public Class Sistema
Public Shared Sub EjecutaFichero(Fichero As String)
Dim p As New Process
p.StartInfo = New ProcessStartInfo(Fichero)
p.StartInfo.UseShellExecute = True
p.Start()
End Sub
Public Shared Function EjecutarScript(rutaScript As String, Optional argumentos As String = "") As Integer
Try
Dim extension As String = Path.GetExtension(rutaScript).ToLower()
Dim proceso As New Process()
proceso.StartInfo.UseShellExecute = False
proceso.StartInfo.RedirectStandardOutput = True
proceso.StartInfo.RedirectStandardError = True
proceso.StartInfo.CreateNoWindow = True
Select Case extension
Case ".bat", ".cmd"
proceso.StartInfo.FileName = "cmd.exe"
proceso.StartInfo.Arguments = "/c """ & rutaScript & """ " & argumentos
Case ".ps1"
proceso.StartInfo.FileName = "powershell.exe"
proceso.StartInfo.Arguments = "-ExecutionPolicy Bypass -File """ & rutaScript & """ " & argumentos
Case ".exe"
proceso.StartInfo.FileName = rutaScript
proceso.StartInfo.Arguments = argumentos
Case Else
proceso.StartInfo.FileName = rutaScript
proceso.StartInfo.Arguments = argumentos
proceso.StartInfo.UseShellExecute = True
End Select
AddHandler proceso.OutputDataReceived, Sub(sender, e)
If e.Data IsNot Nothing Then Console.WriteLine("OUT: " & e.Data)
End Sub
AddHandler proceso.ErrorDataReceived, Sub(sender, e)
If e.Data IsNot Nothing Then Console.WriteLine("ERR: " & e.Data)
End Sub
proceso.Start()
proceso.BeginOutputReadLine()
proceso.BeginErrorReadLine()
proceso.WaitForExit()
Return proceso.ExitCode
Catch ex As Exception
Throw New Exception("Error al ejecutar el script: " & rutaScript & " parámetros: " & ex.Message)
End Try
End Function
End Class