如何通过命令net user为列表中返回的每个用户运行批处理文件?

时间:2014-11-14 17:29:43

标签: batch-file vbscript

我的目标是创建一个循环遍历计算机中所有用户帐户名的通用代码,并将输出发送到批处理文件。 "网络用户"命令输出用户列表,但我想将每个用户名分配给一个参数。所以我应该使用for /f命令来做到这一点。这可能看起来很简单,但请记住用户名可能包含任何地方的空格:开头,结尾或中间,此外,它可以找到2个连续的空格

提示1:用户名的最大长度为20个字符
提示2:循环需要遍历错误的用户名,因此需要检查它们是否有效。如果用户名" uName"则命令net user "uName"%ErrorLevel%设置为0 (例如)有效。

如果没有办法,可能VBScript可以用每个用户帐户名启动批处理文件。

编辑:我终于制作了自己的代码,但只有15个帐户大约需要5秒钟。

我想要一个更好的解决方案,也许还有另一种编程语言。

setlocal EnableDelayedExpansion
For /f "skip=4 delims=" %%I in ('net user') do (
    Set "Line=%%I"
    If not "!Line:~-1,1!" == "." for %%I in (0,25,50) do (
        Set "Name=!Line:~%%I,20!"
        Call :setName
    )
)

Set Name=
:setName
If not "!Name!" == "" (
    Net user "!Name!" && %~dp0Test.bat "!Name!"
    If not "!Name:~-1,1!" == " " goto:eof
    Set "Name=!Name:~0,-1!"
    Goto setName
)

endlocal

2 个答案:

答案 0 :(得分:0)

在名称的开头或结尾有空格的用户帐户名称根据我的测试不可能。空格只能位于用户帐户名称中的非空白字符之间。

这是我的评论批量解决方案,也不是很快。

@echo off
rem Process output of command 'net user' line by line with skipping
rem the first 4 lines and stopping processing when line with success
rem message is reached which of course depends on language of Windows.
for /F "usebackq skip=4 delims=" %%L in (`%SystemRoot%\System32\net.exe user`) do (
    if "%%L"=="The command was successfully executed." goto :EOF
    call :ProcessAccounts "%%L"
)
goto :EOF

rem Subrountine to process a line with user account names.
:ProcessAccounts
rem Get the line without the double quotes.
set "Line=%~1"

:NextUser
rem Get first 20 characters from line and next remove those 20 characters.
set "Name=%Line:~0,20%"
set "Line=%Line:~20%"

rem Remove all spaces at end of current user account name.
:TrimRight
if "%Name:~-1%"==" " (
    set "Name=%Name:~0,-1%"
    goto TrimRight
)

rem Check if this user account name is valid.
%SystemRoot%\System32\net.exe user "%Name%" 1>nul 2>nul
if not errorlevel 1 (
    echo Valid user account name is: %Name%
    rem Do here what should be done with this user account name.
)

rem Remove leading spaces from remaining line.
:TrimLeft
if "%Line:~0,1%"==" " (
    set "Line=%Line:~1%"
    goto TrimLeft
)

rem Is the shortened line now completely processed?
if not "%Line%"=="" goto NextUser

rem Exit the subroutine ProcessAccounts.
goto :EOF

对于左右修剪空间可能有更好的解决方案,请参阅How to remove trailing and leading whitespace for user-provided input in a batch file?

此批处理文件执行速度较慢的原因是:

%SystemRoot%\System32\net.exe user "%Name%" 1>nul 2>nul
if not errorlevel 1 (
    echo Valid user account name is: %Name%
    rem Do here what should be done with this user account name.
)

将此块更改为

echo Valid user account name is: %Name%
rem Do here what should be done with this user account name.

使批处理文件更快,更快。

使用net user "%Name%"验证用户帐户名称会使批处理文件变慢。

答案 1 :(得分:0)

这是一个单行的PowerShell命题:

foreach ($account in Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" -Filter "LocalAccount='$True'" -ComputerName . -ErrorAction Stop) { myscript.bat $account.Name}

您可以存储在.ps1文件中并使用powershell myfile.ps1

执行

如果您需要的不仅仅是本地帐户,则必须使用-Filter参数。 此外,任何带空格的帐户名称都将与周围的引号一起传递,您需要在被调用者批处理脚本中处理。