如何批量输出列表格式的net group命令

如何批量输出列表格式的net group命令

我使用的net group "Groupname" /domain命令以 3 列格式输出组中的一组用户。列之间有空格。

如何输出命令以便每行有一个用户?

答案1

下次询问之前,请先尝试一些事情。

假设该命令的输出大致如下:

bossMan    Arescet    Ice
superuser  lazyWorker Dog 
(etc...)

由于我不确定我写这个文件的输出是什么,所以我不知道输出中是否给出了需要忽略的前缀。

:: First attempt with a single line;
for /f %%G in ('net group "Groupname" /domain') do (echo %%G)

现在我更喜欢按照备份和奇数字符处理的习惯使用文件,所以我会使用它,尽管它只有 3 行并写入文件;

:: Write the output of your command into a file:
net group "Groupname" /domain >userList.temp

:: Split the content by spaces, and write each space separated token.
:: Column 1 is %%~G, 2 is %%~H, 3 is %%~I. 
for /f %%G in (userList.tmp) do (
    echo %%~G
    echo %%~H
    echo %%~I
)

:: Cleanup the temp file;
del "userList.temp"

当然>>output.ext可以单独应用于%%~G %%~H %%~I,或者与当前行的指定列一起调用。

答案2

请使用 tokens=1,2,3 选项

echo off
:: Write the output of your command into a file:
net group "Groupname" /domain >userList.temp

:: Split the content by spaces, and write each space separated token.
:: Column 1 is %%~G, 2 is %%~H, 3 is %%~I. 
for /f "skip=8 tokens=1,2,3" %%G in (userList.temp) do (
    echo %%G
    echo %%H
    echo %%I
)

:: Cleanup the temp file;
del "userList.temp"
pause
echo on

答案3

感谢您为创建此脚本付出的所有艰辛努力,它运行得非常好。
但是我想评估多个组,所以我添加了将组作为参数传递并将输出写入文件的功能。对现有解决方案进行了非常小的调整。

echo off
set GRP_NM=%1
:: Write the output of your command into a file:
net group "%GRP_NM%" /domain >userList.temp

:: Split the content by spaces, and write each space separated token.
:: Column 1 is %%~G, 2 is %%~H, 3 is %%~I. 
echo UserName > %GRP_NM%_userlist.txt
for /f "skip=8 tokens=1,2,3" %%G in (userList.temp) do (
    echo %%~G >> %GRP_NM%_userlist.txt
    echo %%~H >> %GRP_NM%_userlist.txt
    echo %%~I >> %GRP_NM%_userlist.txt

)

:: Cleanup the temp file;
del "userList.temp"
::pause
echo on

答案4

您可以直接使用 FOR 'command' 选项来执行此操作,而无需临时文件。

简单版本在命令行上仅提取给定组中的用户名如下:

@ECHO off
CLS
::Get the group name from the command line
IF not "%1"=="" SET str_Grp-Nm=%1
IF not "%1"=="" ^> Given group name: '%str_Grp-Nm%'.
IF not DEFINED str_Grp-Nm SET str_Grp-Nm=Default_group
::Remove double quotes if any.
SET str_Grp-Nm=%str_Grp-Nm:"=%
::Default DOS-language: EN-US/ENU ^(Engels, USA^)
  SET str_LastLn=The command completed

ECHO User names in group name;%str_Grp-Nm%>"%~dpn0 - %str_Grp-Nm%.csv"
::Split the content in three columns; column 1 is %%~g, 2 is %%~h and 3 is %%~i. 
FOR /f "skip=8 tokens=1,2,3" %%g IN ('NET group "%str_Grp-Nm%" /domain') DO (
  rem Skip the last line 'The command completed successfully.'. Note; this last line is language dependent, in Dutch the last line is: 'De opdracht is voltooid.'.
  IF /i not "%%~g %%~h %%~i"=="%str_LastLn%" (
    ECHO ^> Found username '%%~g':
      ECHO %%~g>>"%~dpn0 - %str_Grp-Nm%.csv"
    IF not '%%~h'=='' ECHO ^> Found username '%%~h':
      IF not '%%~h'=='' ECHO %%~h>>"%~dpn0 - %str_Grp-Nm%.csv"
    IF not '%%~i'=='' ECHO ^> Found username '%%~i':
      IF not '%%~i'=='' ECHO %%~i>>"%~dpn0 - %str_Grp-Nm%.csv"
  ) ELSE (
    ECHO * %%~g %%~h %%~i successfully!
  )
)
IF exist "%~dpn0 - %str_Grp-Nm%.csv" START "This quoted line is needed for %OS%" "%~dpn0 - %str_Grp-Nm%.csv"

::Cleanup the environment...
SET str_Grp-Nm=
SET str_LastLn=

扩展版本,还提取了完整的用户名并检测了 DOS 语言(在我的情况下是荷兰语)。注意:DOS 语言可以与您的操作系统语言不同,尤其是在多语言系统中。

@ECHO off
CLS
::Get the group name from the command line
IF not "%1"=="" SET str_Grp-Nm=%1
IF not "%1"=="" ECHO ^> Given group name: '%str_Grp-Nm%'.
IF not DEFINED str_Grp-Nm SET str_Grp-Nm=Default_group
::Remove double quotes if any.
SET str_Grp-Nm=%str_Grp-Nm:"=%
::Default DOS-language: EN-US/ENU ^(Engels, USA^)
  SET str_Usr-Fn=Full Name
  SET str_Usr-Un=UserName
  SET str_LastLn=The command completed
::Detect DOS-language: NL-NL/NLD ^(Nederlands, Nederland^)
NET user %UserName% /domain | FIND /i "vOltoOid.">NUL &IF not ErrorLevel 1 (
  ECHO ^> DOS-language: NL-NL/NLD ^(Nederlands, Nederland^) detected.
  SET str_Usr-Fn=Volledige naam
  SET str_Usr-Un=Gebruikersnaam
  SET str_LastLn=De opdracht is
)

ECHO * Check for usernames in group: '%str_Grp-Nm%'
ECHO Press any key to continue, Ctrl+C to quit.
  PAUSE > NUL
  ::Or continue afther...   TimeOut /t 33
ECHO Group name;%str_Grp-Nm%>"%~dpn0 - %str_Grp-Nm%.csv"
ECHO %str_Usr-Un%;%str_Usr-Fn%>>"%~dpn0 - %str_Grp-Nm%.csv"
::Split the content in three columns; column 1 is %%~g, 2 is %%~h and 3 is %%~i. 
FOR /f "skip=8 tokens=1,2,3" %%g IN ('NET group "%str_Grp-Nm%" /domain') DO (
  rem Skip the last line 'The command completed successfully.'. Note; this last line is language dependent, in Dutch the last line is: 'De opdracht is voltooid.'.
  IF /i not "%%~g %%~h %%~i"=="%str_LastLn%" (
    ECHO ^> Check '%%~g':
      REM Get the 'Full Name' from the UserName by using: 'NET user %%~g /domain | FIND /i "%str_Usr-Fn%"'. Use double qoutes to get the pipe-action in the FOR-command. Because there is a space in 'Full Name' a search for "name" does not give the correct result (2 ànd 3 columns).
      FOR /f "tokens=2,*" %%u IN ('"NET user %%~g /domain | FIND /i "%str_Usr-Fn%""') DO ECHO %%~g;%%v>>"%~dpn0 - %str_Grp-Nm%.csv"
      FOR /f "tokens=2,*" %%u IN ('"NET user %%~g /domain | FIND /i "%str_Usr-Fn%""') DO ECHO   %str_Usr-Un%; '%%~g'; %str_Usr-Fn%; "%%v".
    IF not '%%~h'=='' ECHO ^> Check '%%~h':
      IF not '%%~h'=='' FOR /f "tokens=2,*" %%u IN ('"NET user %%~h /domain | FIND /i "%str_Usr-Fn%""') DO ECHO %%~h;%%v>>"%~dpn0 - %str_Grp-Nm%.csv"
      IF not '%%~h'=='' FOR /f "tokens=2,*" %%u IN ('"NET user %%~h /domain | FIND /i "%str_Usr-Fn%""') DO ECHO   %str_Usr-Un%; '%%~h'; %str_Usr-Fn%; "%%v".
    IF not '%%~i'=='' ECHO ^> Check '%%~i':
      IF not '%%~i'=='' FOR /f "tokens=2,*" %%u IN ('"NET user %%~i /domain | FIND /i "%str_Usr-Fn%""') DO ECHO %%~i;%%v>>"%~dpn0 - %str_Grp-Nm%.csv"
      IF not '%%~i'=='' FOR /f "tokens=2,*" %%u IN ('"NET user %%~i /domain | FIND /i "%str_Usr-Fn%""') DO ECHO   %str_Usr-Un%; '%%~i'; %str_Usr-Fn%; "%%v".
  ) ELSE (
    ECHO * %%~g %%~h %%~i successfully!
  )
)
IF exist "%~dpn0 - %str_Grp-Nm%.csv" START "This quoted line is needed for %OS%" "%~dpn0 - %str_Grp-Nm%.csv"

::Cleanup the environment...
SET str_Grp-Nm=
SET str_Usr-Fn=
SET str_Usr-Un=
SET str_LastLn=

笔记:您可以从组合中提取更多信息NET 用户寻找,基本上遵循下面的语法。

NET user <username> /domain | FIND /i "<search string>"

而且使用 Visual Basic 还可以实现更多功能。不过那是其他故事了 ;o)

祝你好运!

相关内容