Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 691 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filter the output of a command as if it was text

#1
I have a simple question, but I am also a beginner in PowerShell. I think it has to do with the fact that the output of the [Get-Process][1] command (alias `ps`) is objects and not text.

I want to get a list of the services running that have the name "sql" in them.

This is what I tried so far, but every attempt returns nothing:

Get-Service | where {$_ -match 'sql'}

Get-Service | where {$_ -like 'sql'}

Get-Service | Select-String sql

I am looking for a pattern that lets me treat the output of every command as searchable text.

[1]:

[To see links please register here]



Reply

#2
The other answers are right of course about your specific question of starting services that have "sql" in their name, but to answer the generic question:

You can do `Get-Service | Out-String`, and you will get the output as string, much like how Unix commands work.

Also when the output is piped to non-PowerShell commands, it does get converted to text, so for example: `Get-Service | grep sql` would work the way you wanted.

But again, like @JPBlanc says, it is good embrace the way PowerShell works, which is that the outputs are objects. It gives you way more control and keeps things simple and readable (the Unix commands with sed, awk and what not operating on text output of other command outputs can get very cryptic!).


Reply

#3
You're working way too hard at it:

Get-Service *sql*

Reply

#4
Just ***forget it*** :o)

Outputs *are* objects. You are right, and you are going to use this.

So [mjolinor has the shortest answer][1], but for your knowledge just test:

Get-Service | Get-Member

So you will understand that

Get-Service | Where-Object {$_.name -match ".*sql.*" }

also works, and there you've got your text as a *property* of the object.

[1]:

[To see links please register here]





Reply

#5
That the text of the name is a property of the object is important to get your head around, and how to use the property values in a filter.

Another aspect of PowerShell you can leverage to solve this is selecting properties out of objects with [Select-Object][1] (alias `select`):

Get-Service | select -expand name

will get you a string array with the names of the servers, and two of your original three filters would work on that. The `-like` isn't going to work, because there's no wildcards in the test string. The only thing it will ever match is just 'sql'.

I still believe [the first solution I posted][2] is best. It's important to know how to do late filtering, but also how to use early filtering when you can.

[1]:

[To see links please register here]

[2]:

[To see links please register here]


Reply

#6
If anyone wants more information on logical operations, please see *[Using the Where-Object Cmdlet][1]*:

• -lt -- Less than

• -le -- Less than or equal to

• -gt -- Greater than

• -ge -- Greater than or equal to

• -eq -- Equal to

• -ne -- Not equal to

• -like - Like; uses wildcards for pattern matching

`Get-Service | where {$_ -match 'sql'}` would be `Get-Service | where {$_ -eq "sql"}`

`Get-Service | where {$_ -like 'sql'}` would be `Get-Service | where {$_ -like "sql"}`

And now an actual example.

```lang-none
PS C:\> Get-Service | where {$_.name -like "net*"}

Status Name DisplayName
------ ---- -----------
Running Net Driver HPZ12 Net Driver HPZ12
Running Netlogon Netlogon
```

[1]:

[To see links please register here]



Reply

#7
Get-Service | Select-String -Pattern "sql"

This works just like [grep][1]. And you can even sort:

Get-Service | Select-String -Pattern "sql" | sort

[1]:

[To see links please register here]

Reply

#8
Most answers here focus on finding the service name with "sql" in the name, not on filtering *the entire* output *as if it was text*. Also, the accepted answer uses a non-PowerShell function, "findstr".

So, granted, what follows is not the most elegant solution, but for sake of completeness I would like to provide the 100% PowerShell solution that takes the question of the OP literally:

(get-Service | Out-String) -split "`r`n" | Select-String sql

- We need [`Out-String`][1], because using the solutions provided in other answers doesn't provide us the full text output of the [Get-Service][2] command, only the `Name` parameter.
- We need to split on newlines, because [Select-String][3] seems to treat the entire text as one long string, and returns it as a whole, if "sql" is found in it.
- I use Select-String instead of [findstr][4], because findstr is not a PowerShell function.

This is a purist answer, and in practice, for this specific use-case, I would not recommend it. But for people coming here through Google Search based on the question title, this is a more accurate answer...

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]



Reply

#9
You probably want this:

Function Select-ObjectPropertyValues {
param(
[Parameter(Mandatory=$true, Position=0)]
[String]
$Pattern,
[Parameter(ValueFromPipeline)]
$input
)

$input | Where-Object {($_.PSObject.Properties | Where-Object {$_.Value -match $Pattern} | Measure-Object).count -gt 0} | Write-Output
}

Here we are going though each property of an object to see if it matches the given pattern. If the object contains one or more such properties, we write it out. End result: grep by all properties of an object.

Put it in your configuration files and grep to your heart's content.


Reply

#10
how about:


Get-Service| Out-String -stream | Select-String sql


where the key point is that -stream option converts the Out-String output in separate lines of text.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through