-- CAUTION: This method will not work for NT account names that can 
--          have spaces in them.

DECLARE @CmdStr varchar( 8000 );
-- Get accounts on the SQL Server m/c
SELECT @CmdStr = 'for /F "skip=4 tokens=1,2,3" %a in '+
                 '(''net users^|findstr /V /C:"completed"'') ' +
                 'do @if not (%a)==() echo %a&if not (%b)==() echo %b&' +
                 'if not (%c)==() echo %c';
/*
        The command string below can be used for getting accounts from
        the NT domain. The output format is different for NET USERS on a
        local machine & the domain. The main parameter to change is the
        number of lines to skip initially.
*/
/*
SELECT @CmdStr = 'for /F "skip=7 tokens=1,2,3" %a in '+
                 '(''net users /domain^|findstr /V /C:"completed"'') ' +
                 'do @if not (%a)==() echo %a&if not (%b)==() echo %b&' +
                 'if not (%c)==() echo %c';
*/
EXECUTE master..xp_cmdshell @CmdStr;
/*
output
---------------------
Administrator
Guest
IUSR_VALHALLA-MDD06H
IWAM_VALHALLA-MDD06H
Service Account
TsInternetUser
*/
This page was last updated on May 01, 2006 04:28 PM.