Pour les professionnels de l’informatique >
Forums - Accueil
>
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
- ModifiéVamcSAdministrateurvendredi 26 juin 2009 17:37Fixing thread title bug
Réponses
- A little typo there. Assuming you just needed to sort by dbname...
This works for me:
PS>invoke-sqlcmd 'sp_who'|sort dbname- Marqué comme réponseMarco ShawMVP, Modérateurmardi 23 juin 2009 15:05
Toutes les réponses
cd SqlServerProviderSnapin100\SqlServer::SQLSERVER:\SQL\<instance name>
Invoke-Sqlcmd 'sp_who'
PowerShell Help Reader is an extensible viewer for Windows PowerShell Help System.- Non proposé comme réponseMarco ShawMVP, Modérateurmardi 23 juin 2009 14:18
- Proposé comme réponseAlexey.Martseniuk lundi 15 juin 2009 08: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- Marqué comme réponseMarco ShawMVP, Modérateurmardi 23 juin 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

