Avatar of SAM IT
SAM IT
 asked on

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

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"}

}
Scripting LanguagesPowershellSecurityActive Directory

Avatar of undefined
Last Comment
Pber

8/22/2022 - Mon
SOLUTION
Jason Crawford

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jason Crawford

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 Dent

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 Crawford

Crap
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
arnold

$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 IT

ASKER
@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 Crawford

Yes absolutely a UNC path works just fine.  I just like using environment variables to avoid non-existent paths.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SAM IT

ASKER
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 IT

ASKER
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
Pber

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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23