通过PowerShell命令给Azure VM添加CustomScriptExtension
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的更多相关文章
- Azure AD Domain Service(一)将 Azure VM 实例添加到域服务里
一,引言 有网友提到实际项目中如何将 Azuer VM 实例加入到 Azure AD 域,那我们今天就带着整个问题开始今天的分析!首先我们得了解什么是 Azure AD 域服务,Azure AD 域服 ...
- CentOS 7.2 PowerShell下安装Azure Module
目前Linux版本的PowerShell还是Alpha版本,所以很多功能不能使用. 比如通过Powershell命令:install-module AzureRM在线安装Azure的Module.但我 ...
- 用于创建和管理 Azure 虚拟机的常用 PowerShell 命令
本文介绍一些可用于在 Azure 订阅中创建和管理虚拟机的 Azure PowerShell 命令. 若要获取特定命令行开关和选项的详细帮助,可以使用 Get-Help 命令. 有关安装最新版 Azu ...
- Azure PowerShell (9) 使用PowerShell导出订阅下所有的Azure VM的Public IP和Private IP
<Windows Azure Platform 系列文章目录> 笔者在之前的工作中,有客户提出想一次性查看Azure订阅下的所有Azure VM的Public IP和Private IP. ...
- Azure PowerShell (12) 通过Azure PowerShell创建SSH登录的Linux VM
<Windows Azure Platform 系列文章目录> 本章将介绍如何使用Azure PowerShell,创建SSH登录的Linux VM 前提要求: 1.安装Azure Pow ...
- 【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例
当需要通过代码的方式执行PowerShell脚本时,可以参考以下的示例. Azure SDK中提供了两个方法来执行PowerShell脚本 (SDK Source Code: https://gith ...
- Azure按订阅,资源组,资源类型导出所有资源powershell命令
一般可以借助powershell命令来读取资源:例如:1, 读取某个订阅下的资源:$subscriptionID = "xxxxxxxx"Set-AzureRmContext $s ...
- 在 Azure VM 中使用应用商店映像创建 HPC Pack 群集的头节点
使用 Azure 应用商店和 Azure 门户中的 Microsoft HPC Pack 2012 R2 虚拟机映像创建 HPC 群集的头节点. 此 HPC Pack VM 映像基于预安装了 HPC ...
- 通过Python查看Azure VM的状态
Azure的管理平台采用Restful API的方式实现管理.比如获取VM的管理API的各种操作的文档请参考: https://docs.microsoft.com/en-us/rest/api/co ...
随机推荐
- java内置注解、元注解和自定义注解
注解的作用: 1.生成文档 2.跟踪代码依赖性 3.编译时进行格式检查 ---------------------------------------------------------------- ...
- PHP的异常处理、错误的抛出及错误回调函数
一.错误.异常和等级常量表 error:不能再编译期发现运行期的错误,不如试图echo输出一个未赋值的变量,这类问题往往导致程序或逻辑无法继续下去而需要中断. exception:程序执行过程中出现意 ...
- Vue.js学习笔记 第四篇 列表渲染
遍历数组和对象 和条件选择一样,循环也和其他语言类似,也尝试着用一个例子解决问题 <!DOCTYPE html> <html> <head> <meta ch ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- Linux下Python科学计算包numpy和SciPy的安装
系统环境: OS:RedHat5 Python版本:Python2.7.3 gcc版本:4.1.2 各个安装包版本: scipy-0.11.0 numpy-1.6.2 nose-1.2.1 lap ...
- 【P2107】小Z的AK计划(优先队列+贪心)
水一发优先队列的水题.. 这个题貌似以前有做过类似的.具体的方法是用大根堆辅助贪心算法得出正解.可以看出来,如果小Z走到了某个地方,那么他最远一定是到了这里,不可能有再走回来这种操作,因为很明显那样不 ...
- Spark基本概念快速入门
Spark集群 一组计算机的集合,每个计算机节点作为独立的计算资源,又可以虚拟出多个具备计算能力的虚拟机,这些虚拟机是集群中的计算单元.Spark的核心模块专注于调度和管理虚拟机之上分布式计算任务 ...
- Http协议与生命周期
一.Http知识: 1.基于socket 浏览器(格式一) web服务器(格式一) MYSQL客户端(格式二) MYSQL服务端(格式三) ...
- Oracle创建表空间和增加表空间
1.创建表空间 create tablespace fgq datafile 'E:\app\Administrator\oradata\fms\fgq01.dbf' size 1000M autoe ...
- character_set_connection、character_set_results、 character_set_client的作用
如题.通常的使用中,character_set_client,character_set_connection这两个变量的值是一样的,也就是说查询不需要进行编码转换.这样看来变量character_s ...