NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

# PowerShell version of the script

# Output file
$OutputFile = "gkeout.txt"

# Start time
$StartTime = Get-Date
Add-Content -Path $OutputFile -Value "Script started at: $($StartTime.ToString('yyyy-MM-dd HH:mm:ss'))"
Add-Content -Path $OutputFile -Value "GCP Project ID | Cluster Name | Kubernetes Version | Cluster IP Address"
Add-Content -Path $OutputFile -Value "-------------------------------------------------------------"

# Get a list of all GCP projects
$Projects = gcloud projects list --format="value(projectId)" | Out-String | ForEach-Object { $_ -split "`n" } | Where-Object { $_ -ne "" }

if (-not $Projects) {
Add-Content -Path $OutputFile -Value "No GCP projects found. Exiting."
Exit
}

$ProjectCount = 0

# Traverse through each project
foreach ($Project in $Projects) {
$ProjectCount++
Write-Output "Inspecting project $ProjectCount: $Project"

# Set the project in gcloud
$SetProjectResult = gcloud config set project $Project 2>&1
if ($LASTEXITCODE -ne 0) {
Add-Content -Path $OutputFile -Value "$ProjectCount | $Project | Failed to set project. Skipping."
continue
}

# Get the list of GKE clusters and their details
$Clusters = gcloud container clusters list --format="value(name,currentMasterVersion,endpoint)" | Out-String | ForEach-Object { $_ -split "`n" } | Where-Object { $_ -ne "" }

if (-not $Clusters) {
Add-Content -Path $OutputFile -Value "$ProjectCount | $Project | No GKE clusters found. Skipping."
continue
}

# Append cluster details to the output file
foreach ($Cluster in $Clusters) {
$Parts = $Cluster -split "s+"
if ($Parts.Length -lt 3) {
continue
}
$ClusterName = $Parts[0]
$ClusterVersion = $Parts[1]
$ClusterIP = $Parts[2]
Add-Content -Path $OutputFile -Value "$ProjectCount | $Project | $ClusterName | $ClusterVersion | $ClusterIP"
}
}

# End time
$EndTime = Get-Date
Add-Content -Path $OutputFile -Value "Script ended at: $($EndTime.ToString('yyyy-MM-dd HH:mm:ss'))"

# Calculate time taken
$TimeTakenMinutes = ($EndTime - $StartTime).TotalMinutes

# Analyze and summarize
$VersionSummary = @{}
$RegionSummary = @{}
$TotalClusters = 0

$Lines = Get-Content -Path $OutputFile | Select-Object -Skip 3
foreach ($Line in $Lines) {
$Parts = $Line -split '|'
if ($Parts.Length -lt 4) {
continue
}

$ClusterVersion = $Parts[3].Trim()
$ClusterName = $Parts[2].Trim()

if ($ClusterVersion -ne "") {
if (-not $VersionSummary.ContainsKey($ClusterVersion)) {
$VersionSummary[$ClusterVersion] = 0
}
$VersionSummary[$ClusterVersion]++
$TotalClusters++
}

$RegionMatch = $ClusterName -match "us-[a-z]+[0-9]"
if ($RegionMatch) {
$Region = $Matches[0]
if (-not $RegionSummary.ContainsKey($Region)) {
$RegionSummary[$Region] = 0
}
$RegionSummary[$Region]++
}
}

# Write version summary
Add-Content -Path $OutputFile -Value "nClusters by Kubernetes Version:"
foreach ($Version in $VersionSummary.Keys) {
Add-Content -Path $OutputFile -Value "$($VersionSummary[$Version]) clusters running Kubernetes version $Version"
}

# Write region summary
Add-Content -Path $OutputFile -Value "nClusters by Region:"
foreach ($Region in $RegionSummary.Keys) {
Add-Content -Path $OutputFile -Value "$($RegionSummary[$Region]) clusters in region $Region"
}

# Final summary
Add-Content -Path $OutputFile -Value "nSummary:"
Add-Content -Path $OutputFile -Value "Total projects inspected: $ProjectCount"
Add-Content -Path $OutputFile -Value "Total clusters found: $TotalClusters"
Add-Content -Path $OutputFile -Value "Script runtime: $([math]::Round($TimeTakenMinutes, 2)) minutes"

Write-Output "Script finished. Output saved to $OutputFile"
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.