PowerShell – ODBC SQL Query

Hre is an Example SQL Query Using ODBC and Powershell

The key bits there is to ensure the SQL Query  (in my example SELECT PrinterId,DeviceName,Description,ServerDefault  FROM $SQLDBName.dbo.Printer where Available = 1 ) actually works correctly in SQL Server Management Studio, with the credentials you are going to use with the ODBC system DSN.

In the example below, I am wanting a listed of printers from the table $SQLDBName.dbo.Printer where the field  “Available” equals 1 and returning to the $Result object the fields  PrinterId,DeviceName,Description,ServerDefault, as well as a bunch of other info automatically injected into the object (do a $Result |get-member to poke around)

I am then taking just the object $Result.rows, which funnily enough contains the rows from the SQL select statement



Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.