Projecting commands in pipeline

Welcome Forums General PowerShell Q&A Projecting commands in pipeline

Viewing 3 reply threads
  • Author
    Posts
    • #255542
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      Hello

      I am new to powershell so apologies in advance if this is a silly question.

      I am trying to write a powershell pipeline that will take output from kubectl, project that into a state I can do logic on (filter mostly) and then apply commands to it.

      My first pipeline which will cache all the pvc objects (this is essentially just a big json object of which I extract a few fields)

      At this point I have a bunch of objects in $pvcs with the names I want. Piping this to Format-Table looks great.

      Now to my problem:

      I want to run a kubectl command on every one of these objects, the only way I have been able to generate the command I want to do is via a for loop which produces a string I can throw to the call operator

      foreach ($i in $pvcs) {"kubectl delete pod $($i.Name)" }

      Is there a more concise way of doing this were I can simply pipe my objects from the first statement into a set of commands that I want to execute?

      Thank you for your help

      Joakim

      • This topic was modified 2 weeks, 3 days ago by grokkit. Reason: code formatting
    • #255554
      Participant
      Topics: 0
      Replies: 81
      Points: 362
      Helping Hand
      Rank: Contributor

      I don’t know much about kubectl, but if you are calling an external executable, you should not even need a call operator. If you don’t need anything stored in a variable, you can pipe to Foreach-Object {}

      Inside Foreach-Object{}, $_ is the current object passed into the pipeline. So you could continue executing other commands within the script block on that same object if you wish.

    • #255629
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      This was exactly what I was looking for, thank you so much!

    • #255965
      Senior Moderator
      Topics: 9
      Replies: 1311
      Points: 4,794
      Helping Hand
      Rank: Community Hero

      you would wanna have a look at below PowerShell module developed by Jim from PowerShell team as well.

      https://www.powershellgallery.com/packages/Microsoft.PowerShell.KubeCtl/0.0.3

      PS: PowerShell 7 is a prereq for this

Viewing 3 reply threads
  • You must be logged in to reply to this topic.