How do I create an Azure Virtual Machine (virtual machine) by using PowerShell

Spread the love

Each now and then I attempt to create new resources for my Azure subscription using Command line utilities (PowerShell and CLI) in order to stay current with the advancements and changes of the various ways to create resources. While Azure portal is a great tool for creating virtual machines, Azure portal is a fantastic GUI to create virtual machine, it does not meet the needs in terms of customization. I’m pretty obsessive regarding my environment, and you should too. A well-organized cloud space is joyful cloud environment.

One thing I attempt to make a mark on the minds of my clients each day, is the necessity for standardization. If you don’t have a set of standards in place for SSIS packages Data Factory Pipelines SQL Server names, database objects, and in the present instance, cloud resources, you’re setting yourself up for a major maintenance headache in the near future. Set your standards early and ensure that you adhere to them, with only a only a few exemptions (some Azure resources just can’t be changed in name).

We’re now in the middle of our journey: Virtual Machine Resource Naming.

The process of creating a virtual machine on Azure’s Azure portal is fast and simple. Just fill in a few details, press Create and you’ll have a VM ready for use in just a couple of minutes!

How do I create an Azure Virtual Machine (virtual machine) by using PowerShell

For this script to work, you must possess a resource groups as well as a virtual network set up or you can extend the script to incorporate the other items. The script is quite basic and you are at ease to use it and extend it in any way you like. I’ve taken bits of code from various Azure document pages, and incorporated them into my own code to reach the point where it’s not entirely my own creation. Some good additions to this script could be the addition of a data disk, and an NSG. For my test environment , however I’m getting to where I’m supposed to be. In the future, maybe I’ll add more, and post it here.

#Install the Az module if you haven't done so already.
#Install-Module Az
#Login to your Azure account.
#Define the following parameters for the virtual machine.
$vmAdminUsername = "LocalAdminUserNameHere"
$vmAdminPassword = ConvertTo-SecureString "LocalAdminP@sswordHere" -AsPlainText -Force
$vmComputerName = "BS-SRV-SQL02"
#Define the following parameters for the Azure resources.
$azureLocation              = "EastUS2"
$azureResourceGroup         = "BSDomain-RG"
$azureVmName                = "BS-SRV-SQL02"
$azureVmOsDiskName          = "BS-SRV-SQL02-OS"
$azureVmSize                = "Standard_E4s_v3"
#Define the networking information.
$azureNicName               = "BS-SRV-SQL02-NIC"
$azurePublicIpName          = "BS-SRV-SQL02-IP"
#Define the existing VNet information.
$azureVnetName              = "BSDomain-Vnet"
$azureVnetSubnetName        = "default"
#Define the VM marketplace image details.
$azureVmPublisherName = "MicrosoftWindowsServer"
$azureVmOffer = "WindowsServer"
$azureVmSkus = "2019-Datacenter"
#Get the subnet details for the specified virtual network + subnet combination.
$azureVnetSubnet = (Get-AzVirtualNetwork -Name $azureVnetName -ResourceGroupName $azureResourceGroup).Subnets | Where-Object {$_.Name -eq $azureVnetSubnetName}
#Create the public IP address.
$azurePublicIp = New-AzPublicIpAddress -Name $azurePublicIpName -ResourceGroupName $azureResourceGroup -Location $azureLocation -AllocationMethod Dynamic
#Create the NIC and associate the public IpAddress.
$azureNIC = New-AzNetworkInterface -Name $azureNicName -ResourceGroupName $azureResourceGroup -Location $azureLocation -SubnetId $azureVnetSubnet.Id -PublicIpAddressId $azurePublicIp.Id
#Store the credentials for the local admin account.
$vmCredential = New-Object System.Management.Automation.PSCredential ($vmAdminUsername, $vmAdminPassword)
#Define the parameters for the new virtual machine.
$VirtualMachine = New-AzVMConfig -VMName $azureVmName -VMSize $azureVmSize
$VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $vmComputerName -Credential $vmCredential -ProvisionVMAgent -EnableAutoUpdate
$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $azureNIC.Id
$VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName $azureVmPublisherName -Offer $azureVmOffer -Skus $azureVmSkus -Version "latest"
$VirtualMachine = Set-AzVMBootDiagnostic -VM $VirtualMachine -Disable
$VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -StorageAccountType "Premium_LRS" -Caching ReadWrite -Name $azureVmOsDiskName -CreateOption FromImage
#Create the virtual machine.
New-AzVM -ResourceGroupName $azureResourceGroup -Location $azureLocation -VM $VirtualMachine -Verbose

When the script is finished, I am left with an VM as well as a NIC, IP address and OS disk with the names I want. My OCD is happy, as is my Azure environment is well-organized and meets my requirements!

What is PowerShell in Azure?

PowerShell in Azure is an advanced scripting language designed for automating and managing cloud-based tasks and processes within Microsoft Azure. It’s a command-line shell and scripting language built upon the.NET framework, designed to assist IT pros configure and administer Azure cloud services more efficiently compared to manual methods. PowerShell offers more efficiency when managing resources within the cloud compared to manual methods alone.

PowerShell in Azure automates tasks such as creating and managing virtual machines, running scripts and commands, deploying applications, managing resources like storage, networking, and databases. It can be used to create, monitor, manage user accounts as well as administer Azure Active Directory. Furthermore, users have the capability of monitoring application performance with this powerful tool.

PowerShell can be used to integrate with a range of Azure services, such as Azure Storage, Azure Service Bus, Azure SQL Database and Azure Functions. It provides scripting support for creating automation workflows in Azure Automation. Furthermore, PowerShell manages Marketplace items like creating virtual machines or managing Azure Resource Manager templates.

PowerShell in Azure provides a secure and scalable way to manage cloud resources. It simplifies the management of Azure resources, allows creating scripts/commands for automation tasks/processes, as well as orchestrating application deployments and services.

PowerShell in Azure allows IT pros to create, manage, and automate resources in the cloud. It offers a secure and efficient way of handling cloud-based resources while helping streamline management of these assets.

What is the main purpose of PowerShell?

PowerShell is a Microsoft task automation and configuration management framework composed of a command-line shell and scripting language. It was built upon the.NET Framework with the purpose of helping IT pros configure systems and automate administrative tasks.

PowerShell was designed to give system administrators a powerful, user-friendly, and consistent tool for managing Windows-based systems. It simplifies system administration tasks through an extensive scripting language and collection of cmdlets (pronounced “command-lets”) that make complex tasks much simpler.

PowerShell can be useful for automating repetitive tasks such as user and group management, file system operations, creating services and configuring network settings. Furthermore, it offers an interactive shell environment to quickly execute commands and scripts.

PowerShell allows system administrators to easily create and update PowerShell scripts for automation of complex tasks. It is an indispensable part of enterprise IT infrastructure management, making it simpler for administrators to administer Windows servers, workstations, and services with ease.

PowerShell allows system administrators to quickly and easily create scripts to automate various system tasks, such as creating users, managing services, creating backups, and managing user accounts. Furthermore, they can write scripts for more complex jobs like installing software or configuring network settings.

PowerShell is an indispensable tool for system administrators and IT professionals, simplifying the management of Windows-based systems. It offers a powerful interactive shell environment and scripting language to quickly execute commands and scripts, as well as making automation complex tasks possible. PowerShell plays an integral role in enterprise IT infrastructure management by allowing administrators to automate various system processes and quickly create scripts to handle more intricate jobs.

Can I run a PowerShell script in Azure?

The Run Command feature uses the virtual machine (VM) agent to execute PowerShell scripts inside an Azure Windows Virtual Machine. The scripts are available to manage your general machine or application management. They can assist you to quickly find and correct VM access issues and network issues and bring your VM back to its original condition.

People Also View below Post


Spread the love

Leave a Comment