질문하기질문하기
 

답변됨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일 금요일 오후 5: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일 월요일 오후 6: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일 월요일 오후 7: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일 수요일 오후 3: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일 수요일 오후 3: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일 화요일 오후 3: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