如何运行由命令net用户列表中返回每个用户的批处理文件? [英] How to run a batch file for every user returned in list by command net user?

查看:197
本文介绍了如何运行由命令net用户列表中返回每个用户的批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是让通用code它通过所有的用户帐户名称循环在计算机和将输出发送到一个批处理文件。 网用户命令输出的用户的列表,但我想每个用户名分配到一个参数。所以,我应该使用 FOR / F 命令来做到这一点。这可能看起来很容易,但要记住用户名可以包含在任何地方空间:开头,结尾或中间,此外,它可以找到连续2个空格

提示1:用户名的最大长度为20个字符结果
提示2:循环需要通过错误的用户名进行迭代,因此需要检查它们是否有效。命令网​​络用户的uname%ERRORLEVEL% 0,如果用户名的uname(例如)是有效的。

如果没有可能的方式,也许一个VBScript可以与每个用户帐户名的启动批处理文件。

编辑:我终于做了我自己code,但它需要大约5秒,只有15帐户

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

  SETLOCAL EnableDelayedExpansion
FOR / F跳过= 4 delims =%%我在('网络用户')做(
    设置行= %%我
    如果不是行!〜1,1! ==。为%%我在(0,25,50)做(
        设置名称=行:〜!%%我,20!
        致电:的setName
    )
)设置名称=
:的setName
如果没有!名字! ==(
    网用户!的名字! &功放;&安培; %〜dp0Test.bat!名字!
    如果没有姓名:〜!1,1! ==GOTO:EOF
    设置!名=名称:〜0,-1!
    后藤的setName
)ENDLOCAL


解决方案

下面是一个行PowerShell的命题:

 的foreach(在GET-WmiObject -Class Win32_UserAccount  - 命名空间ROOT \\ CIMV2-Filter $帐户LocalAccount ='$真'-ComputerName。-ErrorAction停止){的MyScript。蝙蝠$ account.Name}

您可以在一个名为.ps1文件存储和的PowerShell myfile.ps1执行

如果你需要的不仅仅是本地帐户越多,你将不得不与 -Filter 参数一塌糊涂。
此外,有空格的帐户名称将与周围的报价进行传递,你需要应付被调用批处理脚本。

My goal is to make a universal code which loops through all user account names in a computer and sends the output to a batch file. The "net user" command outputs a list of users but I want to assign each user name to an argument. So I should use the for /f command to do it. This may look easy, but remember user names may include spaces at any place: at the beginning, at the end or in the middle, Also, it's possible to find 2 consecutive spaces

Hint 1: The maximum length of a user name is 20 characters
Hint 2: The loops needs to iterate through wrong user names so it needs to check if they're valid. The command net user "uName" sets %ErrorLevel% to 0 if the user name "uName" (for example) is valid.

If there's no way possible, maybe a VBScript could start the batch file with each of the user account names.

Edit: I finally made my own code, but it takes about 5 seconds for only 15 accounts.

I would like a better solution, maybe with another programming language.

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

解决方案

Here is a one line powershell proposition :

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

you can store in a .ps1 file and execute with powershell myfile.ps1

If you need more than just local account you will have to mess with the -Filter parameter. Also, any account name with spaces will be passed with surrounding quotes, you need to cope with in the callee batch script.

这篇关于如何运行由命令net用户列表中返回每个用户的批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆