msdos script for ad...
 

Subscribe now and choose from over 30 free gifts worth up to £49 - Plus get £25 to spend in our shop

[Closed] msdos script for adding user permissions to "eponymous" folders

18 Posts
9 Users
0 Reactions
67 Views
 D0NK
Posts: 592
Full Member
Topic starter
 

using
[i]for /f %d in (list.txt) do md %d[/i]
I can quickly create personal folders from a list of usernames

is there any way of adding user permissions to their respective folders?
so list contains dave bob & steve the first script will create folders dave bob steve, I need to add permissions for each user to their own respective folder.
Any ideas?

pretty sure I did something similar waaaaaay back but cant remember details and my google-fu is poor today

(file server is domain joined, I have local admin on server, limited rights to domain, all users have domain accounts)


 
Posted : 29/09/2017 12:12 pm
 keir
Posts: 0
Free Member
 

You could probably do something using subinacl


 
Posted : 29/09/2017 12:15 pm
Posts: 0
Full Member
 

try this

for /f %d in (list.txt) do md %d & icacls "%d" /grant MyDomain\%d:(OI)(CI)F

Should give Full Control of the folder created. Obviously change MyDomain to your domain name.

I had to do something similar a while back and this solution worked for me then.


 
Posted : 29/09/2017 12:27 pm
Posts: 77689
Free Member
 

icacls is what you need. The syntax can be a bit impenetrable though.

https://technet.microsoft.com/en-us/library/2009.07.geekofalltrades.aspx

EDIT: curses, beaten to it by 21 seconds...!


 
Posted : 29/09/2017 12:28 pm
Posts: 4660
Full Member
 

Hack this apart, specifically teh bit below wehere it says "EDIT DOMAIN NAME"

'Creates profile folder and permissions as appropriate

Sub onPostCreate (Request)

If (LCase(Request.Class) <> "user") Then Exit Sub

arrbytGuid = request.get("objectGUID")
strHexGuid = OctetToHexStr(arrbytGuid)
strGuid = HexGuidToGuidStr(strHexGuid)

Set objUser = GetObject("EDMS://<GUID=" & strHexGuid & ">")

objUser.getinfo
strUserName = objUser.Get("sAMAccountName")
'EDIT PATH ADN USE FQDN
path = "\\zebsvr2k301\profiles$\" & strUserName
' Creates the Profile folder in the appropriate share.
Set shell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder (path)

'EDIT DOMAIN NAME
cmd = ("%COMSPEC% /c Echo Y| cacls " & path & " /T /E /C /G " & "zebin\" & strUserName & ":C")
Shell.Run cmd

End Sub

Function OctetToHexStr(arrbytOctet)
' Function To convert OctetString (Byte array) To Hex string.

Dim k
OctetToHexStr = ""
For k = 1 To Lenb(arrbytOctet)
OctetToHexStr = OctetToHexStr _
& Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
Next
End Function

Function HexGuidToGuidStr(strGuid)
' Function To convert Hex Guid To display form.
Dim k

HexGuidToGuidStr = ""
For k = 1 To 4
HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 9 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-"
For k = 1 To 2
HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 13 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-"
For k = 1 To 2
HexGuidToGuidStr = HexGuidToGuidStr & Mid(strGuid, 17 - 2*k, 2)
Next
HexGuidToGuidStr = HexGuidToGuidStr & "-" & Mid(strGuid, 17, 4)
HexGuidToGuidStr = HexGuidToGuidStr & "-" & Mid(strGuid, 21)
End Function


 
Posted : 29/09/2017 12:28 pm
Posts: 17851
Full Member
 

In the dim and distant past I've used cacls. Is it icacls now?


 
Posted : 29/09/2017 12:28 pm
Posts: 0
Full Member
 

both are still used.
cacls reports the current rights
icacls applies new rights


 
Posted : 29/09/2017 12:31 pm
Posts: 77689
Free Member
 

As an aside, if you're doing this sort of cmd script hackery on a regular basis, you might want to make a start on learning Powershell.

https://blogs.technet.microsoft.com/heyscriptingguy/2014/11/22/weekend-scripter-use-powershell-to-get-add-and-remove-ntfs-permissions/

I've done a considerable amount of commandline scripting over the years, it's workable but the entire thing is a bunch of hacks to make what is essentially still DOS do things it was never designed to do. Powershell is a bit more wordy but it's terrifically powerful as a system scripting tool.


 
Posted : 29/09/2017 12:33 pm
Posts: 0
Full Member
 

I agree Cougar. Powershell is great.

If you are still on Windows 7 though make sure to upgrade to Powershell V3, it comes with V2 installed but V3 is much better.

Windows 10 has V5 by default


 
Posted : 29/09/2017 12:35 pm
Posts: 4660
Full Member
 

^^what cougar said^^. Especially of you're dealing with data stored on SANs as some refuse to allow you to script into them to create shares or add/edit permissions without using their powershell interfaces.

Also look at some proper tooling. Our ActiveRoles product or my mate's Cayosoft suite do this OOTB. The ARS powershell cmdlets further simplify what you can do.


 
Posted : 29/09/2017 12:37 pm
 D0NK
Posts: 592
Full Member
Topic starter
 

cheers fellas will have a look.

Not a regular thing, colleague who usually does it (manually using GUI on a per folder basis - [i]I think[/i]) is off so it's been given to me. Learning some powershell is probably a good shout genaerally tho.

I've no doubt the "proper" SAN is better setup, this is just a local file server bodged together


 
Posted : 29/09/2017 12:45 pm
Posts: 3735
Free Member
 

This should work, there or there about 🙂

$users = Import-Csv -Delimiter "," -Path C:\temp\list.txt
$FolderRoot = "\\your\folder\root"

Foreach($user in $users)
{
$path = "$FolderRoot\$($user.username)"
$folder = New-Item -ItemType Directory -Path $path
$acl = get-acl $folder
$NewRule = New-Object System.Security.AccessControl.FileSystemAccessRule($($user.username),'FullControl','ContainerInherit,ObjectInherit', 'None', 'Allow')
$acl.SetAccessRule($newrule)
Set-Acl -path $Path -AclObject $Acl
}


 
Posted : 29/09/2017 12:45 pm
 D0NK
Posts: 592
Full Member
Topic starter
 

quick bit of sandbox testing suggests Jake25's script is spot on so kudos for that.

Haven't got the full user list yet anyway but will test a couple more times and then run it monday, fingers crossed 🙂


 
Posted : 29/09/2017 12:57 pm
 D0NK
Posts: 592
Full Member
Topic starter
 

had to edit the script to

[i]for /f %d in (list.txt) do md %d & icacls "%d" /grant MyDomain\%d:(OI)(CI)(RD,WD,AD,REA,WEA,RA,WA,DC,X,RC)[/i]

to match the permissions as per our documentation, but I ran it this morning and it's all looking good. Thanks everyone.


 
Posted : 02/10/2017 9:28 am
Posts: 0
Free Member
 

Just as a wildcard.

Consider MacroExpress.
Its a keyboard emulator ( at its most basic ) & so its works across everything


 
Posted : 02/10/2017 9:36 am
 D0NK
Posts: 592
Full Member
Topic starter
 

spoke too soon. Tested with my account and it worked fine - but I have extra rights - tested with a user account: no access, set user to full access all fine. Seems my permissions edit is flawed.
back to testing.


 
Posted : 02/10/2017 11:21 am
Posts: 77689
Free Member
 

Runas any use to you?


 
Posted : 02/10/2017 11:28 am
Posts: 901
Full Member
 

Is this for Home Folders or Profile type folders? I've got a script I've used to tidy these up / set ownership / etc but it does rely on the root folder having the permissions set up correctly. i.e.

Domain Users - [i]Traverse folder ... + List folder ... + Read attributes + Create folders ... + Read permissions [/i]- [b]This folder only[/b]

Edit: as per https://blogs.technet.microsoft.com/askds/2008/06/30/automatic-creation-of-user-folders-for-home-roaming-profile-and-redirected-folders/


 
Posted : 02/10/2017 11:40 am
 D0NK
Posts: 592
Full Member
Topic starter
 

permissions are a bit of a mess, it's fed in part by our own user account system that was written in house. Documentation not great. Just tried another test user and having different issues with that.
Guessing it's an issue to do with our systems own idiosyncrasies
I shall battle on

cheers for the tips tho


 
Posted : 02/10/2017 12:03 pm