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

时间:2014-06-18 09:01:33

标签: java powershell wmi powershell-remoting

我想在提升模式下执行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.,

任何人都可以建议如何克服这种情况

1 个答案:

答案 0 :(得分:0)

您应该以高架模式(如错误中所述)以管理员身份运行Java程序。

在高架mod中启动PowerShell脚本的方法如下:

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