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

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();
}
}
}