gpt4 book ai didi

java - 在提升模式下执行 wmi 命令 Enable-PSRemoting

转载 作者:太空宇宙 更新时间:2023-11-04 14:47:02 24 4
gpt4 key购买 nike

我想在提升模式下执行Enable-PSRemoting。我是 powershell 的新手。我正在 JAVA 中工作并执行 WMI 命令

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hp.hostconfig.windows.ExecuteException;


public class testWMI {

private static final String ENABLEREMOTING = "powershell -Command \"& {Enable-PSRemoting -force}\"";

public static void main(String[] args) throws ExecuteException{
String command =String.format(ENABLEREMOTING);
System.out.println("Command ===> " +command );
String commandArray [] = (command.split(" "));
System.out.println("Command Array " + commandArray);
Runtime runtime = Runtime.getRuntime();
Process proc;
try {
proc = runtime.exec(commandArray);
proc.getOutputStream().close();
List<Map<String, String>> dataList = readNVPData(proc);
System.out.println("Datalist ===> " +dataList);
if (dataList == null) {
// not single line was read, it could be either an error or no
// data has been read.
checkForError(proc);
// since there is no error (no exception thrown) then there is
// no data
return ;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


private static void checkForError(Process proc) throws IOException,
ExecuteException {
List<String> errorList = readError(proc);
if (errorList == null)
// nothing was read, there is no data
return;
System.out.println("ErrorList ===> " + errorList);
if (!errorList.isEmpty()) {

System.out.println("If some data was read ErrorList ===> " + errorList);
// build error line
StringBuilder errorLine = new StringBuilder();
for (String line : errorList) {
if (!line.startsWith("At line:"))
errorLine.append(line);
else
break;
}

// it is split by ":" into Command called and error
String[] errorTokens = errorLine.toString().split(":", 2);
if (errorTokens.length == 2)
throw new ExecuteException(errorTokens[1]);
else {
throw new ExecuteException(errorTokens[0]);
}
} else
throw new ExecuteException("Unknown internal error");
}

private static List<String> readError(Process proc) throws IOException {
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getErrorStream()));
List<String> list = new ArrayList<String>();
String line = null;
boolean readLine = false;
while ((line = reader.readLine()) != null) {
readLine = true;
line = line.trim();
if (!line.isEmpty())
list.add(line);
}
reader.close();
if (!readLine)
return null;
return list;
}

private static List<Map<String, String>> readNVPData(Process proc)
throws IOException {
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
String line = null;
Map<String, String> currentMap = null;
boolean readLine = false;
try {
while ((line = reader.readLine()) != null) {
readLine = true;
line = line.trim();
// System.out.println(line);
if (line.isEmpty()) {
if (currentMap != null && !currentMap.isEmpty()) {
list.add(makeCopy(currentMap));
currentMap = null;
}
} else {
if (currentMap == null)
currentMap = new HashMap<String, String>();
// split
String[] tokens = line.split(":", 2);
if (tokens.length == 2) {
String name = tokens[0].trim();
String value = tokens[1].trim();
currentMap.put(name, value);
}
}
}
} finally {
reader.close();
}
if (!readLine)
return null;
return list;
}

protected static Map<String, String> makeCopy(Map<String, String> orig) {
Map<String, String> copy = new HashMap<String, String>();
copy.putAll(orig);
return copy;
}
}

我得到异常为

Enable-PSRemoting : Access is denied. You need to run this cmdlet from an eleva, ted process.,

谁能建议如何克服这种情况

最佳答案

您应该在提升的 mod 中以管理员身份运行 Java 程序(如错误中所述)。

在提升的 mod 中启动 PowerShell 脚本的方法如下:

$arg = "-file Path-To-A-Script-Fil\YourScript.ps1"
start-process powershell -verb runas –argumentlist $arg

关于java - 在提升模式下执行 wmi 命令 Enable-PSRemoting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24281330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com