• Improve this Doc

    Show / Hide Table of Contents

    How to create a virtual machine using PowerShell

    Overview

    With UKCloud for Microsoft Azure, you can leverage the power of Microsoft Azure to create virtual machines (VMs) for your on-premises applications. As UKCloud for Microsoft Azure is built on UKCloud's assured, UK-sovereign multi-cloud platform, those applications can work alongside other cloud platforms, such as Oracle, VMware and OpenStack, and benefit from native connectivity to non-cloud workloads in Crown Hosting and government community networks, including PSN, HSCN and RLI. Before creating the virtual machine, it is necessary to create storage and networking resources for the the VM to use.

    Prerequisites

    Before you begin, ensure your PowerShell environment is set up as detailed in Configure the Azure Stack Hub user's PowerShell environment.

    Creating a virtual machine

    Declare variables

    Enter details below to provide values for the variables in the following script in this article:

    Variable name Variable description Input
    $ArmEndpoint The Azure Resource Manager endpoint for Azure Stack Hub
    $RGName Name of the resource group to be created
    $SAName Name of the storage account to be created
    $SubnetName Name of the subnet to be created
    $SubnetRange Address range of the subnet to be createdin CIDR notation
    $VNetName Name of the virtual network to be created
    $VNetRange Address range of the virtual network to be createdin CIDR notation
    $PublicIPName Name of the public IP to be created
    $NSGName Name of the network security group to be created
    $NICName Name of the network interface controller to be created
    $Username Username of the VM to be created
    $Password Password of the VM to be created
    $ComputerName Computer name of the VM to be created
    $VMName Name of the virtual machine to be created
    $VMSize Size of the virtual machine to be created (More info)
    VMType (switch) The type of virtual machine to be created (Linux or Windows)
    $VMImage The image template to deploy the virtual machine from

    Deploy the virtual machine

    From your PowerShell window:

    • Unmanaged Disk
    • Managed Disk
    # Initialise environment and variables
    
    # Declare endpoint
    $ArmEndpoint = "https://management.frn00006.azure.ukcloud.com"
    
    ## Add environment
    Add-AzEnvironment -Name "AzureStackUser" -ArmEndpoint $ArmEndpoint
    
    ## Login
    Connect-AzAccount -Environment "AzureStackUser"
    
    # Get location of Azure Stack Hub
    $Location = (Get-AzLocation).Location
    
    # Input Variables
    $RGName = "MyResourceGroup"
    $SAName = "MyStorageAccount".ToLower()
    $SubnetName = "MySubnet"
    $SubnetRange = "192.168.1.0/24"
    $VNetName = "MyVNetwork"
    $VNetRange = "192.168.0.0/16"
    $PublicIPName = "MyPublicIP"
    $NSGName = "MyNSG"
    $NICName = "MyNIC"
    $ComputerName = "MyComputer"
    $VMName = "MyVM"
    $VMSize = "Standard_DS1_v2"
    $VMImage = "*/CentOS/Skus/7.5"
    
    # Create a new resource group
    Write-Output -InputObject "Creating resource group"
    New-AzResourceGroup -Name $RGName -Location $Location
    
    ## Create storage resources
    
    # Create a new storage account
    Write-Output -InputObject "Creating storage account"
    $StorageAccount = New-AzStorageAccount -Location $Location -ResourceGroupName $RGName -Type "Standard_LRS" -Name $SAName
    
    ## Create network resources
    
    # Create a subnet configuration
    Write-Output -InputObject "Creating virtual network"
    $SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetRange
    
    # Create a virtual network
    $VirtualNetwork = New-AzVirtualNetwork -ResourceGroupName $RGName -Location $Location -Name $VNetName -AddressPrefix $VNetRange -Subnet $SubnetConfig
    
    # Create a public IP address
    Write-Output -InputObject "Creating public IP address"
    $PublicIP = New-AzPublicIpAddress -ResourceGroupName $RGName -Location $Location -AllocationMethod "Dynamic" -Name $PublicIPName
    
    # Create network security group rule (SSH or RDP)
    Write-Output -InputObject "Creating SSH/RDP network security rule"
    $SecurityGroupRule = switch ("-Linux") {
        "-Linux" { New-AzNetworkSecurityRuleConfig -Name "SSH-Rule" -Description "Allow SSH" -Access "Allow" -Protocol "TCP" -Direction "Inbound" -Priority 100 -DestinationPortRange 22 -SourceAddressPrefix "*" -SourcePortRange "*" -DestinationAddressPrefix "*" }
        "-Windows" { New-AzNetworkSecurityRuleConfig -Name "RDP-Rule" -Description "Allow RDP" -Access "Allow" -Protocol "TCP" -Direction "Inbound" -Priority 100 -DestinationPortRange 3389 -SourceAddressPrefix "*" -SourcePortRange "*" -DestinationAddressPrefix "*" }
    }
    
    # Create a network security group
    Write-Output -InputObject "Creating network security group"
    $NetworkSG = New-AzNetworkSecurityGroup -ResourceGroupName $RGName -Location $Location -Name $NSGName -SecurityRules $SecurityGroupRule
    
    # Create a virtual network card and associate it with the public IP address and NSG
    Write-Output -InputObject "Creating network interface card"
    $NetworkInterface = New-AzNetworkInterface -Name $NICName -ResourceGroupName $RGName -Location $Location -SubnetId $VirtualNetwork.Subnets[0].Id -PublicIpAddressId $PublicIP.Id -NetworkSecurityGroupId $NetworkSG.Id
    
    ## Create the virtual machine
    
    # Define a credential object to store the username and password for the virtual machine
    $Username = "MyUser"
    $Password = 'Password123!' | ConvertTo-SecureString -Force -AsPlainText
    $Credential = New-Object -TypeName PSCredential -ArgumentList ($Username, $Password)
    
    # Create the virtual machine configuration object
    $VirtualMachine = New-AzVMConfig -VMName $VMName -VMSize $VMSize
    
    # Set the VM Size and Type
    $VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Linux -ComputerName $ComputerName -Credential $Credential
    
    # Enable the provisioning of the VM Agent
    if ($VirtualMachine.OSProfile.WindowsConfiguration) {
        $VirtualMachine.OSProfile.WindowsConfiguration.ProvisionVMAgent = $true
    }
    
    # Get the VM Source Image
    $Image = Get-AzVMImagePublisher -Location $Location | Get-AzVMImageOffer | Get-AzVMImageSku | Where-Object -FilterScript { $_.Id -like $VMImage }
    
    # Set the VM Source Image
    $VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName $Image.PublisherName -Offer $Image.Offer -Skus $Image.Skus -Version "latest"
    
    # Add Network Interface Card
    $VirtualMachine = Add-AzVMNetworkInterface -Id $NetworkInterface.Id -VM $VirtualMachine
    
    # Set the OS Disk properties
    $OSDiskName = "OsDisk"
    $OSDiskUri = "{0}vhds/{1}-{2}.vhd" -f $StorageAccount.PrimaryEndpoints.Blob.ToString(), $VMName.ToLower(), $OSDiskName
    
    # Applies the OS disk properties
    $VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -CreateOption "FromImage"
    
    # Create the virtual machine.
    Write-Output -InputObject "Creating virtual machine"
    $NewVM = New-AzVM -ResourceGroupName $RGName -Location $Location -VM $VirtualMachine
    $NewVM
    Write-Output -InputObject "Virtual machine created successfully"
    
    # Initialise environment and variables
    
    # Declare endpoint
    $ArmEndpoint = "https://management.frn00006.azure.ukcloud.com"
    
    ## Add environment
    Add-AzEnvironment -Name "AzureStackUser" -ArmEndpoint $ArmEndpoint
    
    ## Login
    Connect-AzAccount -Environment "AzureStackUser"
    
    # Get location of Azure Stack Hub
    $Location = (Get-AzLocation).Location
    
    # Input Variables
    $RGName = "MyResourceGroup"
    $SAName = "MyStorageAccount".ToLower()
    $SubnetName = "MySubnet"
    $SubnetRange = "192.168.1.0/24"
    $VNetName = "MyVNetwork"
    $VNetRange = "192.168.0.0/16"
    $PublicIPName = "MyPublicIP"
    $NSGName = "MyNSG"
    $NICName = "MyNIC"
    $ComputerName = "MyComputer"
    $VMName = "MyVM"
    $VMSize = "Standard_DS1_v2"
    $VMImage = "*/CentOS/Skus/7.5"
    
    # Create a new resource group
    Write-Output -InputObject "Creating resource group"
    New-AzResourceGroup -Name $RGName -Location $Location
    
    ## Create storage resources
    
    # Create a new storage account
    Write-Output -InputObject "Creating storage account"
    $StorageAccount = New-AzStorageAccount -Location $Location -ResourceGroupName $RGName -Type "Standard_LRS" -Name $SAName
    
    ## Create network resources
    
    # Create a subnet configuration
    Write-Output -InputObject "Creating virtual network"
    $SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetRange
    
    # Create a virtual network
    $VirtualNetwork = New-AzVirtualNetwork -ResourceGroupName $RGName -Location $Location -Name $VNetName -AddressPrefix $VNetRange -Subnet $SubnetConfig
    
    # Create a public IP address
    Write-Output -InputObject "Creating public IP address"
    $PublicIP = New-AzPublicIpAddress -ResourceGroupName $RGName -Location $Location -AllocationMethod "Dynamic" -Name $PublicIPName
    
    # Create network security group rule (SSH or RDP)
    Write-Output -InputObject "Creating SSH/RDP network security rule"
    $SecurityGroupRule = switch ("-Linux") {
        "-Linux" { New-AzNetworkSecurityRuleConfig -Name "SSH-Rule" -Description "Allow SSH" -Access "Allow" -Protocol "TCP" -Direction "Inbound" -Priority 100 -DestinationPortRange 22 -SourceAddressPrefix "*" -SourcePortRange "*" -DestinationAddressPrefix "*" }
        "-Windows" { New-AzNetworkSecurityRuleConfig -Name "RDP-Rule" -Description "Allow RDP" -Access "Allow" -Protocol "TCP" -Direction "Inbound" -Priority 100 -DestinationPortRange 3389 -SourceAddressPrefix "*" -SourcePortRange "*" -DestinationAddressPrefix "*" }
    }
    
    # Create a network security group
    Write-Output -InputObject "Creating network security group"
    $NetworkSG = New-AzNetworkSecurityGroup -ResourceGroupName $RGName -Location $Location -Name $NSGName -SecurityRules $SecurityGroupRule
    
    # Create a virtual network card and associate it with the public IP address and NSG
    Write-Output -InputObject "Creating network interface card"
    $NetworkInterface = New-AzNetworkInterface -Name $NICName -ResourceGroupName $RGName -Location $Location -SubnetId $VirtualNetwork.Subnets[0].Id -PublicIpAddressId $PublicIP.Id -NetworkSecurityGroupId $NetworkSG.Id
    
    ## Create the virtual machine
    
    # Define a credential object to store the username and password for the virtual machine
    $Username = "MyUser"
    $Password = 'Password123!' | ConvertTo-SecureString -Force -AsPlainText
    $Credential = New-Object -TypeName PSCredential -ArgumentList ($Username, $Password)
    
    # Create the virtual machine configuration object
    $VirtualMachine = New-AzVMConfig -VMName $VMName -VMSize $VMSize
    
    # Set the VM Size and Type
    $VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Linux -ComputerName $ComputerName -Credential $Credential
    
    # Enable the provisioning of the VM Agent
    if ($VirtualMachine.OSProfile.WindowsConfiguration) {
        $VirtualMachine.OSProfile.WindowsConfiguration.ProvisionVMAgent = $true
    }
    
    # Get the VM Source Image
    $Image = Get-AzVMImagePublisher -Location $Location | Get-AzVMImageOffer | Get-AzVMImageSku | Where-Object -FilterScript { $_.Id -like $VMImage }
    
    # Set the VM Source Image
    $VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName $Image.PublisherName -Offer $Image.Offer -Skus $Image.Skus -Version "latest"
    
    # Add Network Interface Card
    $VirtualMachine = Add-AzVMNetworkInterface -Id $NetworkInterface.Id -VM $VirtualMachine
    
    # Applies the OS disk properties
    $VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -CreateOption "FromImage"
    
    # Enable boot diagnostics.
    $VirtualMachine = Set-AzVMBootDiagnostic -VM $VirtualMachine -Enable -StorageAccountName $SAName -ResourceGroupName $RGName
    
    # Create the virtual machine.
    Write-Output -InputObject "Creating virtual machine"
    $NewVM = New-AzVM -ResourceGroupName $RGName -Location $Location -VM $VirtualMachine
    $NewVM
    Write-Output -InputObject "Virtual machine created successfully"
    

    Feedback

    If you find a problem with this article, click Improve this Doc to make the change yourself or raise an issue in GitHub. If you have an idea for how we could improve any of our services, send an email to feedback@ukcloud.com.

    ☀
    ☾
    Generated by DocFX
    Back to top
    © UKCloud Ltd, 2021. All Rights Reserved.
    Privacy Policy. Terms of Use. Contribute.

    The UKCloud Knowledge Centre uses cookies to ensure that we give you the best experience on our website. If you continue we assume that you consent to receive all cookies on this website.