Select-String for linux users ?

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 https://gist.github.com/aduzsardi/adf0d2282213e3607c71e8695c2707ae

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

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

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 :slight_smile:

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

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’

Did you try with streaming ON,

Out-String -Stream

[quote quote=119994]
I’d try something like: (Invoke-WebRequest -Uri http://keys.domain.com -UseBasicParsing).Content -split ‘\r?\n’ | Select-String -Pattern ‘ssh-rsa’[/quote]

Thank you , that worked :slight_smile:

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

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

Oh , nice trick
Thanks , bookmarked :slight_smile: