简体   繁体   English

我创建的批处理文件出现问题。 它可以正确添加用户,但不会删除

[英]Issue with a batch file I created. It adds a user correctly but won't remove

I have 40 .txt files that are named after 40 of my users computer and each .txt file contains a users AD account. 我有40个.txt文件,它们以40个用户计算机命名,每个.txt文件都包含一个用户AD帐户。 The following bat script works great at injecting the ad account to the administrator group. 以下bat脚本非常适合将广告帐户注入管理员组。

mkdir "c:\newtemp\a_accounts"
xcopy "\\SERVERNAME\FOLDER\a_accounts\%computername%.txt" "c:\newtemp\a_accounts"
for /f "usebackq" %%X in ("c:\newtest\a_accounts\delete\%computername%.txt") do (NET LOCALGROUP ADMINISTRATORS %%X /add)

However, if I make a new bat file and reverse the switch from /add to /delete It doesn't work on the new script. 但是,如果我制作了一个新的bat文件,并将切换从/ add反转到/ delete,则在新脚本上不起作用。 I have to use the script above to be able to delete other users from the local admin group. 我必须使用上面的脚本才能从本地管理组中删除其他用户。

mkdir "c:\newtemp\delete_user_admin"
xcopy "\\SERVERNAME\FOLDER\delete_user_admin\%computername%.txt" "c:\newtemp\delete_user_admin"
For /F "usebackq" %%G IN ("c:\newtemp\delete_user_admin\%computername%.txt") DO (NET LOCALGROUP ADMINISTRATORS %%G /delete)

When the for /f runs it doesn't do anything (no errors neither. It just shows the line). 当for / f运行时,它什么也不做(也没有错误。它只显示该行)。 Which has me stunned as to why it is behaving this way. 我为何对此如此感到震惊。 I tried changing the variable from X to another letter but this does nothing different. 我尝试将变量从X更改为另一个字母,但这没有什么不同。 Any clue why this is happening? 任何线索为什么会这样?

Okay so it looks like the variable is not picking up the user name in the file. 好的,因此看起来该变量未选择文件中的用户名。 I just replaced the second %%G with my ad username and ran the script and it deleted my account from the local admin. 我只是用广告用户名替换了第二个%% G并运行了脚本,该脚本从本地管理员中删除了我的帐户。 Now gotta figure out why the for /f is not working. 现在要弄清楚为什么for / f无法正常工作。 Looking at the website I am using the correct lines. 在网站上,我使用的是正确的行。

7/17/2016 2016年7月17日

Yeah I am completely stomp why this isn't working. 是的,我完全脚为什么这不起作用。 Ran both lines together in a .bat and the following happens. 将两行同时放在.bat中,然后发生以下情况。

This works fine. 这很好。

for /f "usebackq" %%X in ("c:\newtemp\a_accounts\%computername%.txt") do (NET LOCALGROUP ADMINISTRATORS %%X /add)

However, if I change the folder it breaks (no errors just does nothing but look like it runs the line but produces nothing. 但是,如果我更改了文件夹,它就会损坏(没有错误,它只会执行任何操作,但不会产生任何结果,只会执行任何操作。

for /f "usebackq" %%X in ("c:\newtemp\a_accounts2\%computername%.txt") do (NET LOCALGROUP ADMINISTRATORS %%X /delete)

If I go into the .bat file and edit the second line to point to the first folder "a_accounts" and flip the /add to /delete the line works fine!!! 如果我进入.bat文件并编辑第二行以指向第一个文件夹“ a_accounts”,然后将/ add翻转到/ delete,则该行可以正常工作!!! Makes no sense what's so ever. 毫无意义。 I have lost all hope of being able to understand why this isn't working as it should unless someone has the answer. 我完全无法理解为什么除非有人知道答案,否则为什么它不会按预期的那样工作。

You copy to c:\\newtemp\\delete_user_admin\\%computername% yet you read from "c:\\newtemp\\delete_user_admin\\%computername%.txt" 您复制到c:\\newtemp\\delete_user_admin\\%computername%但仍从"c:\\newtemp\\delete_user_admin\\%computername%.txt"读取

These are not the same. 这些不一样。

Weirdist stuff I seen in my life. 我一生中看到的怪异事物。 If I copy the txt file from the working script and modify the name the second line works!!! 如果我从工作脚本中复制txt文件并修改名称,则第二行有效!!! WHAT!!!!?? 什么!!!!?? Makes no sense but I got it to work the way I want. 没有任何意义,但我可以按照自己的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM