質問する質問する
 

回答済みPowershell and "sp_who"

  • 2009年6月15日 3:50barkingdog ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Is it possible to use PowerShell to simply invoke the t-sql "sp_who" 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
    • 編集済みVamcS管理者2009年6月26日 17:37Fixing thread title bug
    •  

回答

すべての返信

  • 2009年6月15日 8:13Alexey.Martseniuk ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     

    cd SqlServerProviderSnapin100\SqlServer::SQLSERVER:\SQL\<instance name>
    Invoke-Sqlcmd 'sp_who'


    PowerShell Help Reader is an extensible viewer for Windows PowerShell Help System.
  • 2009年6月15日 18:28barkingdog ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Alexey,

    I believe your script will run "sp_who2" but not present the output as an object that I can pass through the PowerShell pipeline and apply cmdlets to.

    I was hoping there is a way of taking sp_who2 output, from PowerShell,  presented as an object.




    Barkingdog
  • 2009年6月15日 19:14Marco ShawMVP, モデレータユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    *Everything* that PowerShell emits is an object.  I may try to bring up a SQL 2008 VM tomorrow to "see" what the issue might be.
  • 2009年6月17日 15:41Marco ShawMVP, モデレータユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Alexey,

    I believe your script will run "sp_who2" but not present the output as an object that I can pass through the PowerShell pipeline and apply cmdlets to.

    I was hoping there is a way of taking sp_who2 output, from PowerShell,  presented as an object.




    Barkingdog

    OK, I ran invoke-sqlcmd 'sp_who' and see objects of type system.data.datarow being returned.

    Can you be more specific about what you need to do from here?

    Now, I may see what you're trying to get at...

    I tried this:
    SQLPS>invoke-sqlcmd 'sp_who'|where{$_.blk -eq "0"}

    But that doesn't work...
  • 2009年6月17日 15:50Marco ShawMVP, モデレータユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Actually, this works though:

    SQLPS>invoke-sqlcmd 'sp_who'|wehre{$_.dbname -eq "my_name"}

    So there's something odd with some of the properties...
  • 2009年6月23日 15:05Marco ShawMVP, モデレータユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済み
    A little typo there.  Assuming you just needed to sort by dbname...

    This works for me:

    PS>invoke-sqlcmd 'sp_who'|sort dbname
  • 2009年6月24日 3:04barkingdog ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Marco,

    Thanks for the great answers! You discerned exactly what I wanted to do but had no idea how to do iit!

    Barkingdog