Create a VM with AZ Shell configuring all details

Cajunspirit 1 Reputation point
2020-11-01T18:43:32.433+00:00

Pleasant day all,

My Azure free trial has expired and I am trying to create a full shell script for creating a new VM with all settings. I am requesting help in correcting my script.

It appears to do everything with AZ Shell, you need to create the Virtual Network and Subnets first. Is this correct?

Is there a way to assign RAM size in the VM Creation script?

Create Windows 2016 VM

az vm create 
--resource-group test-rg-1 
--name test-win2016-ap 

--admin-password Password1 
--admin-username Admin1

--os-disk-name Win2k16Test
--os-disk-size-gb 72

--image WindowsServer 2016 
--size Standard_DS2_v2 

-Location "East US" `
-VirtualNetworkName "vnet-01" `
-SubnetName "vnet-subnet-01" `
-SecurityGroupName "test-win16-nsg" `
-OpenPorts 80,3389,443

Create LinuxVM

# Create a virtual network card and associate with public IP address and NSG 
    $nic = New-AzNetworkInterface ` 
    -Name "testRedhatNic1" ` 
    -ResourceGroupName "test-rg1" ` 
    -Location "EastUS" ` -SubnetId $vnet.Subnets[0].Id ` 
    -PublicIpAddressId $pip.Id ` 
    -NetworkSecurityGroupId $nsg.Id 

    # Define a credential object 
    $securePassword = ConvertTo-SecureString ' ' -AsPlainText -Force 
    $cred = New-Object System.Management.Automation.PSCredential ("Admin1", $securePassword) 

    # Create a virtual machine configuration 
    $vmConfig = New-AzVMConfig ` 

    -VMName testRedhat
    -VMSize "Standard_D1" 

    --os-disk-name RedHat7.4Test
    --os-disk-size-gb 32

    Set-AzVMOperatingSystem 
    -Linux 
    -ComputerName "TestRedhat1" 
    -Credential $cred 
    -DisablePasswordAuthentication

    Set-AzVMSourceImage 
    -PublisherName "Canonical" 
    -Offer "RedHat" 
    -Skus "7.4-LVM" 
    -Version "latest" 

    Add-AzVMNetworkInterface
    -Id $nic.Id 

-Location "East US" `
    -VirtualNetworkName "vnet-01"
    -SubnetName "vnet-subnet-02"
    -SecurityGroupName "test-RedHat-nsg"
    -OpenPorts 80,3389,443

    # Configure the SSH key 
    $sshPublicKey = cat ~/.ssh/id_rsa.pub Add-AzVMSshPublicKey 
    -VM $vmconfig ` 
    -KeyData $sshPublicKey ` 
    -Path "/home/azureuser/.ssh/authorized_keys" 
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,816 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Andreas Baumgarten 108.2K Reputation points MVP
    2020-11-01T19:08:18.353+00:00

    you need to create the Virtual Network and Subnets first. Is this correct?

    That's right. First you have to create the virtual network and the subnets.
    https://learn.microsoft.com/en-us/azure/virtual-network/quick-create-cli

    a way to assign RAM size in the VM Creation script?

    The amount of RAM is related to the VM SKU -> Standard_DS2_v2 / Standard_D1 in your script.

    More details about the SKUs you can find here:

    https://learn.microsoft.com/en-us/azure/virtual-machines/sizes

    For instance all the DS2_v2 SKUs:
    https://learn.microsoft.com/en-us/azure/virtual-machines/dv2-dsv2-series#dsv2-series

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.