We help IT Professionals succeed at work.

create multiple folders with security group permission powershell-  similar script mentioned below . Want to know that where to mention destination path to create folders

291 Views
Last Modified: 2018-02-06
Requirement is script has to create multiple folders in shared path and has to set the security group permission for created folders using powershell. Want to know that where to mention destination path to create folders in the below script and where to mention .csv to import the folder names .

please assist

--------------------------------------------------

$csvFile = "D:\file.csv"

$create = Import-CSV $csvFile

function DoPermissions
{
    param( $permissionGroup, $folder, $level)
    $toAdd = $permissionGroup -split ";"
    Write-Host $folder
    foreach ($item in $toAdd)
    {
        $acl = (Get-Item $folder).GetAccessControl('Access')
        $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($item, $level, 'ContainerInherit,ObjectInherit','None','Allow')
        $acl.SetAccessRule($ar)
        Set-ACL -path $folder -AclObject $acl
    }
}

foreach ($folder in $create)
{
    $fullPath = $folder.folder #$path + $folder.folder
    if (!(Test-Path $fullPath)) {
    New-Item -ItemType Directory -Path $fullPath
    $fAcl = Get-Acl -Path $fullPath
    $fAcl.SetAccessRuleProtection($true, $true)
    Set-Acl -Path $fullPath -AclObject $fAcl
    }

    if ($folder.full_control) {DoPermissions $folder.full_control $fullPath "FullControl"}
    if ($folder.modify) {DoPermissions $folder.modify $fullPath "Modify"}
    if ($folder.read_execute) {DoPermissions $folder.read_execute $fullPath "ExecuteFile"}
    if ($folder.list_folder_content) {DoPermissions $folder.list_folder_content $fullPath "ListDirectory"}
    if ($folder.read) {DoPermissions $folder.read $fullPath "Read"}
    if ($folder.write) {DoPermissions $folder.write $fullPath "Write"}

}
Comment
Watch Question

Jason CrawfordTransport Ninja
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Jason CrawfordTransport Ninja
CERTIFIED EXPERT

Commented:
Wait the only place I deviated from the verb-noun format was in the transcript path...right?  I yield to you everytime I've seen your blog, but I'm sticking with what I got this time.
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
You updated the command name DoPermissions -> Do-Permissions. Which would have been fine, but you've left calls to the function using the old name in the if statement block. 'tis easy enough to fix, but a gotcha.
Jason CrawfordTransport Ninja
CERTIFIED EXPERT

Commented:
Crap
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
$create gets the contents of the csv file

folder is included in the csv file.

The place where you obtained this script has to include a sample of the .csv file.

Folder,full_control,modify,read_execute,list_folder,read,write
C:\somefolder,0,0,1,1,1,0
C:\another,1,0,0,0,0,0


At least I think I read the things...
SAM ITAD windows Admin  

Author

Commented:
@jason:

instead of $ent: USERPROFILE  can I mentioned the path in this format I.e '\\servername\projectfolders\newprojects'
____________________________________________________

Start-Transcript -Path "$env:USERPROFILE\Desktop\DoPermisssionsTranscript_$date" -Force
Jason CrawfordTransport Ninja
CERTIFIED EXPERT

Commented:
Yes absolutely a UNC path works just fine.  I just like using environment variables to avoid non-existent paths.
SAM ITAD windows Admin  

Author

Commented:
I need to add a security group i.e. "shr-projects_new"  for each folder created using this script. can i know where i need to add the security group with modify permissions
SAM ITAD windows Admin  

Author

Commented:
I need to add a security group i.e. "shr-projects_new"  for each folder created using this script. can i know where i need to add the security group with modify permissions
PberSolutions Architect
CERTIFIED EXPERT

Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- Chris Dent (https:#a42066865)
-- Jason Crawford (https:#a42066736)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Pber
Experts-Exchange Cleanup Volunteer