awk from bash to PowerShell

11/5/2019

I want to map the host port 5000 to the minikube port. The command already works in the bash but now I need it in PowerShell.

I have already changed the bash command to the point "Code in PowerShell" and now only need to replace the awk command.

Code in Bash:

kubectl port-forward --namespace kube-system $(kubectl get po -n kube-system | grep kube-registry-v0 | awk '{print $1;}') 5000:5000

Code in PowerShell (awk to be replaced)

kubectl port-forward --namespace kube-system $(kubectl get po -n kube-system | Select-String -Pattern "kube-registry-v0" | awk '{print $1;}') 5000:5000
-- Sibylla Heckmann
kubectl
kubernetes
minikube
powershell

1 Answer

11/5/2019

Here's a way to do it. Select-string actually returns on object with multiple properties, and the line property has the string.

kubectl port-forward --namespace kube-system (kubectl get po -n kube-system |
  Select-String kube-registry-v0 | 
  foreach { -split $_.line | select -index 0} ) 5000:5000

Another way, using where or where-object, and the array index [0] notation:

kubectl port-forward --namespace kube-system (kubectl get po -n kube-system |
  where { $_ -match 'kube-registry-v0' } | 
  foreach { (-split $_)[0] ) 5000:5000

Similar post about awk and powershell: Awk command to Powershell equivalent

-- js2010
Source: StackOverflow