首页 > 解决方案 > 如何获取 RunCommandResult 以确定是否有错误?

问题描述

当远程执行具有以下源代码的 Azure Liunux 虚拟机的 shell 脚本并且退出代码不是时,是否可能引发异常0



public static void main(String[] args) {

        String subscriptionId="";
        AppServiceMSICredentials appServiceMsiCredentials = new 
        AppServiceMSICredentials(AzureEnvironment.AZURE);
        Azure azure = Azure
                .configure()
                .withLogLevel(LogLevel.NONE)
                .authenticate(appServiceMsiCredentials)
                .withSubscription(subscriptionId);
        final String rgName1 = "testlinux_group";
        final String linuxVMName = "testlinux";
        try {

            VirtualMachine virtualMachine = azure.virtualMachines().getByResourceGroup(rgName1, linuxVMName);

            System.out.println("Running Command");
            List<String> commands = new ArrayList<>();

            commands.add("echo 1");

            RunCommandInput runParams = new RunCommandInput()
                    .withCommandId("RunShellScript")
                    .withScript(commands);

            RunCommandResult runResult = azure.virtualMachines().runCommand(virtualMachine.resourceGroupName(), virtualMachine.name(), runParams);

            for (InstanceViewStatus res : runResult.value()) {
                context.getLogger().info("code : " + res.code());
                context.getLogger().info("status : " + res.displayStatus());
                context.getLogger().info("message : " + res.message());
            }


        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            System.out.println("final");
        }

标签: azureazure-sdk

解决方案


推荐阅读