with following code you list all users that are connected to certain DB
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.SMO’) | out-null
$Sql = New-Object (‘Microsoft.SqlServer.Management.Smo.Server’) “DEVSQL”
$DB = $Sql.Databases.Item("TestDB")
$DB.Users
Is there a way to get hostnames of the computers that users are connected from ? Something like query below.
select distinct hostname, loginame, db_name(dbid)
from master..sysprocesses
where db_name(dbid) = 'TestDB'