Creation of sub folders with Parent folders in one handy

Tagged: 

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 1 month ago.

  • Author
    Posts
  • #69265
    Profile photo of Narasimha Sumanth N
    Narasimha Sumanth N
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

You must be logged in to reply to this topic.