0

我最近接到了通过 SOAP UI 连接到 dynamo DB 的任务,以断言我的公司 API 正在创建正确的内容。在尝试访问 Dynamo DB 数据时遇到了许多困难后,我们决定使用 windows powershell 通过 groovy 脚本进行连接,以利用 AWS CLI 及其由 Amazon 提供的授权。

我已经完成并配置了 AWS CLI,并且能够通过 power shell 按预期运行命令,例如:

    $ aws dynamodb scan --table-name Accounts
    $ aws dynamodb get-item --table-name Accounts --key '{\"id\":{\"S\":\"3b7e2c4a-f672-4a12-b2bc-7313b60b0a9a\"}}'

在 SOAP UI 中切换到 groovy 脚本时,第一个查询按预期工作,但第二个问题抛出错误:“解析参数时出错:--key”:无效的 JSON:期望的属性名称用双引号括起来:第 1 行第 2 列(字符 1)"

这是我用来运行 powershell 命令的各种互联网资源拼凑而成的代码片段:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    //Get details and Build the command
    String powerShellCommand = context.expand( '${#TestCase#PowerShellCommand}' )
    String command = "powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile  -Command \"${powerShellCommand}\""

    // Executing the command
    Process powerShellProcess = Runtime.getRuntime().exec(command);

    // Getting the results
    powerShellProcess.getOutputStream().close();
    String line;
    //-----------------------------------------------
    //Run query
            BufferedReader stdout = new BufferedReader(new InputStreamReader(
                powerShellProcess.getInputStream()));
            String jsonString = "";
                while ((line = stdout.readLine()) != null) {
                        jsonString = jsonString.concat(line);
                }
            stdout.close();
    //-----------------------------------------------
    //Error stuff:
            BufferedReader stderr = new BufferedReader(new InputStreamReader(
                    powerShellProcess.getErrorStream()));
                    while ((line = stderr.readLine()) != null) {
                            log.info(line);
                    }
            stderr.close();
    //-----------------------------------------------
    log.info(jsonString);

我相信这可能与引号和 exscapes 的放置有关,但还没有找到正确的组合,我相信这是我当前对 powershell 的输入:

    powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile  -Command "aws dynamodb get-item --table-name Platform.Accounts --key '{\"id\":{\"S\":\"3b7e2c4a-f672-4a12-b2bc-7313b60b0a9a\"}}'"

我什至尝试了上面代码的修改版本,我将只在 AWS 命令​​中发布,但这只会返回一个不同的错误:解析参数错误:预期:'=',接收:''' 输入:

这让我感到难过的原因是,当直接进入 windows powershell 时,这个命令按预期工作。

4

1 回答 1

0

您可能必须使用反引号 ` 转义反斜杠\和/或双引号"

或者只是用\反引号 ` char 替换 char 因为在 powershell 转义 char 是反引号而不是反斜杠...

于 2018-05-22T10:45:22.567 回答