This commit is contained in:
xking 2023-01-16 01:25:26 +08:00
parent 4499f7cddb
commit b4b20d870c
6 changed files with 119 additions and 45 deletions

1
help.md Normal file
View File

@ -0,0 +1 @@
https://learn.microsoft.com/zh-cn/virtualization/hyper-v-on-windows/quick-start/try-hyper-v-powershell

23
pom.xml
View File

@ -11,7 +11,6 @@
<url>https://git.dr1997.com/hyper-v/hyper-v-java</url> <url>https://git.dr1997.com/hyper-v/hyper-v-java</url>
<properties> <properties>
<java.version>17</java.version> <java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -21,15 +20,25 @@
</properties> </properties>
<dependencies> <dependencies>
<!-- <dependency>-->
<!-- <groupId>com.profesorfalken</groupId>-->
<!-- <artifactId>jPowerShell</artifactId>-->
<!-- <version>3.1.1</version>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>com.profesorfalken</groupId> <groupId>com.github.tuupertunut</groupId>
<artifactId>jPowerShell</artifactId> <artifactId>powershell-lib-java</artifactId>
<version>3.1.1</version> <version>2.0.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>cn.hutool</groupId>
<artifactId>junit</artifactId> <artifactId>hutool-all</artifactId>
<version>4.13.2</version> <version>5.8.11</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,12 @@
package cn.x47.service;
/**
* hyper-v 虚拟操作相关接口
*/
public interface VmService {
}

View File

@ -0,0 +1,59 @@
package cn.x47.service.impl;
import cn.hutool.core.util.ReUtil;
import cn.x47.service.VmService;
import com.github.tuupertunut.powershelllibjava.PowerShell;
import com.github.tuupertunut.powershelllibjava.PowerShellExecutionException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
public class VmServiceImpl implements VmService {
public void test() {
try (PowerShell psSession = PowerShell.open()) {
String s = psSession.executeCommands("Get-VM");
// String s = psSession.executeCommands("Get-VMSwitch");
String[] split = s.split("\\r\\n");
List<String> allGroups = ReUtil.getAllGroups(Pattern.compile("(\\S+\\s+)"), split[1], false, true);
List<Integer> integers = allGroups.stream().map(String::length).toList();
List<List<String>> lists = Arrays.stream(split).skip(3).map(e -> {
List<String> a = new ArrayList<>();
int index = 0;
for (int i = 0; i < integers.size(); i++) {
if (i == 0) {
a.add(e.substring(index, integers.get(i)));
} else {
a.add(e.substring(index, index + integers.get(i)));
}
index += integers.get(i);
}
return a;
}).toList();
lists.forEach(System.out::println);
} catch (IOException |
PowerShellExecutionException ex) {
ex.printStackTrace();
}
}
}

View File

@ -1,38 +0,0 @@
package cn.x47;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

View File

@ -0,0 +1,31 @@
package cn.x47.service.impl;
import cn.x47.service.VmService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class VmServiceImplTest {
static VmServiceImpl vmService;
@BeforeAll
static void setUp() {
vmService = new VmServiceImpl();
}
@AfterEach
void tearDown() {
System.out.println("执行结束");
}
@Test
void test1() {
vmService.test();
}
}