向服务结构/vms arm模板添加了3个参数:
...
"scriptLocation": {
"type": "string",
"metadata": {
"description": "Location of custom extension scripts on storage account container"
}
},
"scriptStorageAccount": {
"type": "string",
"metadata": {
"description": "Name of custom extension scripts storage account"
}
},
"scriptStorageAccountKey": {
"type": "string",
"metadata": {
"description": "Key to custom extension scripts storage account"
}
},
...
这些参数填充在powershell脚本中,上载自定义扩展脚本文件并调用
New-AzureRmResourceGroupDeployment
是的。
...
$StorageAccountName = "mydeploymentstorage"
$StorageContainerName = "ext"
$ArtifactStagingDirectory = ".\ExtensionScripts"
...
# transfer Extension script to Storage $StorageAccount = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -eq $StorageAccountName})
$StorageAccountContext = $StorageAccount.Context
New-AzureStorageContainer -Name $StorageContainerName -Context $StorageAccountContext -Permission Container -ErrorAction SilentlyContinue *>&1
$ArtifactFilePaths = Get-ChildItem $ArtifactStagingDirectory -Recurse -File | ForEach-Object -Process {$_.FullName}
foreach ($SourcePath in $ArtifactFilePaths) {
Write-Host "transfering" $SourcePath
$BlobName = $SourcePath.Substring($SourcePath.LastIndexOf("\")+1)
Set-AzureStorageBlobContent -File $SourcePath -Blob $BlobName -Container $StorageContainerName -Context $StorageAccountContext -Force -ErrorAction Stop
}
# prepare and pass script parameters
$DynamicParameters = New-Object -TypeName Hashtable
$DynamicParameters["scriptLocation"] = $StorageAccountContext.BlobEndPoint + $StorageContainerName
$DynamicParameters["scriptStorageAccount"] = $StorageAccountName
$DynamicParameters["scriptStorageAccountKey"] = ($StorageAccount | Get-AzureRmStorageAccountKey).Value[0]
...
# start deployment
New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) ` `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile `
-TemplateParameterFile $TemplateParametersFile `
@DynamicParameters `
-Verbose
增加了磁盘大小
storageProfile
以下内容:
...
"storageProfile": {
"imageReference": {
"publisher": "[parameters('vmImagePublisher')]",
"offer": "[parameters('vmImageOffer')]",
"sku": "[parameters('vmImageSku')]",
"version": "[parameters('vmImageVersion')]"
},
"osDisk": {
"caching": "ReadWrite",
"createOption": "FromImage",
"diskSizeGB": 128,
"managedDisk": {
"storageAccountType": "[parameters('storageAccountType')]"
}
}
}
...
将自定义脚本扩展添加到vms
extensionProfile
以下内容:
...
"virtualMachineProfile": {
"extensionProfile": {
"extensions": [
...
{
"name": "[concat(parameters('vmNodeType0Name'),'_SetupNode')]",
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.9",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"[concat(parameters('scriptLocation'),'/setupNode.ps1')]"
]
},
"protectedSettings": {
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File CreateDisk.ps1",
"storageAccountName": "[parameters('scriptStorageAccount')]",
"storageAccountKey": "[parameters('scriptStorageAccountKey')]"
}
}
}
]
...
创建了脚本
setupNode.PS1
调整操作系统磁盘分区的大小
# resize Partition
$size = (Get-PartitionSupportedSize -DiskNumber 0 -PartitionNumber 1)
Resize-Partition -DiskNumber 0 -PartitionNumber 1 -Size $size.SizeMax