Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

✨Simple Java API to interact with PowerShell console

License

NotificationsYou must be signed in to change notification settings

AutoComplete1/jPowerShell2

 
 

Repository files navigation

CircleCI

🧑‍🏫 About

jPowerShell2 is a fork ofjPowerShell fromprofesorfalken. Unfortunately, nothing has been changed on the project since 2019. After I found the project very good and needed it, I decided to develop it further with the name jPowerShell2. If there are any errors or wishes, please feel free to open issues with detailed information.

💻 Installation

To install jPowerShell you can add the dependecy to your software project management tool:https://search.maven.org/artifact/io.github.autocomplete1/jPowerShell2/1.0.3/jar

Maven:

<dependency>    <groupId>io.github.autocomplete1</groupId>   <artifactId>jPowerShell2</artifactId>   <version>1.0.3</version>   <scope>compile</scope></dependency>

Gradle:

implementation 'io.github.autocomplete1:jPowerShell2:1.0.3'

Instead, you can direct download the JAR file and add it to your classpath.
https://repo1.maven.org/maven2/io/github/autocomplete1/jPowerShell2/1.0.3/jPowerShell2-1.0.3.jar

⚡️ Usage

Single command execution

If you only need to execute a single command, this is the quickest way to do it.

//Execute a command in PowerShell sessionPowerShellResponseresponse =PowerShell.executeSingleCommand("Get-Process");//Print resultsSystem.out.println("List Processes:" +response.getCommandOutput());

Executing one or multiple commands using the same PowerShell session

If you have to execute multiple commands, it is recommended to reuse the same session in order to be more efficient (each session has to open a PowerShell console process in the background).

//Creates PowerShell session (we can execute several commands in the same session)try (PowerShellpowerShell =PowerShell.openSession()) {//Execute a command in PowerShell sessionPowerShellResponseresponse =powerShell.executeCommand("Get-Process");//Print resultsSystem.out.println("List Processes:" +response.getCommandOutput());//Execute another command in the same PowerShell sessionresponse =powerShell.executeCommand("Get-WmiObject Win32_BIOS");//Print resultsSystem.out.println("BIOS information:" +response.getCommandOutput());     }catch(PowerShellNotAvailableExceptionex) {//Handle error when PowerShell is not available in the system//Maybe try in another way?   }

You can also choose to execute the same commands with a more fluent style using theexecuteCommandAndChain method:

PowerShell.openSession()                      .executeCommandAndChain("Get-Process", (res ->System.out.println("List Processes:" +res.getCommandOutput())))                      .executeCommandAndChain("Get-WmiObject Win32_BIOS", (res ->System.out.println("BIOS information:" +res.getCommandOutput())))                      .close();

Configure jPowerShell Session

You can easily configure the jPowerShell session:

  • By project creating ajpowershell.properties file in the classpath of your project and settings the variables you want to override.
  • By call, using a map that can be chained to powershell call.

For example:

//Set the timeout when waiting for command to terminate to 30 seconds instead of 10 (default value)Map<String,String>myConfig =newHashMap<>();myConfig.put("maxWait","30000");response =powerShell.configuration(myConfig).executeCommand("Get-WmiObject Win32_BIOS");

The variables that can be configured in jPowerShell are:

waitPause: the pause in ms between each loop pooling for a response. Default value is 10

maxWait: the maximum wait in ms for the command to execute. Default value is 10000

tempFolder: if you set this variable jPowerShell will use this folder in order to store temporary the scripts to execute.
By default the environment variablejava.io.tmpdir will be used.

Setting the PowerShell executable path

If the PowerShell executable has a different name/path on your system, you can change it when opening a new session:

//Creates PowerShell sessiontry (PowerShellpowerShell =PowerShell.openSession("myCustomPowerShellExecutable.exe")) {         [...]

Executing PowerShell Script

In order to execute a PowerShell Script it is recommended to use the executeScript() method instead of executeCommand():

try (PowerShellpowerShell =PowerShell.openSession()) {//Increase timeout to give enough time to the script to finishMap<String,String>config =newHashMap<String,String>();config.put("maxWait","80000");//Execute scriptresponse =powerShell.configuration(config).executeScript("./myPath/MyScript.ps1");//Print results if the scriptSystem.out.println("Script output:" +response.getCommandOutput());     }catch(PowerShellNotAvailableExceptionex) {//Handle error when PowerShell is not available in the system//Maybe try in another way?   }

Executing PowerShell Scripts packaged inside jar

In order to execute a PowerShell Script that is bundled inside a jar you must use a BufferedReader to load the resource:

PowerShellpowerShell =PowerShell.openSession();Stringscript ="resourcePath/MyScript.ps1"StringscriptParams ="-Parameter value"//Read the resourceBufferedReadersrcReader =newBufferedReader(newInputStreamReader(getClass().getResourceAsStream(script)));if (scriptParams !=null && !scriptParams.equals("")) {response =powerShell.executeScript(srcReader,scriptParams);      }else {response =powerShell.executeScript(srcReader);      }

About

✨Simple Java API to interact with PowerShell console

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java100.0%

[8]ページ先頭

©2009-2025 Movatter.jp