#!/bin/bash

# Stop VMs based on a tag
TAG_NAME="Environment"
TAG_VALUE="Dev"

echo "Searching for VMs with tag $TAG_NAME=$TAG_VALUE..."

# Get list of VM IDs that match the tag
VM_IDS=$(az vm list --query "[?tags.$TAG_NAME=='$TAG_VALUE'].id" -o tsv)

if [ -z "$VM_IDS" ]; then
    echo "No VMs found with tag $TAG_NAME=$TAG_VALUE"
    exit 0
fi

for VM_ID in $VM_IDS; do
    echo "Stopping VM: $VM_ID"
    # Deallocate to stop billing
    az vm deallocate --ids "$VM_ID" --no-wait
done

echo "Stop commands issued."
