尝试使用net localgroups删除所有管理员帐户(除了2个specyfic)我遇到了大问题。问题是没有AND运算符,所以必须采用一些更难的方法。
for /F "tokens=*" %%G in ('net localgroup administrators') Do (
If %%G == Administrator (goto:ex)
If %%G == MWAdmin (goto:ex)
net localgroup administrators %%G /delete
:ex)
答案 0 :(得分:0)
您的问题就在于:
:ex)
这对批处理没有意义,标签不能在代码块中,因为它会因语法错误而中断块和批处理。
您可以通过call
检查块外的帐户名来解决此问题。在goto :eof
返回call
之后使用call
,因此允许离开并返回到for循环中停止的位置。
for /f "tokens=*" %%G in ('net localgroup administrators') do (
call :checkName "%%~G"
)
:: If name matches if, go back to for loop, else del.
:checkName
if "%~1" == "Administrator" goto :eof
if "%~1" == "MWAdmin" goto :eof
net localgroup administrators "%~1" /delete
goto :eof
以防万一,让我们突出显示:
call :label "%variable%"
使用此功能意味着当它转到:label
时,您可以使用%1
获取%variable%
的传递值,并且可以添加~
使用%~1
删除引号。这对于在for循环之外取值%%X
以便于处理非常有用。
或者在for循环中使用neq
和嵌套neq
方法;
for /f "tokens=*" %%G in ('net localgroup administrators') do (
if "%%~G" neq "Administrator" (
if "%%~G" neq "MWAdmin" (
net localgroup administrators %%G /delete
)
)
)
注意我将%%G
加入%%~G
,并在Administrator
和MWAdmin
附加了引号,这是为了稳定,以防将来的名称导致语法错误。< / p>