Следующая PowerShell скрипт будет:
- Войти.
- Создать группу ресурсов.
- Создайте учетную запись хранения.
- Создание контейнера учетной записи хранения.
- Разверните локальный файл VHD на созданную учетную запись/контейнер хранения и т. Д.
- Создание виртуального сетевого устройства с общедоступным IP-адресом &.
- Создайте виртуальную машину, которая обновляет образ диска, используя сетевое устройство.
#LOGIN
Login-AzureRmAccount
# Enable verbose output and stop on error
#$VerbosePreference = 'Continue'
$ErrorActionPreference = 'Stop'
################## Upload Custom VM to Storage Account Details ##################################
Write-Host "The following is a list of available locations:"
((Get-AzureRmResourceProvider -ProviderNamespace Microsoft.Storage).ResourceTypes).Locations | Select-Object -unique
$locationName = Read-Host 'Which location do you want to create things at?'
Write-Host "The following is a list of available Subscriptions:"
Get-AzureRmSubscription | format-Table -Property SubscriptionName
$subscriptionName = Read-Host 'What is the subscription name you want?'
Write-Host "Getting the Subscription..."
Get-AzureRmSubscription –SubscriptionName $subscriptionName | Select-AzureRmSubscription
$resourceGroupName = Read-Host 'What do you want to call the Resource Group?'
New-AzureRmResourceGroup -Name $resourceGroupName -Location $locationName
$storageAccountName = Read-Host 'What do you want to call the storage account?'
Write-Host "Creating the Storage Account..."
New-AzureRmStorageAccount –StorageAccountName $storageAccountName -Location $locationName -ResourceGroupName $resourceGroupName -Type "Standard_LRS"
$storageAccount = Get-AzureRmStorageAccount -StorageAccountName $storageAccountName
$storageContainerName = Read-Host 'What do you want to call the storage container?'
Write-Host "Getting the Storage Container..."
Get-AzureRmStorageAccount | New-AzureStorageContainer -Name $storageContainerName
Write-Host "Getting the Container Endpoint URI..."
$containerURI = (Get-AzureRmStorageAccount | Get-AzureStorageContainer -Name $storageContainerName).CloudBlobContainer.StorageUri.PrimaryUri.AbsoluteUri
$localFileName = Read-Host 'What is the absolute local file path for the source .vhd file?'
$storageFileName = Read-Host 'What do you wan to name the destination file on the storage account? (NOTE: Do NOT include .vhd on the end)'
$osDiskUri = $containerUri + '/' + $storageFileName + '.vhd'
#Upload to URL specified, with container name at end of path, the local fle specified.
Add-AzureRmVhd -Destination $osDiskUri -LocalFilePath $localFileName -ResourceGroupName $resourceGroupName
################## Create VM From Storage Account Details ##################################
$vmName = Read-Host 'What do you want to name the Virtual Machine?'
$vmSuffix = Get-Random -Minimum 10000 -Maximum 99999
$vmSize = 'Standard_D3'
$nicName = 'VM{0}-NIC' -f $vmSuffix
$ipName = 'VM{0}-IP' -f $vmSuffix
$domName = 'vm-from-customimage-powershell-{0}' -f $vmSuffix
$vnetName = $vmName + '_network'
# Create the VNET
Write-Verbose 'Creating Virtual Network'
$vnetDef = New-AzureRmVirtualNetwork -ResourceGroupName $resourceGroupName -Location $locationName -Name $vnetName -AddressPrefix '10.0.0.0/16'
Write-Verbose 'Adding subnet to Virtual Network'
$vnet = $vnetDef | Add-AzureRmVirtualNetworkSubnetConfig -Name 'Subnet-1' -AddressPrefix '10.0.0.0/24' | Set-AzureRmVirtualNetwork
# Create the PIP
Write-Verbose 'Creating Public IP'
$pip = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroupName -Location $locationName -Name $ipName -DomainNameLabel $domName -AllocationMethod Dynamic
# Create the NIC
Write-Verbose 'Creating NIC'
$nic = New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName -Location $locationName -Name $nicName -PublicIpAddressId $pip.Id -SubnetId $vnet.Subnets[0].Id
# Specify the VM name and size
Write-Verbose 'Creating VM Config'
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $storageFileName -VhdUri $osDiskUri -CreateOption Attach -Linux
Write-Verbose 'Creating VM...'
$result = New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $locationName -VM $vm
if($result.Status -eq 'Succeeded') {
$result
Write-Verbose ('VM named ''{0}'' is now ready')
} else {
Write-Error 'Virtual machine was not created sucessfully.'
}