1

I'm trying to create a home directories with permissions from a text file. I can only get the batch file to run the first line. Can anyone tell me why?

I initiate the scripts by running go.bat as administrator.

go.bat

@echo
for /f %%a in (users1.txt) do call test.bat %%a

test.bat

@echo off
m:
cd \
mkdir %1
icacls %1 /grant %1:(OI)(CI)M 

cd %1

mkdir public
icacls public /inheritance:d
icacls public / All:(OI)(CI)(RD)
icacls public /grant All:(OI)(CI)R

mkdir private
icacls private /inheritance:d
icacls private /remove All

cd \

users1.txt

user1
user2
user3
Zypher
  • 37,405
  • 5
  • 53
  • 95
Steven
  • 63
  • 1
  • 6

1 Answers1

3

You could use your familiar icacls commands with PowerShell looping as an "introduction" to PowerShell.

Something like this should do it

Get-Content C:\users.txt | ForEach-Object {
    $User = $_
    $WorkingPath = "M:\" + "$User"
    mkdir $WorkingPath

    icacls $WorkingPath /grant $User:(OI)(CI)M 

    $Public = "$WorkingPath" + "public"
    mkdir $Public
    icacls $Public /inheritance:d
    icacls $Public / All:(OI)(CI)(RD)
    icacls $Public /grant All:(OI)(CI)R

    $Private = "$WorkingPath" + "private"
    mkdir $Private
    icacls $Private /inheritance:d
    icacls $Private /remove All
}

There's Get-ACL and Set-ACL for working with permissions in PowerShell, as well as replacements for mkdir and similar commands, but this way you can ease into using it.

MDMarra
  • 100,734
  • 32
  • 197
  • 329
  • I wish I could give more than +1. Good answer, good advice of "start using PowerShell", good advice of "ease into it slowly". Steven - get into PowerShell as quick as you can. It is about a million times more powerful as batch files and only about 50% harder to learn. – Greenstone Walker Jul 03 '13 at 02:45
  • Looks interesting, thanks for your help. I will try this out! – Steven Jul 03 '13 at 16:47
  • Note that I didn't actually run the commands, I took the icacls statements from your script. But it's a good outline – MDMarra Jul 03 '13 at 16:50
  • Yeah, I tried to run it as is and the results weren't as expected. That's okay though, this will be a good first step to learning. – Steven Jul 03 '13 at 17:12