Azure的VM提供了一种管理工具叫Azure VM Extension。它实现了一些管理虚拟机所需要的重要功能,比如:重设密码、设置RDP参数、以及许多其他关键的功能,并且Azure VM一直在添加新的Extension。默认情况下,创建的Windows VM会安装以下几个基本的VM Extension,包括IaaSDiagnostics,VMAccess和BGInfo。Linux的VM,如果开启Diagnostics功能,默认会安装LinuxDiagnostic的Extension。

通过Azure CLI可以获得Azure在某个区域提供的的Extension和相关信息。可以通过如下命令:

azure vm extension-image list

本文将介绍,通过PowerShell命令,给Azure的Linux VM添加CustomerScriptForLinux的Extension。

1 准备Azure VM

Azure VM在添加Extension时,必须是运行状态,同时,其Windows Azure Agent必须已经安装。可以通过如下命令查看是否有WAagent程序在运行:

[root@hwiscsi02 waagent]# ps -ef | grep "waagent -daemon" | grep -v grep

root    : ? :: python /usr/sbin/waagent -daemon

2 准备PowerShell环境

//登陆Azure China
$mycred = Get-Credential -UserName admin@weiheng.partner.onmschina.cn -Message hello
Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $mycred //定义Resource Group、VM和Location变量
$RGName = 'hwiscsi'
$VmName = 'hwiscsi02'
$Location = 'China East' //定义Extension相关信息
$ExtensionName = 'CustomScriptForLinux'
$Publisher = 'Microsoft.OSTCExtensions'
$version = '1.5' $PublicConf = '{"fileUris":["https://hwarm.blob.core.chinacloudapi.cn/soft/extension.sh"], "commandToExecute": "sh extension.sh" }' 
$PrivateConf = '{
  "storageAccountName": "hwarm",
  "storageAccountKey": "xxxxxxxx=="
}' //执行Set-AzureRmVMExtension命令,安装extension:
Set-AzureRmVMExtension -ResourceGroupName $RGName -VMName $VmName -Location $Location `
  -Name $ExtensionName -Publisher $Publisher `
  -ExtensionType $ExtensionName -TypeHandlerVersion $Version `
  -Settingstring $PublicConf -ProtectedSettingString $PrivateConf

执行完后,屏幕输出如下:

执行成功。

或者采用Azure CLI命令:

azure vm extension set hwiscsi hwiscsi02 CustomScriptForLinux Microsoft.OSTCExtensions  1.5 -i '{"fileUris":["https://hwarm.blob.core.chinacloudapi.cn/soft/extension.sh"], "commandToExecute": "sh extension.sh"}'

info:    Executing command vm extension set
+ Looking up the VM "hwiscsi02
+ Installing extension "CustomScriptForLinux", VM: "hwiscsi"
info: vm extension set command OK

3 检查VM内的信息

在VM中,可以在/var/log/azure/Microsoft.OSTCExtensions.CustomScriptForLinux/1.5.2.0查看相关的日志。

// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]sequence number is
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]setting file path is/var/lib/waagent/Microsoft.OSTCExtensions.CustomScriptForLinux-1.5.2.0/config/.settings
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]JSON config: {
// :: "runtimeSettings": [
// :: {
// :: "handlerSettings": {
// :: "publicSettings": {"fileUris":["https://hwarm.blob.core.chinacloudapi.cn/soft/extension.sh"],"commandToExecute":"sh extension.sh"}
// :: }
// :: }
// :: ]
// :: }
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Install,success,,Install succeeded
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]sequence number is
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]setting file path is/var/lib/waagent/Microsoft.OSTCExtensions.CustomScriptForLinux-1.5.2.0/config/.settings
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]JSON config: {
// :: "runtimeSettings": [
// :: {
// :: "handlerSettings": {
// :: "publicSettings": {"fileUris":["https://hwarm.blob.core.chinacloudapi.cn/soft/extension.sh"],"commandToExecute":"sh extension.sh"}
// :: }
// :: }
// :: ]
// :: }
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]set most recent sequence number to
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Command to execute:sh extension.sh
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Enable,transitioning,,Launching the script...
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]sequence number is
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]setting file path is/var/lib/waagent/Microsoft.OSTCExtensions.CustomScriptForLinux-1.5.2.0/config/.settings
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]JSON config: {
// :: "runtimeSettings": [
// :: {
// :: "handlerSettings": {
// :: "publicSettings": {"fileUris":["https://hwarm.blob.core.chinacloudapi.cn/soft/extension.sh"],"commandToExecute":"sh extension.sh"}
// :: }
// :: }
// :: ]
// :: }
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Will try to download files, number of retries = , wait SECONDS between retrievals = 20s
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Command to execute:sh extension.sh
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Downloading,transitioning,,Downloading files...
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]No azure storage account and key specified in protected settings. Downloading scripts from external links...
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Converting /var/lib/waagent/Microsoft.OSTCExtensions.CustomScriptForLinux-1.5.2.0/download//extension.sh from DOS to Unix formats: Done
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Removing BOM of /var/lib/waagent/Microsoft.OSTCExtensions.CustomScriptForLinux-1.5.2.0/download//extension.sh: Done
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Succeeded to download files, retry count =
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Internal DNS is ready, retry count =
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Command to execute:sh extension.sh
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Command is finished.
// :: ---stdout---
// ::
// :: ---errout---
// ::
// ::
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Daemon,success,,Command is finished.
// :: ---stdout---

脚本执行成功。

通过PowerShell命令给Azure VM添加CustomScriptExtension的更多相关文章

  1. Azure AD Domain Service(一)将 Azure VM 实例添加到域服务里

    一,引言 有网友提到实际项目中如何将 Azuer VM 实例加入到 Azure AD 域,那我们今天就带着整个问题开始今天的分析!首先我们得了解什么是 Azure AD 域服务,Azure AD 域服 ...

  2. CentOS 7.2 PowerShell下安装Azure Module

    目前Linux版本的PowerShell还是Alpha版本,所以很多功能不能使用. 比如通过Powershell命令:install-module AzureRM在线安装Azure的Module.但我 ...

  3. 用于创建和管理 Azure 虚拟机的常用 PowerShell 命令

    本文介绍一些可用于在 Azure 订阅中创建和管理虚拟机的 Azure PowerShell 命令. 若要获取特定命令行开关和选项的详细帮助,可以使用 Get-Help 命令. 有关安装最新版 Azu ...

  4. Azure PowerShell (9) 使用PowerShell导出订阅下所有的Azure VM的Public IP和Private IP

    <Windows Azure Platform 系列文章目录> 笔者在之前的工作中,有客户提出想一次性查看Azure订阅下的所有Azure VM的Public IP和Private IP. ...

  5. Azure PowerShell (12) 通过Azure PowerShell创建SSH登录的Linux VM

    <Windows Azure Platform 系列文章目录> 本章将介绍如何使用Azure PowerShell,创建SSH登录的Linux VM 前提要求: 1.安装Azure Pow ...

  6. 【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例

    当需要通过代码的方式执行PowerShell脚本时,可以参考以下的示例. Azure SDK中提供了两个方法来执行PowerShell脚本 (SDK Source Code: https://gith ...

  7. Azure按订阅,资源组,资源类型导出所有资源powershell命令

    一般可以借助powershell命令来读取资源:例如:1, 读取某个订阅下的资源:$subscriptionID = "xxxxxxxx"Set-AzureRmContext $s ...

  8. 在 Azure VM 中使用应用商店映像创建 HPC Pack 群集的头节点

    使用 Azure 应用商店和 Azure 门户中的 Microsoft HPC Pack 2012 R2 虚拟机映像创建 HPC 群集的头节点. 此 HPC Pack VM 映像基于预安装了 HPC ...

  9. 通过Python查看Azure VM的状态

    Azure的管理平台采用Restful API的方式实现管理.比如获取VM的管理API的各种操作的文档请参考: https://docs.microsoft.com/en-us/rest/api/co ...

随机推荐

  1. jQuery自动轮播图片焦点图

    在线演示 本地下载

  2. get_class_methods--返回由类的方法名组成的数组

    get_class_methods--返回由类的方法名组成的数组 array get_class_methods ( mixed $class_name ) 返回由类的方法名组成的数组. <?p ...

  3. jar 冲突解决方案

    val urlOfClass = classOf[Nothing].getClassLoader.getResource("org/slf4j/spi/LocationAwareLogger ...

  4. python之算法LOB三人组

    一.冒泡排序 a.冒泡排序----优化 如果冒泡排序中执行一趟而没有交换,则列表已经是有序状态,可以直接结算法 import random from timewrap import * @cal_ti ...

  5. openstack-ansible Chapter 4. Deployment configuration

    Initial environment configuration Copy the contents of the /opt/openstack-ansible/etc/openstack_depl ...

  6. tomcat配置 启动

     <Context docBase="E:\apache-tomcat-7.0.50\wtpwebapps\mycms" path="" reloadab ...

  7. weinre远程调试

    一: 关于weinre weinre是一款依赖于nodejs的远程调试工具,现阶段一般用到手机app上调试非常的强大 二: weinre的安装 1)  安装 nodejs以及npm 2) 安装wein ...

  8. 20个PHP面试题及答案

    php学了那么久了,先来小试牛刀,看下这些PHP程序员面试题都会不会?初级题目1.问题:请用最简单的语言描述PHP是什么?答:PHP全称:Hypertext Preprocessor,是一种用来开发动 ...

  9. 如何关闭 window10 自带的杀毒软件

    打开运行,快捷键[win+R],输入[ gpedit.msc],点击确定 在打开的组策略窗口中,依次展开计算机配置>管理模板>Windows组件,选择[windows组件]中的window ...

  10. 51nod 1428 贪心

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活动安排问题 基准时间限制:1 秒 空间限制:13107 ...