Powershell: Get SQL Data using powershell

Small background / Use Case – I’m having a printing solution that prints a small, almost invisible watermark on every printed page. This watermark consists of printjob ID number only. I would like HelpDesk users to check this Job ID and resolve job owner / printing time etc. Unfortunately, it’s not possible through GUI, but… powershell made my day.

Ok, to summarize, I currently have:

  • Print Job ID
  • Running SQL Server with required print job and user tables

And I want to get:

  • Nice GUI window asking for print job ID
  • GUI output with:
    • User details (name, surname, login)
    • Job title
    • Job accept time

Translating above into powershell; first you need to specify database details and open up SQL connection:

Then, we are going to ask user for job ID with small GUI window:

Above pop-up code will show up in following style:

powershell-gui-popup

Of course, you can validate this input box with string length, type etc. I didn’t need it. Next step – Run SELECT SQL query using powershell to get JobID related userID and then run another query to fetch user details from users table:

Last step, having all that information in variables, you can display ’em to end user, again, using powershell GUI window:

This will result in something similar to:

powershell-gui-popup-2

At the end, please keep in mind to close SQL connection:

This was just one, small use case that worked for me, but there are many more possibilities automate your work using powershell and SQL together 🙂

For non-reading ones, below you can get complete script:

 

Leave a Reply