Powershell and "sp_who"Is it possible to use PowerShell to simply invoke the t-sql &quot;sp_who&quot; and have it return the result as an object? (sp_who and sp_who2 are OK but the DBNames are not displayed in the order I want. I can tweak Microsoft's code but was hoping that PowerShell could help out in some manner.) TIA, barkingdog © 2009 Microsoft Corporation. All rights reserved.Wed, 24 Jun 2009 03:04:50 Z903bcebf-7c78-439c-997e-ae96bda00b3fhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#903bcebf-7c78-439c-997e-ae96bda00b3fhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#903bcebf-7c78-439c-997e-ae96bda00b3fbarkingdoghttp://social.technet.microsoft.com/Profile/en-US/?user=barkingdogPowershell and "sp_who"Is it possible to use PowerShell to simply invoke the t-sql &quot;sp_who&quot; and have it return the result as an object? (sp_who and sp_who2 are OK but the DBNames are not displayed in the order I want. I can tweak Microsoft's code but was hoping that PowerShell could help out in some manner.) TIA, barkingdog Mon, 15 Jun 2009 03:50:49 Z2009-06-26T17:37:48Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#6937e00d-4ed4-4027-9792-de75f08698d6http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#6937e00d-4ed4-4027-9792-de75f08698d6Alexey.Martseniukhttp://social.technet.microsoft.com/Profile/en-US/?user=Alexey.MartseniukPowershell and "sp_who"<p>cd SqlServerProviderSnapin100\SqlServer::SQLSERVER:\SQL\&lt;instance name&gt;<br/>Invoke-Sqlcmd 'sp_who'</p><hr class="sig"><a href="http://powershelltools.com/">PowerShell Help Reader</a> is an extensible viewer for Windows PowerShell Help System.Mon, 15 Jun 2009 08:13:52 Z2009-06-15T08:13:52Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#b991f3b7-6e42-45a3-a1ce-7c766568f57fhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#b991f3b7-6e42-45a3-a1ce-7c766568f57fbarkingdoghttp://social.technet.microsoft.com/Profile/en-US/?user=barkingdogPowershell and "sp_who"Alexey,<br/><br/>I believe your script will run &quot;sp_who2&quot; but not present the output as an object that I can pass through the PowerShell pipeline and apply cmdlets to.<br/><br/>I was hoping there is a way of taking sp_who2 output, from PowerShell,  presented as an object.<br/><br/><br/><br/><br/>Barkingdog<br/>Mon, 15 Jun 2009 18:28:18 Z2009-06-15T18:28:18Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#4dad96c3-050c-4d87-ada2-49625bf3e24ahttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#4dad96c3-050c-4d87-ada2-49625bf3e24aMarco Shawhttp://social.technet.microsoft.com/Profile/en-US/?user=Marco%20ShawPowershell and "sp_who"*Everything* that PowerShell emits is an object.  I may try to bring up a SQL 2008 VM tomorrow to &quot;see&quot; what the issue might be.<br/>Mon, 15 Jun 2009 19:14:51 Z2009-06-15T19:14:51Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#ec218e9f-42fc-40c7-8c75-5d465605d1e3http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#ec218e9f-42fc-40c7-8c75-5d465605d1e3Marco Shawhttp://social.technet.microsoft.com/Profile/en-US/?user=Marco%20ShawPowershell and "sp_who"<blockquote>Alexey,<br/> <br/> I believe your script will run &quot;sp_who2&quot; but not present the output as an object that I can pass through the PowerShell pipeline and apply cmdlets to.<br/> <br/> I was hoping there is a way of taking sp_who2 output, from PowerShell,  presented as an object.<br/> <br/> <br/> <br/> <br/> Barkingdog<br/></blockquote> <br/> OK, I ran invoke-sqlcmd 'sp_who' and see objects of type system.data.datarow being returned.<br/> <br/> Can you be more specific about what you need to do from here?<br/> <br/> Now, I may see what you're trying to get at...<br/> <br/> I tried this:<br/> SQLPS&gt;invoke-sqlcmd 'sp_who'|where{$_.blk -eq &quot;0&quot;}<br/> <br/> But that doesn't work...<br/>Wed, 17 Jun 2009 15:41:14 Z2009-06-17T15:41:14Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#d1d90431-7b99-4b38-8508-661d838a0162http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#d1d90431-7b99-4b38-8508-661d838a0162Marco Shawhttp://social.technet.microsoft.com/Profile/en-US/?user=Marco%20ShawPowershell and "sp_who"Actually, this works though:<br/> <br/> SQLPS&gt;invoke-sqlcmd 'sp_who'|wehre{$_.dbname -eq &quot;my_name&quot;}<br/> <br/> So there's something odd with some of the properties...<br/>Wed, 17 Jun 2009 15:50:13 Z2009-06-17T15:50:13Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#e62adeaa-7432-49cf-8ce5-67778fb1782ahttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#e62adeaa-7432-49cf-8ce5-67778fb1782aMarco Shawhttp://social.technet.microsoft.com/Profile/en-US/?user=Marco%20ShawPowershell and "sp_who"A little typo there.  Assuming you just needed to sort by dbname...<br/> <br/> This works for me:<br/> <br/> PS&gt;invoke-sqlcmd 'sp_who'|sort dbnameTue, 23 Jun 2009 15:05:19 Z2009-06-23T15:05:19Zhttp://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#550845b2-ebd7-49cb-920b-49092cb93b25http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/903bcebf-7c78-439c-997e-ae96bda00b3f#550845b2-ebd7-49cb-920b-49092cb93b25barkingdoghttp://social.technet.microsoft.com/Profile/en-US/?user=barkingdogPowershell and "sp_who"Marco,<br/><br/>Thanks for the great answers! You discerned exactly what I wanted to do but had no idea how to do iit! <br/><br/>BarkingdogWed, 24 Jun 2009 03:04:50 Z2009-06-24T03:04:50Z