设为首页 收藏本站
查看: 1376|回复: 0

[经验分享] 通过PowerShell命令给Azure VM添加CustomScriptExtension

[复制链接]

尚未签到

发表于 2017-6-30 13:10:24 | 显示全部楼层 |阅读模式
  Azure的VM提供了一种管理工具叫Azure VM Extension。它实现了一些管理虚拟机所需要的重要功能,比如:重设密码、设置RDP参数、以及许多其他关键的功能,并且Azure VM一直在添加新的Extension。默认情况下,创建的Windows VM会安装以下几个基本的VM Extension,包括IaaSDiagnostics,VMAccess和BGInfo。Linux的VM,如果开启Diagnostics功能,默认会安装LinuxDiagnostic的Extension。
DSC0000.png

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



azure vm extension-image list
DSC0001.png

  本文将介绍,通过PowerShell命令,给Azure的Linux VM添加CustomerScriptForLinux的Extension。
  1 准备Azure VM
  Azure VM在添加Extension时,必须是运行状态,同时,其Windows Azure Agent必须已经安装。可以通过如下命令查看是否有WAagent程序在运行:



[iyunv@hwiscsi02 waagent]# ps -ef | grep "waagent -daemon" | grep -v grep
root 935 1 0 08:29 ? 00:00:01 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
  执行完后,屏幕输出如下:
DSC0002.png

  执行成功。
  或者采用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查看相关的日志。



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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-389662-1-1.html 上篇帖子: Writing DynamicTableEntity to Azure Storage Table 下篇帖子: 使用SAS和JavaScript前端上传Azure Bolb大文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表