Reputation:
I recently used this command to fix an issues on one of my servers;
c:\windows\system32\icacls \\"server_name"\C$\Windows\System32\Tasks /grant "everyone":(OI)(CI)F
server list.txt
server1
server2
server3
What I would like to do is to script this to use a list of computers to replace "server_name" with the name from the server list and run through each until it has done so from all servers.
Right now I'm just running all of them directly as such below;
c:\windows\system32\icacls \\server1\C$\Windows\System32\Tasks /grant "everyone":(OI)(CI)F
c:\windows\system32\icacls \\server2\C$\Windows\System32\Tasks /grant "everyone":(OI)(CI)F
c:\windows\system32\icacls \\server3\C$\Windows\System32\Tasks /grant "everyone":(OI)(CI)F
Any ideas?
Upvotes: 1
Views: 257
Reputation: 1271
You can do this using the FOR
command as such:
FOR /F "usebackq" %%a IN ("server list.txt") DO c:\windows\system32\icacls \\%%a\C$\Windows\System32\Tasks /grant "everyone":(OI)(CI)F
In this case we use the usebackq
option because your filename has spaces.
Alternatively, you could simply use a space delimited list:
FOR %%a IN ("server1" "server2" "server3") DO c:\windows\system32\icacls \\%%a\C$\Windows\System32\Tasks /grant "everyone":(OI)(CI)F
Both of these options assume you will be using a batch file. If you are simply running the command in the command prompt, use %a
instead of %%a
.
If you will expand this to add more commands in the FOR
loop using parentheses, you will also need to escape the parentheses in your icacls
command.
Further reading:
Upvotes: 1