0

I have been scratching my head for ages and cannot find any information about this. So can someone please tell me how to create a VM using PowerShell so that the VM has managed disks without using ConvertTo-AzureRmVMManagedDisk and is based on a market place image like Get-AzureRmVMImage -Location $location -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2016-Datacenter" | Sort-Object Version -Descending | Select-Object -Index 0

Thanks.

John E
  • 71
  • 1
  • 2
  • 8

1 Answers1

1

Do you mean you want to use PowerShell to create an Azure VM with managed OS disk? If I understand it correctly, we can use the following PowerShell script to create it:

$location = "eastus"

New-AzureRmResourceGroup -Name jasonvm -Location $location

# Create a subnet configuration
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24

# Create a virtual network
$vnet = New-AzureRmVirtualNetwork -ResourceGroupName jasonvm -Location $location `
    -Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig

# Create a public IP address and specify a DNS name
$pip = New-AzureRmPublicIpAddress -ResourceGroupName jasonvm -Location $location `
    -AllocationMethod Static -IdleTimeoutInMinutes 4 -Name "mypublicdns$(Get-Random)"

# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP  -Protocol Tcp `
    -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
    -DestinationPortRange 3389 -Access Allow

# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleWWW  -Protocol Tcp `
    -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
    -DestinationPortRange 80 -Access Allow

# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName jasonvm -Location $location `
    -Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP,$nsgRuleWeb

# Create a virtual network card and associate with public IP address and NSG
$nic = New-AzureRmNetworkInterface -Name myNic -ResourceGroupName jasonvm -Location $location `
    -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

# Define a credential object
$cred = Get-Credential

# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName myVM -VMSize Standard_DS2_v2 | `
    Set-AzureRmVMOperatingSystem -Windows -ComputerName myVM -Credential $cred | `
    Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer `
    -Skus 2016-Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $nic.Id

# Create the virtual machine
New-AzureRmVM -ResourceGroupName jasonvm -Location $location -VM $vmConfig

Here is the result:

enter image description here

Jason Ye
  • 13,710
  • 2
  • 16
  • 25
  • Just checking in to see if the information provided was helpful. Please let me know if you would like further assistance. – Jason Ye Oct 24 '17 at 02:54
  • Hi Jason, I can't believe it was that simple; I was sure I tried all that *sigh*. One more thing though, is it possible to give the Disk a name of my choosing? – John E Oct 25 '17 at 00:14
  • @JohnE For now, that script does not support to specify the OS disk name. If you want to specify the OS disk name, please refer to this [link](https://github.com/Azure-Samples/managed-disks-powershell-getting-started/blob/master/CreateManagedDisksFromVHDInAnotherSubscription.ps1). – Jason Ye Oct 25 '17 at 06:38
  • @JohnE Does that link work for you? please let me know if you need more help:) – Jason Ye Oct 26 '17 at 01:49
  • I know this is an old post, but we are wanting to start using Managed Disks for our new VM OSs. What determines if the OS is a managed disk? Is it just the disk type? As your example for instance using "Standard_DS2_v2". –  Dec 20 '18 at 15:41