init
This commit is contained in:
parent
4499f7cddb
commit
b4b20d870c
1
help.md
Normal file
1
help.md
Normal file
@ -0,0 +1 @@
|
||||
https://learn.microsoft.com/zh-cn/virtualization/hyper-v-on-windows/quick-start/try-hyper-v-powershell
|
23
pom.xml
23
pom.xml
@ -11,7 +11,6 @@
|
||||
<url>https://git.dr1997.com/hyper-v/hyper-v-java</url>
|
||||
|
||||
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
@ -21,15 +20,25 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.profesorfalken</groupId>-->
|
||||
<!-- <artifactId>jPowerShell</artifactId>-->
|
||||
<!-- <version>3.1.1</version>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.profesorfalken</groupId>
|
||||
<artifactId>jPowerShell</artifactId>
|
||||
<version>3.1.1</version>
|
||||
<groupId>com.github.tuupertunut</groupId>
|
||||
<artifactId>powershell-lib-java</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.9.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
12
src/main/java/cn/x47/service/VmService.java
Normal file
12
src/main/java/cn/x47/service/VmService.java
Normal file
@ -0,0 +1,12 @@
|
||||
package cn.x47.service;
|
||||
|
||||
|
||||
/**
|
||||
* hyper-v 虚拟操作相关接口
|
||||
*/
|
||||
public interface VmService {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
59
src/main/java/cn/x47/service/impl/VmServiceImpl.java
Normal file
59
src/main/java/cn/x47/service/impl/VmServiceImpl.java
Normal 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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 );
|
||||
}
|
||||
}
|
31
src/test/java/cn/x47/service/impl/VmServiceImplTest.java
Normal file
31
src/test/java/cn/x47/service/impl/VmServiceImplTest.java
Normal 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();
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user