Creation of sub folders with Parent folders in one handy

Welcome Forums General PowerShell Q&A Creation of sub folders with Parent folders in one handy

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
1 year, 6 months ago.

  • Author
    Posts
  • #69265

    Participant
    Points: 0
    Rank: Member

    Hello All,

    I have tested the below script. The output is almost we reached the expectation. It will create the Parent folders which are mentioned in CSV and creates the sub folders on each Parent folder created.

    And sub folders creates only on Last folder which is mentioned in the Parent folder CSV input. please find the below CSV format. Not sure what is working on this

    folder full_control modify read_execute list_folder_content read write
    folderxyz DL-WGK-US-Houston-Projects-XXXXXX-RW-TEST
    new
    folder456

    [code]Set-Location "C:\Users\IBM_ADMIN\Desktop\foldertest"
    $csvFile = "C:\Users\IBM_ADMIN\Desktop\import.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}

    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 "ReadData"}
    if ($folder.write) {DoPermissions $folder.write $fullPath "Write"}

    }
    # define the subfolder names
    $subfolders = "Subfolder1","confidential","SubFolder3","Subfolder4","document"

    for ($i = 1;$i -lt 4;$i++)
    { # create the sub folder
    $subpath = "$fullPath\$($subfolders[$i])"
    if (!(test-path $subpath -PathType Container)) {New-Item $subpath -type directory}
    # set the sub folder permissions
    switch ($i) {
    1 {DoPermissions $folder.owners $fullPath "ReadData"}
    2 {DoPermissions $folder.owners $fullPath "ReadData";DoPermissions $folder.owners $fullPath "Write"}
    3 {DoPermissions $folder.owners $fullPath "ReadData"}
    4 {DoPermissions $folder.owners $fullPath "ReadData";DoPermissions $folder.owners $fullPath "ListDirectory"}

    }
    }

  • #69369

    Keymaster
    Points: 1,625
    Helping HandTeam Member
    Rank: Community Hero

    It's a bit difficult to follow the script because you didn't format it (instructions are shown right above the posting text box). Offhand, I'd guess the logic in your ForEach is the problem – that's usually the case.

The topic ‘Creation of sub folders with Parent folders in one handy’ is closed to new replies.