1
승격 모드에서 Enable-PSRemoting
을 실행하고 싶습니다. 나는 powershell.I에 초보자가 WMI 명령을 JAVA에서 작업 Executing (실행 중)입니다입니다wmi 명령 실행 상승 모드에서 Enable-PSRemoting
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.,
사람은
감사합니다 .. 나는 $ 인수 = "-PSRemoting 사용"을 시작 프로세스 파워 쉘'로이 시도 - 동사 runas -argumentlist $ argu '... 그러나 이것은 새로운 명령 프롬프트를 엽니 다. 나는 이걸 달성하기 위해 새 창을 열려고하지 않습니까? – user2629457
'$ arg = "Enable-PSRemoting; exit"; 프로세스 시작 powershell -verb runas -argumentlist $ arg ...' – JPBlanc
didnot work .... 피곤한'start-process powershell -verb runas -argumentlist $ arg -WindowStyle Hidden'. 그러나 어떤 가치도 반환하지 않습니다.이 단서의 단서 – user2629457