Last active
March 19, 2025 01:18
-
-
Save langheran/76634c636bb3c533d02746e5daeff047 to your computer and use it in GitHub Desktop.
C:\Users\NisimHurst\NDS\scripts\cmds\sh_pod.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# read parameters from the command line | |
param( | |
[string]$namespace = "default", | |
[string]$pattern = "", | |
[bool]$previous = $false | |
) | |
# check if pattern has a space and if so, split it in two and use the first part | |
$container = "" | |
if ($pattern.Contains(" ")) { | |
$container = $pattern.Split(" ")[1] | |
$pattern = $pattern.Split(" ")[0] | |
write-host "Pattern: $pattern" | |
write-host "Container: $container" | |
} | |
$pattern = ".*$pattern.*" | |
# Define the namespace and the regular expression pattern to match pod names | |
# Get the list of pod names in the specified namespace | |
$params = "--no-headers" | |
if ($namespace -ne "default") { | |
$params += " -n $namespace" | |
} | |
write-host "Fetching pods" | |
$pods_ = Invoke-Expression $("kubectl get pods " + $params) | |
$pods = $pods_ | ForEach-Object { | |
$podName = $_.Split(" ")[0] | |
if ($podName -match $pattern) { | |
return $podName | |
} | |
} | |
# Check if any pods matched the pattern | |
if ($pods) { | |
# Retrieve logs from the matched pods | |
foreach ($pod in $pods) { | |
Write-Host "Connecting to pod: $pod" | |
$params = "exec -it " | |
if($previous) { | |
$params += " --previous" | |
} | |
$namespace_param = "" | |
if($namespace -ne "default") { | |
$namespace_param = " -n $namespace" | |
$params += $namespace_param | |
} | |
if ($container) { | |
# split by space and get all the container names | |
$containers_ = Invoke-Expression $("kubectl get pod " + $pod + $namespace_param + " -o jsonpath='{range .spec.containers[*]}{.name}{\""\n\""}{end}'") | |
# split by space and get all the container names | |
$newContainer = $containers_ | ForEach-Object { | |
$containerName = $_ | |
# write-host "Container: $containerName" | |
if ($containerName -match ".*" + $container + ".*") { | |
return $containerName | |
} | |
} | |
$params += " -c $newContainer" | |
} | |
$params += " $pod" | |
# $params += " -- bash" | |
$params += " -- /bin/sh" | |
# Write-Host "kubectl $params" | |
start-process -NoNewWindow -Wait -FilePath kubectl -ArgumentList $params | |
} | |
} else { | |
Write-Host "No pods found matching the pattern '$pattern' in namespace '$namespace'." | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
chcp 437 > nul | |
pushd %~dp0 | |
powershell -command ".\sh_pod.ps1 -pattern ""%*""" | |
popd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment