通过PowerShell命令给Azure VM添加CustomScriptExtension

时间:2024-05-29 00:06:56

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

通过PowerShell命令给Azure VM添加CustomScriptExtension

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

azure vm extension-image list

通过PowerShell命令给Azure VM添加CustomScriptExtension

本文将介绍,通过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

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

通过PowerShell命令给Azure VM添加CustomScriptExtension

执行成功。

或者采用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---

脚本执行成功。