1
我想在提升模式下執行Enable-PSRemoting
。 我新手powershell.I我在JAVA工作,excuting WMI命令執行wmi命令在提升模式下啓用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.,
任何人都可以提出如何克服這種情況下
感謝您的答覆。我想這是'$ ARG =「啓用,PSRemoting」 開始處理PowerShell的 - 動詞runas -argumentlist $ arg' ...但是這打開了一個新的命令提示符..我不想打開新窗口..如何實現這一目標? – user2629457
類似於'$ arg =「Enable-PSRemoting; exit」; start-process powershell -verb runas -argumentlist $ arg ...' – JPBlanc
didnot ....我累了''開始 - 進程powershell -verb runas -argumentlist $ arg -WindowStyle Hidden'。但不會返回任何價值..任何線索在這 – user2629457