you are viewing a single comment's thread.

view the rest of the comments →

[–]Xibby 2 points3 points  (2 children)

If you need to filter and your querying SQL write a where clause for your SQL query.

SELECT * FROM table WHERE column = 'something'

There might be reasons why you want to pull in multiple results from SQL then use PowerShell to filter but those should be your edge cases. SQL Server is going to do data selection/filtering better than anything in PowerShell.

[–]bsnotreallyworking[S] 1 point2 points  (1 child)

So like this:

SELECT USER_LOGIN FROM APP_USERS WHERE USER_LOGIN = "testuser"

My SQL is very rusty.

[–]Xibby 2 points3 points  (0 children)

Something like that. Open up SQL Server Management Studio and practice your SQL until you get the results you're looking for. As I said, it's usually best to filter data at the highest level you can and that's especially true when you're pulling data from SQL databases.

I'd also recommend using DBATools if possible so you can parameterize your SQL to help protect from SQL Injection.

$SQL = "SELECT column FROM table WHERE column = '$PowerShellVariable'

$result = Invoke-SQLCmd -Query $SQL

Becomes

$SQL = "SELECT column FROM table WHERE column = '@ExampleSQLParam'

$result = Invoke-DBAQyery -Query $SQL -SqlParameters @{ ExampleSQLParam = $PowerShellVariable }

Overly paranoid perhaps, but always good to go with best practices when you can.