Select-String for linux users ?

Welcome Forums General PowerShell Q&A Select-String for linux users ?

This topic contains 8 replies, has 3 voices, and was last updated by

 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #119949

    Participant
    Points: 35
    Rank: Member

    I feel like this should be far easier to figure out , but it looks like it's not (at least for me)
    I have a text file sitting somewhere on a webserver containing a very very simple/basic shell script.
    This is the file

    Now on linux it would be very easy , to select just the keys from that file if i needed to

    wget http://web.url.com/keys.sh -qO- | egrep 'ssh-rsa|ecdsa-sha2-nistp256'

    I can't seem to do the same in powershell
    I tried different ways , but no success

    (Invoke-WebRequest -Uri http://keys.domain.com -UseBasicParsing).Content | Select-String -Pattern 'ssh-rsa' 
    # - doesn't work, all the file contents displayed
    $mykeys = Invoke-WebRequest -Uri http://keys.domain.com -UseBasicParsing
    $mykeys | Select-String -Pattern 'ssh-rsa'

    I need to loop trough the ssh-keys and add them to a YAML config file , so indentation need to be preserved, something like this

            $sectionPasswd = @"
    ssh_authorized_keys:
    
    "@
    
    foreach($key in $rootkeys) {
        $sectionPasswd += @"
     - $key
    
    "@
    }
    
    $sectionPasswd
  • #119958

    Participant
    Points: 889
    Helping Hand
    Rank: Major Contributor

    What is the output of (Invoke-WebRequest -Uri http://keys.domain.com -UseBasicParsing).Content ?
    I am able to get the keys using the same expression, but I do Get-Content (cat) from a file(I have saved this as a shell script).

    I can reproduce it when I do below

    Get-Content -Path c:\Temp\Test.sh | Out-String | Select-String -Pattern ssh-rsa
    #or
    Get-Content -Path c:\Temp\Test.sh -Raw | Select-String -Pattern ssh-rsa
    

    Can you try below, this should solve

    (Invoke-WebRequest -Uri http://keys.domain.com -UseBasicParsing).Content | Out-String -Stream | Select-String -Pattern ssh-rsa
    
  • #119977

    Participant
    Points: 35
    Rank: Member

    Hi,
    Yes ... it's true , it works from a file, but i would rather not save something to disk if i don't need to 🙂

    The example with Out-String doesn't work for me either (or am i doing something wrong) , it displays the whole file on the screen just like without that pipe

    • #120025

      Participant
      Points: 889
      Helping Hand
      Rank: Major Contributor

      Did you try with streaming ON,

      Out-String -Stream

  • #119994

    Participant
    Points: 297
    Helping Hand
    Rank: Contributor

    Pretty sure that the .Content property for Invoke-WebRequest dumps the string as a single big block, whereas Select-String expects one string per line of content to be able to reliable select.

    I'd try something like: (Invoke-WebRequest -Uri http://keys.domain.com -UseBasicParsing).Content -split '\r?\n' | Select-String -Pattern 'ssh-rsa'

  • #120051

    Participant
    Points: 35
    Rank: Member

    I'd try something like: (Invoke-WebRequest -Uri http://keys.domain.com -UseBasicParsing).Content -split '\r?\n' | Select-String -Pattern 'ssh-rsa'

    Thank you , that worked 🙂

  • #120378

    Participant
    Points: 35
    Rank: Member

    by the way , how can you tell if a commands output is dumped all at once like the conetent of Invoke-WebRequest ?
    thank you!

  • #120417

    Participant
    Points: 297
    Helping Hand
    Rank: Contributor

    I guess the simplest way is to check the Count property; if it's more than 1, it's split into an array.

    $Req = Invoke-WebRequest https://www.google.com
    # Wrap in @() to ensure it comes out as an array (even if it's only a single item) with a Count property you can check
    @($Req.Content).Count
  • #120423

    Participant
    Points: 35
    Rank: Member

    Oh , nice trick
    Thanks , bookmarked 🙂

You must be logged in to reply to this topic.