the way I get this done is:
SELECT @@SERVERNAME
----myserver
DECLARE @NTLogin nvarchar(128) ;
SET @NTLogin = 'mydomain\myuser'
DECLARE @UserList TABLE (
[Account Name] nvarchar(128)collate Latin1_General_CI_AS,
[Type] nvarchar(128) collate Latin1_General_CI_AS,
[Privilege] nvarchar(128) collate Latin1_General_CI_AS,
[Mapped Login Name] nvarchar(128)collate Latin1_General_CI_AS,
[Permission Path] nvarchar(128) )
INSERT INTO @UserList EXEC master.dbo.xp_logininfo @NTLogin, 'all' --insert group information
IF EXISTS (SELECT * FROM @UserList WHERE [Type] = 'group') --only if it's a group
INSERT INTO @UserList EXEC master.dbo.xp_logininfo @NTLogin, 'members' --insert member information
SELECT [Server Name] = @@SERVERNAME,
[Account Name],
[Type],
[Privilege],
[Mapped Login Name],
[Permission Path]
FROM @UserList
you can wrap the code inside a stored procedure with @ntLogin
as a parameter, that is how I did it here, including the error handling stuff, which is important too.
Another thing is to install the active directory module for windows powershell
also for testing from inside sql server you can run the following script:
DECLARE @User VARCHAR(20)
SELECT @USER = SUBSTRING(SUSER_SNAME(), CHARINDEX('\', SUSER_SNAME()) + 1, LEN(SUSER_SNAME()))
SELECT @USER
, SUSER_SNAME()
,SYSTEM_USER
, USER_NAME()
, CURRENT_USER
, ORIGINAL_LOGIN()
,USER
,SESSION_USER
this will show your credentials
and if you execute as the login you are testing you can see how it looks like.
EXECUTE AS LOGIN='yourdomain\the_long_login'
DECLARE @User VARCHAR(20)
SELECT @USER = SUBSTRING(SUSER_SNAME(), CHARINDEX('\', SUSER_SNAME()) + 1, LEN(SUSER_SNAME()))
SELECT @USER
, SUSER_SNAME()
,SYSTEM_USER
, USER_NAME()
, CURRENT_USER
, ORIGINAL_LOGIN()
, USER
,SESSION_USER
REVERT
Get-ADGroupMember
. I have never seen an AD group name with Over30Chars !