What Runs on the SQL Server when you run a PowerShell script?–Question from #SQLRelay

Last week I ran a PowerShell lab at SQL Relay in Cardiff. There are still a few places available for SQL Relay week 2. Take a look here for more details and follow the twitter hashtag #SQLRelay for up to date information

The link for my slides and demos from the second part are here https://t.co/Fik2odyUMA

Whilst we were discussing Show-LastDatabaseBackup Kev Chant @KevChant asked where it was getting the information from and I answered that PowerShell was running SQL commands under the hood against the server and if you ran profiler that is what you would see. We didn’t have time to do that in Cardiff but I thought I would do it today to show what happens

A reminder of what Show-LastDatabaseBackup function does

image

If we start a trace with Profiler and run this function we get these results in PowerShell

image

In Profiler we see that it is running the following T-SQL for

image

 

 

and then for

image

 

 

For

image

 

 

And for

image

 

 

So the answer to your question Kev is

Yes it does get the information from the msdb database

Please feel free to comment on this post. All comments are moderated first before appearing on the site