Do you often create virtual machines in Azure? Have you ever wished you could automate it? Then this article is for you! This PowerShell script will create a new VM in Azure and prompt the user to set the new VM's local administrator credentials. Depending on your processes and environment, you may want to plug in some default credentials or create random credentials for each new VM and save them in a protected location or password manager. There are a lot of variables involved with creating a new virtual machine (VM) in Azure. From configuring the Resource Group all the way down to the new VM's network interface card (NIC), this article will cover each variable involved in creating a new VM in Azure. Click below to view the full .ps1 file on github Best Practices for Windows VMs in Azure This article can't cover the best configuration for every environment, so be sure that the variables are changed before using this script. For more information see Microsoft's article covering best practices for Windows VMs in Azure. This article assumes that best practices are being followed in your environment already. The variables we will configure in this article include:
Global Variables In this instance, the resource group and location for resources this VM will use are global variables. If you already have a resource group in mind, be sure to change the $ResourceGroupName variable accordingly. ## Global Compute Variables Next we will define the name of the VM, the computer name as seen by Windows, the size of the VM, and the name of the boot disk that Windows will run on. The disk will automatically be named based on the name of the VM. ## Compute Storage Variables Storage accounts must be unique across Azure and contain all lowercase characters. This script accounts for that by generating a random string and then converting that string to lowercase using the ".ToLower" function. This script creates a new storage account for the VM. If a storage account exists, replace the $StorageAccount variable with "Get-AzureRmStorageAccount | Select storageaccountname". ## Storage Network Variables The network variables are named based on the name of the VM so keep that in mind. For more information see Plan Virtual Networks in Azure. ## Network Create OS Credentials Next we'll need to define a local administrator account to be used to log into the VM. This can be pre-configured, but this script prompts the user running the script to create it on the spot so that they can be specific to each VM. The credentials are assigned to the $Credential variable. $Credential = Get-Credential Add Variables to Virtual Machine Configuration This portion of the script assigns the values and variables to the $VirtualMachine variable to be used in the creation of the VM. ## Add Variables to Virtual Machine Configuration Create the VM Now that the variables are all stored in the $VirtualMachine variable, it can be used for a rather short one-line command to actually create the virtual machine. ## Create the VM Conclusion
Whew! We're done. Now we have a VM in Azure that has been created using PowerShell. Feel free to use all or portions of this script to help your organization automate your workloads in Azure. |
RepositoriesAuthorSheridan's interests are in technology, business, music, and adventures Categories
All
Archives
June 2019
|