🚀 Managing Azure VMs just got easier! ☁️

📌 Description

I’ve been tinkering with PowerShell again and created a handy script to list all running Azure VMs and let you pick which ones to shut down – no more manual hassle! 💻✨ Whether you want to stop one, a few, or all of them, this script has you covered.

 

🚀 Features

✅ Lists all running VMs with their resource groups – Get a clear overview in seconds.

✅ Lets you select by number (e.g., "1,3") or type "all" – Flexible and user-friendly.

✅ Handles errors gracefully – Because nobody likes a crash mid-script.

 

🛠️ Prerequisites

1- PowerShell installed

2- Azure PowerShell module installed

3- Azure subscription

PowerShell Script

  

  
  
# First, we need to log in to Azure
Connect-AzAccount

# Get all running VMs
$runningVMs = Get-AzVM -Status | Where-Object { $_.PowerState -eq "VM running" }

# Check if there are any running VMs
if ($null -eq $runningVMs -or $runningVMs.Count -eq 0) {
    Write-Host "No running virtual machines found in Azure." -ForegroundColor Yellow
} else {
    Write-Host "Found $($runningVMs.Count) running virtual machines:" -ForegroundColor Green
    
    # Create an array to store VMs the user wants to shut down
    $vmsToShutdown = @()
    
    # Display all VMs and let the user choose
    $index = 1
    foreach ($vm in $runningVMs) {
        $resourceGroup = $vm.ResourceGroupName
        $vmName = $vm.Name
        
        Write-Host "`n[$index] Virtual Machine: $vmName" -ForegroundColor Cyan
        Write-Host "Resource Group: $resourceGroup"
        $index++
    }
    
    # Ask which VMs to shut down
    Write-Host "`nEnter the numbers of the VMs you want to shut down (separate with commas, e.g., 1,3)" -ForegroundColor Yellow
    $choices = Read-Host "Your choices (or 'all' to shut down all)"
    
    # If user selects 'all', include all VMs; otherwise, process the selected numbers
    if ($choices.ToLower() -eq "all") {
        $vmsToShutdown = $runningVMs
    } else {
        $selectedNumbers = $choices -split ',' | ForEach-Object { $_.Trim() }
        foreach ($num in $selectedNumbers) {
            $idx = [int]$num - 1
            if ($idx -ge 0 -and $idx -lt $runningVMs.Count) {
                $vmsToShutdown += $runningVMs[$idx]
            }
        }
    }
    
    # Shut down the selected VMs
    foreach ($vm in $vmsToShutdown) {
        $resourceGroup = $vm.ResourceGroupName
        $vmName = $vm.Name
        
        try {
            Write-Host "Shutting down $vmName..." -ForegroundColor Yellow
            Stop-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force -NoWait
            Write-Host "$vmName is being shut down." -ForegroundColor Green
        }
        catch {
            $errorMessage = $_.Exception.Message
            Write-Host "An error occurred while shutting down $vmName`: $errorMessage" -ForegroundColor Red
        }
    }
}

# Script completion message
Write-Host "`nScript completed!" -ForegroundColor Green
  

Kommentarer

Populära inlägg i den här bloggen

🚀 IntuneWin – Deploying Win32 Apps via Intune 🎯

Block Personal devices to acces to Desktop apps like teams, Onedrive etc and how to troubleshooting the issue.

🔧 Microsoft 365 Apps Admin Center: Tips & Tricks