You can do this lots of ways. Here's a quick way from the command line:
tasklist /FI "IMAGENAME eq w3wp.exe"
then just line up the PID numbers.
So the way I am getting this data is just normal dbcc showcontig in a db. But for some reason, when I saw the output of dbcc showcontig today, I just decided I *had* to parse it with the command line. /sigh I just wanted the logical scan fragmentation. So this is, in part, the output.
Etc etc etc etc
To do this I just did a simple for loop with a call out to a …. I forget what these things are even called since no one uses them anymore or cares....