Day 6 of #100daysofcloud: Empowering VM Deployment with Azure PowerShell
Tags: 100daysofcloud, azure, adcstreak, cloud
Published: 2024-01-21 20:13:36

It's day 6 of the #100daysofcloud journey and today we took a deep dive into the world of Azure PowerShell, unraveling the intricate process of deploying Virtual Machines (VMs) through the command line.

### How Does Powershell Help Us?

- Scripted Deployments: Azure PowerShell empowers the automation of VM deployments, ensuring consistency and efficiency in creating virtualized environments.
- Custom Configurations: Tailor VMs to specific requirements by adjusting parameters such as size, image, and authentication credentials.
- Resource Group Integration: Seamlessly integrate VM deployments with Azure Resource Groups for organized and structured cloud resource management.
- Scaling Possibilities: With PowerShell, scale VM deployments effortlessly, adapting to fluctuating workloads and business needs.
- Infrastructure as Code (IaC): Implementing IaC principles, PowerShell scripts become a powerful tool for defining and managing infrastructure in a code-centric manner.
- DevOps Pipelines: Integrate VM deployments into DevOps pipelines, enhancing continuous integration and delivery processes.
- Disaster Recovery: PowerShell scripts facilitate the swift recreation of VM environments, crucial for disaster recovery scenarios.

## Commands I Learned
#### Connecting to Azure Account: 
We started the hands-on learning by connecting to our Azure account by "Connect-AzAccount," establishing a connection between the PowerShell environment and our Azure account. This step serves as the gateway to a plethora of Azure PowerShell commands.

#### Exploring the New-AzVM Command: 
The central piece of our exploration was the "New-AzVM" command, responsible for creating a new Azure Virtual Machine. Armed with the knowledge gained from previous days, we dissected the command's syntax and delved into the vast array of parameters available, from resource groups to VM sizes.



#### Interpreting the Get-Help Output: 
Navigating the extensive help documentation using "get-help New-AzVM" showcased the wealth of information at our disposal. Despite the initial overwhelming feeling, the breakdown of parameters and examples provided a roadmap for crafting precise PowerShell commands.

#### Interactive PowerShell Execution: 
The hands-on experience involved running commands like "Get-AzVM" to inspect existing VMs within our subscription. This not only reinforced our understanding but also served as a guide for crafting the deployment command.

#### Scripting Our Azure VM Deployment: 
With insights gained from exploring examples and the verbose output, we scripted the creation of a new VM. We understood the importance of specifying crucial parameters like resource groups, locations, network configurations, and more to tailor the VM deployment to our specific requirements.

#### Overcoming Challenges: Removing Resource Locks: 
A hiccup arose when attempting to remove the VM due to a resource lock from a previous day's activity. We navigated through this challenge by understanding the need to remove locks before deletion, showcasing the interconnected nature of Azure resource management. 

## Commands I used

```
# Connect to Azure Account
Connect-AzAccount

# Define Resource Group and Location
$resourceGroupName = "MyResourceGroup"
$location = "East US"

# Create a Resource Group
New-AzResourceGroup -Name $resourceGroupName -Location $location

# Define VM Configuration
$vmName = "MyVM"
$vmSize = "Standard_B2s"
$imageOffer = "WindowsServer"
$imageSku = "2019-Datacenter"
$adminUsername = "AdminUser"
$adminPassword = "YourPassword"

# Create VM Configuration
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize $vmSize
$vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName $vmName -Credential (Get-Credential -UserName $adminUsername -Message "Enter your password.")

# Choose VM Image
$vmConfig = Set-AzVMSourceImage -VM $vmConfig -Offer $imageOffer -PublisherName MicrosoftWindowsServer -Skus $imageSku -Version "latest"

# Create VM with Configuration
New-AzVM -ResourceGroupName $resourceGroupName -Location $location -VM $vmConfig -Verbose
```

Thanks, @azdevindia and @beingwizard for the enlightening session and resources.