• EnglishSpanishGermanFrenchPolishChinese (Traditional)


EnglishSpanishGermanFrenchPolishChinese (Traditional)

Operating systems, scripting, PowerShell and security

Operating systems, software development, scripting, PowerShell tips, network and security

Menú principal
  • Categorías
  • Cursos
  • Libro de PowerShell
  • Lo mejor
  • Lo último
  • Proyectos
  • Contactar
Ir al contenido

Ejecutar un script de PowerShell desde Java

Código del script en PowerShell (guardar como ejecutar.ps1)

PowerShell
1
echo "hola"

Código en Java para ejecutar el script

Java
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
28
29
30
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class ProcesoPowerShell
{
public static void main(String[] args) throws InterruptedException
{
Runtime runtime = Runtime.getRuntime();
try
{
Process process = runtime.exec("powershell.exe  C:\\Users\\juan\\Desktop\\ejecutar.ps1");
process.getOutputStream().close();
String line;
BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = stdout.readLine()) != null){
System.out.println(line);
}
 
TimeUnit.SECONDS.sleep(2);
}
catch(IOException ex){
System.err.println("Error");
System.exit(-1);
}
}
}

Resultado de la ejecución

Publicado el día 17 de enero de 2020

CATEGORÍAS

Java, PowerShell

ETIQUETAS

Código, Echo, IOException, Java, java.io.BufferedReader, java.io.InputStreamReader, java.io.IOException, java.util.concurrent.TimeUnit, Jesús Niño, Jesús Niño Camazón, stdout, System.out.println

MÁS

  • Crear y compilar un código que escribe en un fichero con la propiedad SecurityManager (sin un fichero policy y utilizando un fichero policy) en JAVA
  • Cómo depurar scripts en ISE de Windows PowerShell
  • Ejercicios de PowerShell: realizar operaciones en un equipo remoto
  • Socket SSL en Java
  • Usar XML:DB API para ejecutar una consulta de base de datos con XPath en el servidor eXist-db
  • Cifrar y descifrar con AES desde Java (con clave aleatoria)