jPowerShell 执行PowerShell脚本,环境必须为Windows系统

https://github.com/profesorfalken/jPowerShell


Installation

To install jPowerShell you can add the dependecy to your software project management tool: http://mvnrepository.com/artifact/com.profesorfalken/jPowerShell/1.7

For example, for Maven you have just to add to your pom.xml:

maven工程依赖导入第三方jar

<dependency>
<groupId>com.profesorfalken</groupId>
<artifactId>jPowerShell</artifactId>
<version>1.7</version>
</dependency>

Instead, you can direct download the JAR file and add it to your classpath. https://repo1.maven.org/maven2/com/profesorfalken/jPowerShell/1.7/jPowerShell-1.7.jar
Basic Usage

The best way to document is providing a good example:
Single command execution

//Execute a command in PowerShell session
PowerShellResponse response = PowerShell.executeSingleCommand("Get-Process");

//Print results
System.out.println("List Processes:" + response.getCommandOutput());

Executing one or multiple commands using the same PowerShell session

PowerShell powerShell = null;
try {
//Creates PowerShell session (we can execute several commands in the same session)
powerShell = PowerShell.openSession();

//Execute a command in PowerShell session
PowerShellResponse response = powerShell.executeCommand("Get-Process");

//Print results
System.out.println("List Processes:" + response.getCommandOutput());

//Execute another command in the same PowerShell session
response = powerShell.executeCommand("Get-WmiObject Win32_BIOS");

//Print results
System.out.println("BIOS information:" + response.getCommandOutput());
} catch(PowerShellNotAvailableException ex) {
//Handle error when PowerShell is not available in the system
//Maybe try in another way?
} finally {
//Always close PowerShell session to free resources.
if (powerShell != null)
powerShell.close();
}

Configure jPowerShell Session

We can easily configure the jPowerShell session:

By project creating a jpowershell.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)
Map<String, String> myConfig = new HashMap<>();
myConfig.put("maxWait", "30000");
response = powerShell.configure(myConfig).executeCommand("Get-WmiObject Win32_BIOS");

The three variables that can be configured in jPowerShell are:

maxThreads: the maximum number of thread to use in pool. 3 is an optimal and default value

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

remoteMode: it should be true when we are executing a command in remote. Otherwise the execution will finish in timeout.
Executing PowerShell Script

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

PowerShellResponse response = null;
try {
//Creates PowerShell session
PowerShell powerShell = PowerShell.openSession();
//Increase timeout to give enough time to the script to finish
Map<String, String> config = new HashMap<String, String>();
config.put("maxWait", "80000");

//Execute script
response = powerShell.configuration(config).executeScript("./myPath/MyScript.ps1");

//Print results if the script
System.out.println("Script output:" + response.getCommandOutput());
} catch(PowerShellNotAvailableException ex) {
//Handle error when PowerShell is not available in the system
//Maybe try in another way?
} finally {
//Always close PowerShell session to free resources.
if (powerShell != null)
powerShell.close();
}


PowerShell 打开一个Session会话,一个会话对象可以执行多个脚本命令,站点验证连通性的办法,直接执行SCMMServer 判断返回的IsConnect是否为True
获取VM,获取Hosts,GetCluster ,GetNetWork
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值