profesorfalken / jPowerShell

Simple Java API to interact with PowerShell console
Apache License 2.0
218 stars 84 forks source link

I encounter a problem when invoking an order, the answer is not displayed correctly, the characters does not respect the utf-8 #64

Closed Abneco closed 5 years ago

Abneco commented 5 years ago

hello, need a little help

I encounter a problem when invoking an order, the answer is not displayed correctly, the characters does not respect the utf-8 ...

new 3.0 : The remote mode is no longer needed. Commands should work on remote as in local. -> the script works but is not well encoded

is there another method, or a way to fix this problem?

here is my code

public String invokePowerShell(String hostname, String nameScript) {
        try (PowerShell powerShell = PowerShell.openSession()) {

            // utf8 encoding problem
            PowerShellResponse responsePS = powerShell.executeCommand("Invoke-Command -ComputerName " + hostname + " -FilePath " + nameScript);

            //Print results
            System.out.println("Script output:" + responsePS.getCommandOutput());
            response = responsePS.getCommandOutput();
            powerShell.close();
            System.out.println("Done");
        } catch (PowerShellNotAvailableException ex) {
            response = ex.toString();
            System.out.println(ex);
        }
        return response;
}

other information (local and remote) CHCP -> 850 PSVersion 5.1.17763.503 WIN 10

PowerShell has no encoding problem, when it is used like this

PowerShellResponse responsePS = powerShell.executeScript("./myPath/MyScript.ps1");

but this code does not work remotely

thank you in advance for your help

Abneco commented 5 years ago

In search of the problem, it would seem that it comes from the recover value, impossible to store in a byte (-128 to 127), and there is no byte unsigned

byte[] byteArray = response.getBytes(); INFOS: 79 INFOS: 112 INFOS: -126 + 256 = 130 = é INFOS: 114 INFOS: 97 INFOS: 116 INFOS: 105 INFOS: 111 INFOS: 110 INFOS: 32 INFOS: 114 INFOS: -126 INFOS: 117 INFOS: 115 INFOS: 115 INFOS: 105 INFOS: 101 INFOS: -1 INFOS: 58 INFOS: 32 INFOS: 108 INFOS: 101 INFOS: 32 INFOS: 112 INFOS: 114 INFOS: 111 INFOS: 99 INFOS: 101 INFOS: 115 INFOS: 115 INFOS: 117 INFOS: 115 INFOS: 32 INFOS: 100 INFOS: 101 INFOS: 32 INFOS: 80 INFOS: 73 INFOS: 68 INFOS: 32 INFOS: 49 INFOS: 51 INFOS: 56 INFOS: 56 INFOS: 52 INFOS: 32 INFOS: 40 INFOS: 112 INFOS: 114 INFOS: 111 INFOS: 99 INFOS: 101 INFOS: 115 INFOS: 115 INFOS: 117 INFOS: 115 INFOS: 32 INFOS: 101 INFOS: 110 INFOS: 102 INFOS: 97 INFOS: 110 INFOS: 116 INFOS: 32 INFOS: 100 INFOS: 101 INFOS: 32 INFOS: 80 INFOS: 73 INFOS: 68 INFOS: 32 INFOS: 50 INFOS: 49 INFOS: 49 INFOS: 51 INFOS: 54 INFOS: 41 INFOS: 32 INFOS: 97 INFOS: 32 INFOS: -126 INFOS: 116 INFOS: -126 INFOS: 13 INFOS: 10 INFOS: 97 INFOS: 114 INFOS: 114 INFOS: -120 INFOS: 116 INFOS: -126 INFOS: 46 INFOS: 13 INFOS: 10

so I create a conversion method


// appelle invokePowerShell

Powershell responsePS = new Powershell();
String response = responsePS.invokePowerShell("192.168.1.51", "C:\\SCRIPT\\Kill-ProcessFirefox.ps1");

byte[] byteArray = response.getBytes();
int[] intArray = convertByteArrayToIntArray(byteArray);
response = cp850ToUTF8(intArray);

// conversion byte array to int array to keep the byte unsigned

    public int[] convertByteArrayToIntArray(byte[] byteArray) {
        int[] intArray = new int[byteArray.length];
        for (int i = 0; i < byteArray.length; i++) {
            if (byteArray[i] < 0) {
                //System.out.println(256 + (byteArray[i]));
                intArray[i] = 256 + (byteArray[i]);
            } else {
                //System.out.println(byteArray[i]);
                intArray[i] = (byteArray[i]);
            }
            //System.out.println(intArray[i]);
        }
        return intArray;
    }

// the bute of this method is to read the int array and convert to a string thanks to the string builder

    public String cp850ToUTF8(int[] intArray) {
        // https://www.wikiwand.com/en/Code_page_437
        // https://www.wikiwand.com/en/Code_page_850
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < intArray.length; i++) {
            switch (intArray[i]) {
                case 0:
                    sb.append("NUL");
                    break;

                case 10:
                    sb.append("\n"); // LF
                    break;

                case 13:
                    sb.append("\r"); // CR
                    break;

                case 32:
                    sb.append(" ");
                    break;
                case 33:
                    sb.append("!");
                    break;
                case 34:
                    sb.append("\"");
                    break;
                case 35:
                    sb.append("#");
                    break;
                case 36:
                    sb.append("$");
                    break;
                case 37:
                    sb.append("%");
                    break;
                case 38:
                    sb.append("&");
                    break;
                case 39:
                    sb.append("'");
                    break;
                case 40:
                    sb.append("(");
                    break;
                case 41:
                    sb.append(")");
                    break;
                case 42:
                    sb.append("*");
                    break;
                case 43:
                    sb.append("+");
                    break;
                case 44:
                    sb.append(",");
                    break;
                case 45:
                    sb.append("-");
                    break;
                case 46:
                    sb.append(".");
                    break;
                case 47:
                    sb.append("/");
                    break;
                case 48:
                    sb.append("0");
                    break;
                case 49:
                    sb.append("1");
                    break;
                case 50:
                    sb.append("2");
                    break;
                case 51:
                    sb.append("3");
                    break;
                case 52:
                    sb.append("4");
                    break;
                case 53:
                    sb.append("5");
                    break;
                case 54:
                    sb.append("6");
                    break;
                case 55:
                    sb.append("7");
                    break;
                case 56:
                    sb.append("8");
                    break;
                case 57:
                    sb.append("9");
                    break;
                case 58:
                    sb.append(":");
                    break;
                case 59:
                    sb.append(";");
                    break;
                case 60:
                    sb.append("<");
                    break;
                case 61:
                    sb.append("=");
                    break;
                case 62:
                    sb.append(">");
                    break;
                case 63:
                    sb.append("?");
                    break;
                case 64:
                    sb.append("@");
                    break;
                case 65:
                    sb.append("A");
                    break;
                case 66:
                    sb.append("B");
                    break;
                case 67:
                    sb.append("C");
                    break;
                case 68:
                    sb.append("D");
                    break;
                case 69:
                    sb.append("E");
                    break;
                case 70:
                    sb.append("F");
                    break;
                case 71:
                    sb.append("G");
                    break;
                case 72:
                    sb.append("H");
                    break;
                case 73:
                    sb.append("I");
                    break;
                case 74:
                    sb.append("J");
                    break;
                case 75:
                    sb.append("K");
                    break;
                case 76:
                    sb.append("L");
                    break;
                case 77:
                    sb.append("M");
                    break;
                case 78:
                    sb.append("N");
                    break;
                case 79:
                    sb.append("O");
                    break;
                case 80:
                    sb.append("P");
                    break;
                case 81:
                    sb.append("Q");
                    break;
                case 82:
                    sb.append("R");
                    break;
                case 83:
                    sb.append("S");
                    break;
                case 84:
                    sb.append("T");
                    break;
                case 85:
                    sb.append("U");
                    break;
                case 86:
                    sb.append("V");
                    break;
                case 87:
                    sb.append("W");
                    break;
                case 88:
                    sb.append("X");
                    break;
                case 89:
                    sb.append("Y");
                    break;
                case 90:
                    sb.append("Z");
                    break;
                case 91:
                    sb.append("[");
                    break;
                case 92:
                    sb.append("\\");
                    break;
                case 93:
                    sb.append("]");
                    break;
                case 94:
                    sb.append("^");
                    break;
                case 95:
                    sb.append("_");
                    break;
                case 96:
                    sb.append("`");
                    break;
                case 97:
                    sb.append("a");
                    break;
                case 98:
                    sb.append("b");
                    break;
                case 99:
                    sb.append("c");
                    break;
                case 100:
                    sb.append("d");
                    break;
                case 101:
                    sb.append("e");
                    break;
                case 102:
                    sb.append("f");
                    break;
                case 103:
                    sb.append("g");
                    break;
                case 104:
                    sb.append("h");
                    break;
                case 105:
                    sb.append("i");
                    break;
                case 106:
                    sb.append("j");
                    break;
                case 107:
                    sb.append("k");
                    break;
                case 108:
                    sb.append("l");
                    break;
                case 109:
                    sb.append("m");
                    break;
                case 110:
                    sb.append("n");
                    break;
                case 111:
                    sb.append("o");
                    break;
                case 112:
                    sb.append("p");
                    break;
                case 113:
                    sb.append("q");
                    break;
                case 114:
                    sb.append("r");
                    break;
                case 115:
                    sb.append("s");
                    break;
                case 116:
                    sb.append("t");
                    break;
                case 117:
                    sb.append("u");
                    break;
                case 118:
                    sb.append("v");
                    break;
                case 119:
                    sb.append("w");
                    break;
                case 120:
                    sb.append("x");
                    break;
                case 121:
                    sb.append("y");
                    break;
                case 122:
                    sb.append("z");
                    break;
                case 123:
                    sb.append("{");
                    break;
                case 124:
                    sb.append("|");
                    break;
                case 125:
                    sb.append("}");
                    break;
                case 126:
                    sb.append("~");
                    break;
                case 127:
                    sb.append("⌂");
                    break;
                case 128:
                    sb.append("Ç");
                    break;
                case 129:
                    sb.append("ü");
                    break;
                case 130:
                    sb.append("é");
                    break;
                case 131:
                    sb.append("â");
                    break;
                case 132:
                    sb.append("ä");
                    break;
                case 133:
                    sb.append("à");
                    break;
                case 134:
                    sb.append("å");
                    break;
                case 135:
                    sb.append("ç");
                    break;
                case 136:
                    sb.append("ê");
                    break;
                case 137:
                    sb.append("ë");
                    break;
                case 138:
                    sb.append("è");
                    break;
                case 139:
                    sb.append("ï");
                    break;
                case 140:
                    sb.append("î");
                    break;
                case 141:
                    sb.append("ì");
                    break;
                case 142:
                    sb.append("Ä");
                    break;
                case 143:
                    sb.append("Å");
                    break;
                case 144:
                    sb.append("É");
                    break;
                case 145:
                    sb.append("æ");
                    break;
                case 146:
                    sb.append("Æ");
                    break;
                case 147:
                    sb.append("ô");
                    break;
                case 148:
                    sb.append("ö");
                    break;
                case 149:
                    sb.append("ò");
                    break;
                case 150:
                    sb.append("û");
                    break;
                case 151:
                    sb.append("ù");
                    break;
                case 152:
                    sb.append("ÿ");
                    break;
                case 153:
                    sb.append("Ö");
                    break;
                case 154:
                    sb.append("Ü");
                    break;
                case 155:
                    sb.append("¢");
                    break;
                case 156:
                    sb.append("£");
                    break;
                case 157:
                    sb.append("¥");
                    break;
                case 158:
                    sb.append("₧");
                    break;
                case 159:
                    sb.append("ƒ");
                    break;
                case 160:
                    sb.append("á");
                    break;
                case 161:
                    sb.append("í");
                    break;
                case 162:
                    sb.append("ó");
                    break;
                case 163:
                    sb.append("ú");
                    break;
                case 164:
                    sb.append("ñ");
                    break;
                case 165:
                    sb.append("Ñ");
                    break;

                default:
                    sb.append("?");
                    System.err.println("err : le caractère '" + intArray[i] + "' n est pas répertorié");
            }
        }
        String response = new String(sb);
        return response;
    }

it must surely exist a simpler way but it is the only one that I managed to make work ... I pose this in case someone would like to do something similar...

Still looking for why the characters received was not correct, I did test the invoke command on windows 10 powershell directly, it turns out that the problem appears too, ... I do not know if I am the only one ??

Invoke-Command -ComputerName 192.168.1.51 -FilePath C:\\SCRIPT\\Kill-ProcessFirefox.ps1
Op‚ration r‚ussieÿ: le processus de PID 18524 (processus enfant de PID 22116) a ‚t‚
arrˆt‚.

even specifying in the console chcp 65001 or [Console] :: OutputEncoding = [Text.UTF8Encoding] :: UTF8 before the invoke command ...