Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

How would I make an Azure Virtual Machine using Powershell?

  1. User

    How would I make an Azure Virtual Machine using Powershell?

  2. Assistant

    To create an Azure virtual machine using PowerShell, follow the steps below:

    1. Install and configure the Azure PowerShell module:

      • Open PowerShell as an administrator.
      • Run the following command: Install-Module -Name Az -AllowClobber -Scope CurrentUser
      • If prompted to install the NuGet provider, type "Y" and press Enter.
      • After the installation completes, run the command: Import-Module Az
      • Finally, sign in to your Azure account using the command Connect-AzAccount.
    2. Create a resource group:

      • Run the command: New-AzResourceGroup -Name <ResourceGroupName> -Location <Location>, replace <ResourceGroupName> with the desired name for the resource group, and <Location> with the desired Azure region.
    3. Create a virtual network and subnet:

      • Run the command:
        $subnetConfig = New-AzVirtualNetworkSubnetConfig -Name <SubnetName> -AddressPrefix <SubnetCIDR>
        New-AzVirtualNetwork -Name <VNetName> -ResourceGroupName <ResourceGroupName> -Location <Location> -AddressPrefix <VNetCIDR> -Subnet $subnetConfig
        
      • Replace <SubnetName> with the desired subnet name, <SubnetCIDR> with the subnet CIDR notation (e.g., "10.0.0.0/24"), <VNetName> with the desired virtual network name, <ResourceGroupName> with the resource group name created in the previous step, <Location> with the Azure region, and <VNetCIDR> with the virtual network CIDR notation (e.g., "10.0.0.0/16").
    4. Create a public IP address:

      • Run the command:
        $publicIP = New-AzPublicIpAddress -Name <PublicIPName> -ResourceGroupName <ResourceGroupName> -Location <Location> -AllocationMethod Dynamic
        
    5. Create a network security group:

      • Run the command:
        $nsg = New-AzNetworkSecurityGroup -ResourceGroupName <ResourceGroupName> -Location <Location> -Name <NSGName>
        
    6. Create a virtual network interface:

      • Run the command:
        $nic = New-AzNetworkInterface -Name <NICName> -ResourceGroupName <ResourceGroupName> -Location <Location> -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIP.Id -NetworkSecurityGroupId $nsg.Id
        
    7. Create the virtual machine:

      • Run the command:
        $vm = New-AzVMConfig -VMName <VMName> -VMSize <VMSize> -Location <Location>
        $vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName <VMName> -Credential (Get-Credential)
        $vm = Add-AzVMNetworkInterface -VM $vm -Id $nic.Id
        $vm = Set-AzVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter-Server-Core -Version latest
        $vm = Set-AzVMOSDisk -VM $vm -StorageAccountType Standard_LRS -DiskSizeInGB 127 -CreateOption FromImage -Caching ReadWrite
        New-AzVM -ResourceGroupName <ResourceGroupName> -Location <Location> -VM $vm
        
      • Replace <VMName> with the desired name for the virtual machine, <VMSize> with the desired size (e.g., "Standard_DS2_v2"), <Location> with the Azure region, and <ResourceGroupName> with the name of the resource group.

    After running these commands, the Azure virtual machine will be created using PowerShell. Adjust the parameters as necessary to suit your specific requirements.