I am trying to run a command like this:
kubectl get namespaces -o custom-columns=:metadata.name --no-headers
| Where-Object{$_.StartsWith("test") -or $_.StartsWith("dev")}
| kubectl get namespaces
(NOTE: the line breaks are just for readability, there are no line breaks in my actual command.)
The end goal is to apply a label to a subset of namespaces, but for now, I am just trying to feed the filtered piped list of spaces back in to kubectl
so it will get
them.
But I can't seem to get kubectl
to accept the piped list as the list of namespaces to return. (this just returns all the namespaces on my cluster).
How can I get kubectl
to take the pipe as input? (So I can filter the list of namespaces.)
The kubectl
docs make no mention of pipeline (stdin) input, so it may not be supported.
You state that passing multiple names as arguments can be achieved with appending a space-separated list of names to kubectl get namespaces
Therefore, you can simply collect the names in an array - which PowerShell automatically does when you collect an external-program call's output in a variable, with each output line becoming its own array element[1] - and use it as an argument:
$namespaces =
kubectl get namespaces -o custom-columns=:metadata.name --no-headers |
Where-Object { $_.StartsWith("test") -or $_.StartsWith("dev") }
kubectl get namespaces $namespaces
PowerShell automatically passes an array's elements as individual, (stringified) arguments to external programs.
[1] If there happens to be just one line of output, it is collected as-is, i.e., as a [string]
rather than a single-element array containing that string. However, the approach still works in that case. If you explicitly do need an array, use something like [array] $namespaces = ...