TechNet - Только для профессионалов. Специально для Вас. >
Форумы
>
Windows PowerShell
>
Powershell and "sp_who"
Powershell and "sp_who"
- 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Администратор26 июня 2009 г. 17:37Fixing thread title bug
Ответы
- A little typo there. Assuming you just needed to sort by dbname...
This works for me:
PS>invoke-sqlcmd 'sp_who'|sort dbname- Помечено в качестве ответаMarco ShawMVP, Модератор23 июня 2009 г. 15:05
Все ответы
cd SqlServerProviderSnapin100\SqlServer::SQLSERVER:\SQL\<instance name>
Invoke-Sqlcmd 'sp_who'
PowerShell Help Reader is an extensible viewer for Windows PowerShell Help System.- Отменено предложение в качестве ответаMarco ShawMVP, Модератор23 июня 2009 г. 14:18
- Предложено в качестве ответаAlexey.Martseniuk 15 июня 2009 г. 8:13
- 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 - *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.
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...- Actually, this works though:
SQLPS>invoke-sqlcmd 'sp_who'|wehre{$_.dbname -eq "my_name"}
So there's something odd with some of the properties... - A little typo there. Assuming you just needed to sort by dbname...
This works for me:
PS>invoke-sqlcmd 'sp_who'|sort dbname- Помечено в качестве ответаMarco ShawMVP, Модератор23 июня 2009 г. 15:05
- Marco,
Thanks for the great answers! You discerned exactly what I wanted to do but had no idea how to do iit!
Barkingdog

