从WMIC查询中排除一些输出

从WMIC查询中排除一些输出

问题描述:

我通过wmic得到了Windows用户及其本地路径的列表(感谢Compo).

I'm getting the list of Windows users and their local path, through wmic (thanks Compo).

我希望从此wmic命令的输出中排除一些用户名:

I would like some user names to be excluded from the output in this wmic command :

@For /F "Skip=1Tokens=1,2" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount Where^
 "LocalAccount='True' And Not Name Like '[_]%%'" Get Name^,SID 2^>Nul'
)Do @For /F %%I In ("%%H")Do @For /F "Tokens=2Delims==" %%J In ('
 %__AppDir__%wbem\WMIC.exe Path Win32_UserProfile Where^
 "SID='%%I' And Special!='True'" Get LocalPath /Value 2^>Nul'
)Do @For /F "Tokens=*" %%K In ("%%J")Do @Echo User name:"%%G",Profile path:"%%K"

我不确定如何添加此排除文件:

I'm not sure how I can add this exclusion file :

%__AppDir__%findstr.exe /V /X / L/ /I G:"usernames.txt"

你能帮我吗?

谢谢

如果您打算从输出中排除名称,那么效率的一般规则是尽快在代码中过滤命令.

If your intention is to exclude names from the output, the general rule for efficiency, is to filter your commands as soon as possible in your code.

因此,最有效的方法是在 Where 子句中进行单个排除.我在评论中提供了一个有关如何执行此操作的示例,例如将当前的排除项(以下划线开头的名称)(而不是像'[_] %%')更改为 And Name!='Dimitri'And Name!='Kalinda'And Name!='Peter'.

For this reason, the most efficient method would be to make the individual exclusions within the Where clause. I provided an example of how to do that in my comment, e.g. change the current exclusion, of names beginning with an underscore, (And Not Name Like '[_]%%'), to And Name!='Dimitri' And Name!='Kalinda' And Name!='Peter'.

如果文件中每行有一个排除项列表,并且有太多内容无法转移到 Where 子句中,那么您应该在 Do 中执行该过滤初始 For 循环的code>部分.此时,您可以将 findstr.exe 与您选择的选项(固定)一起使用.

If you have a list of exclusions one per line in a file, and there are too many to transfer into the Where clause, then you should perform that filtering in the Do portion of that initial For loop. You could at that point use findstr.exe with the options you chose, (just fixed).

由于您从我的原始答案中选择的代码不是很可靠的代码,它可以满足带有空格/问题字符的用户名,建议您也改成那个.

As the code you chose from my original answer was not the robust one, which caters for user names with spaces/problematic characters, I'd suggest you change to that too.

因此,这是我建议的答案,(不包括使用 findstr.exe usernames.txt 中的名称):>

For that reason, this would be my suggested answer, (excluding names within usernames.txt using findstr.exe):

@For /F Tokens^=4Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
 UserAccount Where "LocalAccount='TRUE'" Assoc:List /ResultRole:SID 2^>NUL'
)Do @Set /P "=%%G"<NUL|%SystemRoot%\System32\findstr.exe /XVLIG:"usernames.txt"^
 >NUL&&(For /F "Tokens=1*Delims==" %%H In ('%SystemRoot%\System32\wbem\WMIC.exe
     UserAccount Where "Name='%%G'" Get SID /Value 2^>NUL
     ^|%SystemRoot%\System32\find.exe "="')Do @For %%J In (%%I
    )Do @For /F "Tokens=1*Delims==" %%K In ('%SystemRoot%\System32\wbem\WMIC.exe
     Path Win32_UserProfile Where (SID^="%%J" And Special!^="TRUE" And LocalPath
     Is Not Null^) Get LocalPath /Value 2^>NUL^|%SystemRoot%\System32\find.exe
     "="')Do @For /F Tokens^=* %%M In ("%%L"
     )Do @Echo UserName:"%%G", UserProfile:"%%M")