I add here a small script that was drafted with AI but was tested and modified by me so that it returns the OS version for all of the VM instances under a project in GCP
#!/bin/bash
# Prompt for the project ID
read -p "Enter your Google Cloud Project ID: " PROJECT_ID
if [ -z "$PROJECT_ID" ]; then
echo "Project ID cannot be empty."
exit 1
fi
echo "Fetching VM instances and their OS versions for project: $PROJECT_ID"
echo "--------------------------------------------------------------------"
echo "Instance Name | Zone | OS (from inventory)"
echo "---------------------|----------------------|---------------------"
# Get all instances (name and zone) in the project
# The `instances list` command can list instances across all zones if no --zones flag is specified.
# However, `instances describe` requires a specific zone.
# So, we list name and zone, then describe each.
gcloud compute instances list --project="$PROJECT_ID" --format="value(name,zone)" | while read -r INSTANCE_NAME ZONE; do
# Get the licenses of the boot disk for the current instance
# We filter for the disk that has boot=true
# If multiple licenses, it takes the first one. Often OS licenses are listed.
OS_INFO=$(gcloud compute instances os-inventory describe "$INSTANCE_NAME" \
--zone="$ZONE" \
--project="$PROJECT_ID" \
--format="value(SystemInformation.LongName)" 2>/dev/null)
# If no license info found, display a placeholder
if [ -z "$OS_INFO" ] || [ "$OS_INFO" = "None" ]; then
OS_VERSION="N/A or Custom"
else
# The command above should already give the last part, but an extra check/cleanup
OS_VERSION="$OS_INFO"
fi
# Print the instance name, zone, and OS version in a formatted way
printf "%-20s | %-20s | %s\n" "$INSTANCE_NAME" "$ZONE" "$OS_VERSION"
done
echo "--------------------------------------------------------------------"
I don’t think any other details are needed in regard to this.
Cheers,
Sorin