Creating a virtual host is a non-open operation in an automated process related to azure. Due to the complexity of the system itself, it is difficult to complete the creation of a virtual host with one or two simple commands. So write an article specifically to record the use of PowerShell to create a virtual host on Azure (Ubuntu server).
Virtual host virtual hosts need to be associated with other foundational components to provide services that are available, including: network cards, public IP addresses, virtual networks, network security groups, storage, and so on. Contains additional components that are required to create a new virtual machine:
This means that when we create a virtual machine, we also have to create these components one by one.
Defining Variables
We hope to reuse this script in the future, so put all the variables we use together to make it easier to modify or initialize with the parameters of the script:
$rgName="Vmpool"$rgLocation="East Asia"$subnetConfigName=$rgName+"Subnet"$vnetName=$rgName+"vnet"$vmName="Vmxman"$pipName=$vmName+"Pip"$nsgRule 22Name="NsgRule22"$nsgName=$rgName+"NSG"$interfaceName=$vmName+"Nic"$storageName=$rgName+"Storage"$storageType="Standard_grs"$oSDiskName=$vmName+"Osdisk"$vmSize="standard_d1"$vmVersion="16.04-lts"$userName="Nick"$userPassword="123456"
Hope not to scare you. That's right! This is the need for so many variables, which is not explained here, in the back to use a said one.
create credentials for a logon virtual machine
The virtual machine created by this script creates a user by default, you need to specify the user's name and login password (our created user is logged in by Public key authentication, this password is not really used). Create a Credential object from a user name and password:
$securePassword $userPassword -asplaintext-Force$userCred = New-object System.Management.Automation.PSCredential ($userName$securePassword)
Create Resource Group
Create a new Resource group, the virtual machine and all its related components belong to the same Resource group:
$rgName $rgLocation
The location of the Resource Group specified in the parameter locations is East Asia (access speed is faster).
Create a virtual network
Next create the virtual network. Create a configuration of a subnet first:
$subnetConfig $subnetConfigName -addressprefix 192.168.1.0/24
Then create a virtual network with one subnet:
$vnet $rgName $rgLocation ` $vnetName $subnetConfig
Finally, create a public IP for the host that can be accessed outside the network:
$pip $rgName $rgLocation ` -allocationmethod static-idletimeoutinminutes 4 ' $pipName
Create a network security group
A rule that allows access to port 22 needs to be configured first:
$nsgRule $nsgRule 22Name -Protocol Tcp ' -direction inbound-priority 200-sourceaddressprefix *-sourceportrange *- Destinationaddressprefix * ' -destinationportrange 22-access allow
Then create a network security group:
$NSG $rgName $rgLocation ` $nsgName $nsgRule
Create a network interface
The host also lacks a network card, so create a virtual network card for the host:
$nic $interfaceName $rgName $rgLocation ` $vnet $pip $NSG. Id
Create Storage account
The disk files for the virtual machine are stored in azure storage as blobs, so we need to create a storageaccount to store the disk files:
$storageAccount $rgName $storageName $storageType $rgLocation
The following defines the storage location and name of the disk file:
$oSDiskUri $storageAccount " vhds/ " $oSDiskName " . VHD "
Create a virtual machine
The following creates a virtual machine-related configuration:
$vmConfig $vmName $vmSize | ` Set$vmName$userCred -disablepasswordauthentication | ` Set$vmVersion -version Latest | ` Add$nic. Id | ` Set$oSDiskUri -createoption fromimage
The virtual machine operating system we created is Ubuntu Server 16.04-lts, which prohibits logging in with a user name password. To enable a user to log on through a public key, the user's public key must be provided:
$sshPublicKey " Nick ' s SSH public key "
The following command writes the public key you provide to the user's Authorized_keys file:
$vmconfig $sshPublicKey " /home/$userName/.ssh/authorized_keys "
The following command really creates a virtual machine on Azure:
$rgName $rgLocation $vmConfig
access rights issues
What do you do in a PowerShell script to perform an operation on Azure that requires the user to sign in first?
The author in the "Azure Foundation: Automatic login with PowerShell" in the article has a detailed introduction, interested friends can refer to.
Azure Fundamentals: Creating Virtual Machines with PowerShell