[使用WMIC获取用户配置文件列表时出现的问题

问题描述 投票:0回答:1

我正在使用以下批处理命令来检索所有本地用户配置文件(也包括域用户):

for /f "delims=" %%I in ('dir /a:d-h /b "%SystemDrive%\Users\*" 2^>nul ^| %SystemRoot%\System32\findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"') do (

问题是此命令有其局限性:它并没有真正检查所涉及的用户是否确实具有帐户。

用户Compo为我提供了一种使用WMIC检索配置文件名称的方法。

所以我最终编写了以下命令:

@For /F "tokens=* skip=1" %%I In ('%__AppDir__%wbem\WMIC.exe UserAccount Get Name ^|%__AppDir__%findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"') do (

问题是:它忽略了我的排除文件(每行包含一个用户),并且结束了一个没有任何名称的配置文件。

任何想法我如何解决这些问题?

windows for-loop batch-file wmic
1个回答
0
投票
@echo off
setlocal

set "bin=%~dp0"

For /F "tokens=* skip=1" %%I In ('
    %__AppDir__%wbem\WMIC.exe UserAccount where Disabled^="FALSE" get Name ^|
    %__AppDir__%WindowsPowerShell\v1.0\powershell -noprofile -command "$input.trim()" ^|
    %__AppDir__%findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"
') do echo "%%~I"

wmic输出通过管道传输到powershell进行修剪,然后通过管道传输到findstr

wmic命令将通过使用where子句排除禁用的帐户。

根据需要更改bin的设置。

© www.soinside.com 2019 - 2024. All rights reserved.