当需要通过代码的方式执行PowerShell脚本时,可以参考以下的示例。

Azure SDK中提供了两个方法来执行PowerShell脚本 (SDK Source Code: https://github.com/Azure/azure-libraries-for-java/blob/master/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java#L400)

  • public RunCommandResult runPowerShellScript(String groupName, String name, List<String> scriptLines, List<RunCommandInputParameter> scriptParameters)
  • public Observable<RunCommandResult> runPowerShellScriptAsync(List<String> scriptLines, List<RunCommandInputParameter> scriptParameters)

在使用的时候,需要注意的是参数scriptLines 和 scriptParameters。 下面部分为关键代码,以Java SDK的同步方法runPowerShellScript为例

        Azure azure = null;

        azure = Azure.authenticate(credentials).withSubscription("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

        // 获取虚拟机对象
VirtualMachine testvm = azure.virtualMachines().getById(
"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vm resource group /providers/Microsoft.Compute/virtualMachines/vm name");
//testvm.start(); // 准备PowerShell脚本
List<String> scriptLines = new ArrayList<String>();
scriptLines.add(" param([string]$arg1, [string]$arg2 )");
scriptLines.add(" Write-Host This is a sample script with parameters $arg1 and $arg2");
scriptLines.add(" Get-Host | Select-Object Version"); //设置参数arg1 和 arg2
List<RunCommandInputParameter> scriptParameters = new ArrayList<RunCommandInputParameter>();
RunCommandInputParameter arg1 = new RunCommandInputParameter(){};
arg1.withName("arg1");
arg1.withValue("test1");
RunCommandInputParameter arg2 = new RunCommandInputParameter(){};
arg2.withName("arg2");
arg2.withValue("test2"); scriptParameters.add(arg1);
scriptParameters.add(arg2); //执行 PowerShell
RunCommandResult rcresult = testvm.runPowerShellScript("vm-rg", "lbpstest01", scriptLines, scriptParameters); System.out.println(rcresult.value().get(0).message());
System.out.println(rcresult.value().get(1).message());

注意:

  1. 在获取 azure对象时,需要通过AAD认证。并且当前使用的认证有权限操作所选择的虚拟机(VM)。获取认证信息部分参考博文 “使用Java代码启动Azure VM(虚拟机)
  2. 如PowerShell脚本中需要传入参数,则必须在脚本中进行声明,如:param([string]$arg1, [string]$arg2 ),然后通过scriptParameters对象传入。
  3. PowerShell执行成功的结果包含在RunCommandResult对象的Value 1中,如果所输入的PowerShell脚本有语法等操作,则在Value 2中输出详细的异常消息.

在执行PowerShell脚本时,如发现脚本有错误。在RunCommandResult中会返回PowerShell提示的错误信息:

错误的PowerShell脚本
RunCommandResult中的提示消息

示例完整代码:

package org.example;

import java.util.ArrayList;
import java.util.List; import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.credentials.AzureTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.*; /**
* s Hello world!
*
*/
public class App {
public static void main(String[] args) {
//使用AAD Application 方式获取 认证
AzureTokenCredentials credentials = new ApplicationTokenCredentials("application id",
"tenant id "securt key",
AzureEnvironment.AZURE_CHINA);
Azure azure = null; azure = Azure.authenticate(credentials).withSubscription("subscription id"); // 获取虚拟机对象
VirtualMachine testvm = azure.virtualMachines().getById("resource id");
//testvm.start(); // 准备PowerShell脚本
List<String> scriptLines = new ArrayList<String>();
scriptLines.add(" param([string]$arg1, [string]$arg2)");
scriptLines.add(" Write-Host This is a sample script with parameters $arg1 and $arg2");
scriptLines.add(" Get-Host | Select-Object Version"); //设置参数arg1 和 arg2
List<RunCommandInputParameter> scriptParameters = new ArrayList<RunCommandInputParameter>();
RunCommandInputParameter arg1 = new RunCommandInputParameter(){};
arg1.withName("arg1");
arg1.withValue("test1");
RunCommandInputParameter arg2 = new RunCommandInputParameter(){};
arg2.withName("arg2");
arg2.withValue("test2"); scriptParameters.add(arg1);
scriptParameters.add(arg2); //执行 PowerShell
RunCommandResult rcresult = testvm.runPowerShellScript("vm-rg", "lbpstest01", scriptLines, scriptParameters); System.out.println(rcresult.value().get(0).message());
System.out.println(rcresult.value().get(1).message()); System.out.println("Hello World!"); }
}

在POM.XML中引用的SDK Version:

    <dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.37.1</version>
</dependency>

执行结果的正确输出:

参考资料

使用Java代码启动Azure VM(虚拟机):https://www.cnblogs.com/lulight/p/14295089.html

Run PowerShell scripts in your Windows VM by using Run Commandhttps://docs.microsoft.com/en-us/azure/virtual-machines/windows/run-command#azure-cli

azure-libraries-for-java VirtualMachineImpl.java : https://github.com/Azure/azure-libraries-for-java/blob/master/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineImpl.java#L400

最新文章

  1. O365(世纪互联)SharePoint 之站点个性化
  2. 配置java环境 ,安装Android Studio...(ps:用eclipse也不错,但as是趋势 自己凭爱好选择)
  3. 错误 java.lang.ClassCastException: com.ylpw.sms.YZZYSenderUtil cannot be cast to ResourceBundle
  4. Canvas基础
  5. select查询时,如何把指定的行放置在最前面
  6. GJM :Unity 使用SqlServer数据库 [原创]
  7. 【LintCode】判断一个字符串是否包含另一个字符串的所有字符
  8. java 获取classpath下文件多种方式
  9. js判断用户是否禁用了cookie
  10. python 邮件发送 脚本
  11. PhoneGap API Documentation API Reference
  12. Souerce 之 图片格式
  13. Java之DataInputStream和DataOutputStream-用流操作基本数据类型
  14. Java序列化技术
  15. java基础(十一章)
  16. Xcode无法安装基于ruby的插件问题的解决
  17. jsp面试题
  18. Storm入门(一)原理介绍
  19. vi检索
  20. Java 数据库程序设计

热门文章

  1. react虚拟dom
  2. mybatisPlus中的模糊查询问题
  3. Vue学习笔记-Vue.js-2.X 学习(一)===&gt;基本知识学习
  4. Java流程控制:顺序结构
  5. 迭代器 (Iterator) 和 生成器 (Generator)
  6. 学习java之基础语法(一)
  7. .NET Core中的Worker Service
  8. 基于ABP框架的SignalR,使用Winform程序进行功能测试
  9. bjd_ctf
  10. python-实现链式栈