如何将数据磁盘连接到Windows Server Azure VM并直接在模板中格式化?

如何将数据磁盘连接到Windows Server Azure VM并直接在模板中格式化?,azure,azure-resource-manager,azure-vm-templates,Azure,Azure Resource Manager,Azure Vm Templates,我需要将数据磁盘连接到虚拟机(在虚拟机中),并喜欢立即格式化和使用磁盘,而不需要进一步的手动干预。 如何在ARM模板中直接实现这一点?您可以在模板中使用指向脚本的对象 { "type": "Microsoft.Compute/virtualMachineScaleSets/extensions", "name": "[concat(variables('VmssName'),'/', variables('extensionName'))]", "apiVersion": "2015-05-01

我需要将数据磁盘连接到虚拟机(在虚拟机中),并喜欢立即格式化和使用磁盘,而不需要进一步的手动干预。 如何在ARM模板中直接实现这一点?

您可以在模板中使用指向脚本的对象

{
"type": "Microsoft.Compute/virtualMachineScaleSets/extensions",
"name": "[concat(variables('VmssName'),'/', variables('extensionName'))]",
"apiVersion": "2015-05-01-preview",
"location": "[resourceGroup().location]",
"dependsOn": [
    "[concat('Microsoft.Compute/virtualMachineScaleSets/', variables('VmssName'))]"
],
"properties": {
    "publisher": "Microsoft.Azure.Extensions",
    "type": "CustomScript",
    "typeHandlerVersion": "2.0",
    "autoUpgradeMinorVersion": true,
    "settings": {
        "fileUris": [
            "[parameters('BootScriptUri')]"
        ]
    },
    "protectedSettings": {
        "commandToExecute": "[parameters('commandToExecute')]"
    }
}
然后像这样的脚本

Get-Disk | 
    Where partitionstyle -eq 'raw' |
    Initialize-Disk -PartitionStyle MBR -PassThru |
    New-Partition -DriveLetter "F" -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "DataDisk" -Confirm:$false

有一个linux版本的脚本-有更多的设施!在

我向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脚本中填充,该脚本上载自定义扩展脚本文件并调用
新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
在vms
extensionProfile
中,我添加了自定义脚本扩展(将其与其他扩展放在一个位置):

然后最终创建了脚本。我最初的问题是,我在C:上没有足够的空间来存放所有docker映像,因此我将
docker
移动到新驱动器

# create and format disk

Get-Disk |
    Where PartitionStyle -eq 'Raw' |
    Select-Object -First 1 |
    Initialize-Disk -PartitionStyle MBR -PassThru |
    New-Partition -DriveLetter F -UseMaximumSize |
    Format-Volume -FileSystem NTFS -NewFileSystemLabel "Containers" -Confirm:$false

# move docker to F:\docker

docker images -a -q | %{docker rmi $_ --force}

Stop-Service Docker
$service = (Get-Service Docker)
$service.WaitForStatus("Stopped","00:00:30")

@{"data-root"="F:\docker"} | ConvertTo-Json | Set-Content    C:\programdata\docker\config\daemon.json
Get-Process docker* | % {Stop-Process -Id $_.Id -Force}
docker system info

Copy-Item C:\programdata\docker F:\docker -Recurse

Start-Service Docker

谢谢@michael-b,这为我指明了正确的方向;我将在下面添加与Windows Server VMS对齐的版本
# create and format disk

Get-Disk |
    Where PartitionStyle -eq 'Raw' |
    Select-Object -First 1 |
    Initialize-Disk -PartitionStyle MBR -PassThru |
    New-Partition -DriveLetter F -UseMaximumSize |
    Format-Volume -FileSystem NTFS -NewFileSystemLabel "Containers" -Confirm:$false

# move docker to F:\docker

docker images -a -q | %{docker rmi $_ --force}

Stop-Service Docker
$service = (Get-Service Docker)
$service.WaitForStatus("Stopped","00:00:30")

@{"data-root"="F:\docker"} | ConvertTo-Json | Set-Content    C:\programdata\docker\config\daemon.json
Get-Process docker* | % {Stop-Process -Id $_.Id -Force}
docker system info

Copy-Item C:\programdata\docker F:\docker -Recurse

Start-Service Docker