SOLVED

Getting a parent's parent for a folder using power shell

%3CLINGO-SUB%20id%3D%22lingo-sub-1769210%22%20slang%3D%22en-US%22%3EGetting%20a%20parent's%20parent%20for%20a%20folder%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1769210%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20using%20a%20p%3CSPAN%20class%3D%22lia-link-navigation%20lia-link-disabled%22%3Eowershell%20script%20to%20enable%20inheritance%20for%20the%20folders%20created%20in%20NTFS%20share.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-link-navigation%20lia-link-disabled%22%3EIn%20this%20script%2C%20i%20am%20getting%20the%20folder%20created%20for%20the%20user%20by%20passing%20a%20dynamic%20value%20and%20then%20getting%20the%20parent%20folder%20and%20inheriting%20the%20permission%20to%20the%20new%20folder%20from%20parent.%20It%20is%20all%20working%20good.%20But%20i%20would%20need%20to%20get%20parent's%20parent%20in%20few%20of%20the%20cases.%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-link-navigation%20lia-link-disabled%22%3EUse%20Case%20%3A%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-link-navigation%20lia-link-disabled%22%3ENew%20Folder%20%3A%20%5C%5Clocalhost%5CTestShare%5Cs%5Ctest6%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-link-navigation%20lia-link-disabled%22%3Etest6%20%3A%20is%20the%20new%20folder%20i%20am%20creating.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-link-navigation%20lia-link-disabled%22%3EMy%20current%20script%20gives%20the%20parent%20folder%20as%20%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3Es%20%3C%2FSTRONG%3E%3C%2FFONT%3Eand%20%3CFONT%20color%3D%22%233366FF%22%3Etest6%3C%2FFONT%3E%20inherits%20the%20permissions%20from%20%3CFONT%20color%3D%22%230000FF%22%3E'%3CSTRONG%3Es%3C%2FSTRONG%3E'%3C%2FFONT%3E%20folder.%20But%20i%20would%20need%20to%20get%20the%20parent's%20parent%20(in%20this%20case%20it%20would%20be%20%3CSTRONG%3ETestShare%3C%2FSTRONG%3E)%20if%20the%20length%20of%20the%20parent%20is%20%3CSTRONG%3Eone%3C%2FSTRONG%3E%20in%20few%20cases.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-link-navigation%20lia-link-disabled%22%3EIs%20there%20a%20way%20to%20get%20it%20in%20my%20current%20script%20without%20interrupting%20the%20current%20logic.%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3EGet-Date%0A%24the_shares%20%3D%20args%5B0%5D%0A%24allFolders%3DGet-ItemProperty%20-Path%20%24the_shares%0A%24allFolders.Parent.Name%0A%24Permission%3Dget-acl%20-Path%20%24the_shares%0A%24Permission.SetAccessRuleProtection(%24False%2C%24true)%0ASet-Acl%20-Path%20%24the_shares%20-AclObject%20%24Permission%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1769210%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1769409%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20a%20parent's%20parent%20for%20a%20folder%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1769409%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F806703%22%20target%3D%22_blank%22%3E%40suren424%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20just%20for%20me%20to%20understand%20a%20point%2C%20so%20do%20you%20need%20to%20get%20the%20parent%20parent%20folder%20incase%20the%20parent%20length%20is%201%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1769797%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20a%20parent's%20parent%20for%20a%20folder%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1769797%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYes%2C%20if%20the%20parent%20length%20is%201%2C%20i%20need%20to%20get%20it's%20parent%20folder.%26nbsp%3B%20Below%20is%20the%20explanation%20with%20an%20example%20%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EExample%20%3A%26nbsp%3B%20%24the_shares%20%3D%20('C%3A%5CShare2%5Ca%5Ctest'%2C%20'C%3A%5CShare1%5Cabc1%5C')%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CU%3E%3CSTRONG%3ECase%201%3A%26nbsp%3B%20'C%3A%5CShare2%5Ca%5Ctest'%3C%2FSTRONG%3E%3C%2FU%3E%3C%2FP%3E%3CP%3EThe%20parent%20is%20%3CSTRONG%3E'a'%3C%2FSTRONG%3E%20and%20the%20length%3D1.%20So%2C%20i%20need%20to%20get%20it's%20parent%2C%20which%20is%20%3CSTRONG%3E'Share2'%3C%2FSTRONG%3E%20and%20set%20the%20permission%20on%20%3CSTRONG%3E'test'%3C%2FSTRONG%3E%20folder%20to%20inherit%20from%20'Share2'.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20can%20i%20get%20the%20parent%20parent%20folder%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CU%3E%3CSTRONG%3ECase%202%3A%26nbsp%3B%20'C%3A%5CShare1%5Cabc1'%3C%2FSTRONG%3E%3C%2FU%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%2C%20the%20parent%20is%20'Share1'.%20So%2C%20i%20would%20just%20need%20set%20the%20permission%20on%20%3CSTRONG%3E'abc1'%3C%2FSTRONG%3E%20folder%20to%20inherit%20from%20'Share1'.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%24the_shares%20%3D%20('C%3A%5CShare2%5Ca%5Ctest'%2C%20'C%3A%5CShare1%5Cabc1')%0A%24allFolders%3DGet-ItemProperty%20-Path%20%24the_shares%0Aif%20((%24allFolders.Parent.Name).Length%20-eq%201)%7B%0AWrite-Host%20%22My%20Length%20is%20Only%201%2C%22%20%0AWrite-Host%20%22The%20Parent%20is%20%22%20-ForegroundColor%20Green%20-NoNewline%0A%24allFolders.Parent.Name%0AWrite-Host%20%22Add%20your%20condition%20here%20%3A)%22%0A%0AElse%7B%0AWrite-Host%20%22I%20am%20longer%20than%201%20%3A(%22%0AWrite-Host%20%22Write%20another%20condition%20here%20%3A)%22%0A%24Permission%3Dget-acl%20-Path%20%24the_shares%0A%24Permission.SetAccessRuleProtection(%24False%2C%24true)%0ASet-Acl%20-Path%20%24the_shares%20-AclObject%20%24Permission%0A%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1769926%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20a%20parent's%20parent%20for%20a%20folder%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1769926%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F806703%22%20target%3D%22_blank%22%3E%40suren424%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETry%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%24the_shares%20%3D%20('C%3A%5CShare2%5Ca%5Ctest')%0A%24the_shares%20%3D%20('C%3A%5CShare1%5Cabc1')%0A%24allFolders%3DGet-ItemProperty%20-Path%20%24the_shares%0Aif%20((%24allFolders.Parent.Name).Length%20-eq%201)%7B%0AWrite-Host%20%22The%20Parent%20Length%20is%20Only%201%22%20%0AWrite-Host%20%22Setting%20the%20permissions%20of%22%20%24allfolders.Name%20%22based%20on%22%20%24allfolders.Parent.Parent.Name%0A%24Permission%3Dget-acl%20-Path%20%24allfolders.Parent.Parent.FullName%0A%24Permission.SetAccessRuleProtection(%24False%2C%24true)%0ASet-Acl%20-Path%20%24allfolders.FullName%20-AclObject%20%24Permission%0A%7D%0AElse%7B%0AWrite-Host%20%22The%20Parent%20Length%20is%20longer%20than%201%22%0AWrite-Host%20%22Setting%20the%20permissions%20of%22%20%24allfolders.Name%20%22based%20on%22%20%24allfolders.Parent.Name%0A%24Permission%3Dget-acl%20-Path%20%24allfolders.Parent.FullName%0A%24Permission.SetAccessRuleProtection(%24False%2C%24true)%0ASet-Acl%20-Path%20%24allfolders.FullName%20-AclObject%20%24Permission%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1769956%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20a%20parent's%20parent%20for%20a%20folder%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1769956%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%20very%20much%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F114961%22%20target%3D%22_blank%22%3E%40Wesley%20Baker%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20script%20is%20working.%20But%20if%20%24the_shares%20has%20more%20than%201%2C%20it%20is%20throwing%20error.%20Do%20i%20need%20to%20use%20for%20loop%20%3F%3C%2FP%3E%3CP%3EIf%20yes%2C%20i%20am%20bit%20confused%20on%20what%20to%20use%20in%20the%20for%20loop.could%20you%20please%20help%20me%20on%20this%20one%20too.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24the_shares%20%3D%20('C%3A%5COIMShare2%5Ca%5Ctest%5C'%2C%20'C%3A%5COIMShare2%5Ctest%5C')%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1769963%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20a%20parent's%20parent%20for%20a%20folder%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1769963%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F806703%22%20target%3D%22_blank%22%3E%40suren424%3C%2FA%3E%26nbsp%3BSure%2C%20Here%20is%20how%20I%20would%20handle%20that%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Efunction%20setPermissions(%5Bstring%5D%24theshare)%20%7B%0A%20%20%20%20%24allFolders%3DGet-ItemProperty%20-Path%20%24theshare%0A%20%20%20%20if%20((%24allFolders.Parent.Name).Length%20-eq%201)%7B%0A%20%20%20%20Write-Host%20%22The%20Parent%20Length%20is%20Only%201%22%20%0A%20%20%20%20Write-Host%20%22Setting%20the%20permissions%20of%22%20%24allfolders.Name%20%22based%20on%22%20%24allfolders.Parent.Parent.Name%0A%20%20%20%20%24Permission%3Dget-acl%20-Path%20%24allfolders.Parent.Parent.FullName%0A%20%20%20%20%24Permission.SetAccessRuleProtection(%24False%2C%24true)%0A%20%20%20%20Set-Acl%20-Path%20%24allfolders.FullName%20-AclObject%20%24Permission%0A%20%20%20%20%7D%0A%20%20%20%20Else%7B%0A%20%20%20%20Write-Host%20%22The%20Parent%20Length%20is%20longer%20than%201%22%0A%20%20%20%20Write-Host%20%22Setting%20the%20permissions%20of%22%20%24allfolders.Name%20%22based%20on%22%20%24allfolders.Parent.Name%0A%20%20%20%20%24Permission%3Dget-acl%20-Path%20%24allfolders.Parent.FullName%0A%20%20%20%20%24Permission.SetAccessRuleProtection(%24False%2C%24true)%0A%20%20%20%20Set-Acl%20-Path%20%24allfolders.FullName%20-AclObject%20%24Permission%0A%20%20%20%20%7D%0A%7D%0A%0A%0A%23Method%201%20-%20Hardcoded%20array%0AWrite-Host%20%22Method%201%20-%20Hardcoded%20array%22%20-ForegroundColor%20Green%0A%24the_shares%20%3D%20('C%3A%5CShare2%5Ca%5Ctest'%2C'C%3A%5CShare1%5Cabc1')%0Aforeach%20(%24f%20in%20%24the_shares)%20%7B%0A%20%20%20%20setPermissions%20%24f%0A%7D%0A%0A%23Method%202%20-%20Iterate%20a%20folder%20and%20process%20all%20sub%20folders%0AWrite-Host%20%22Method%202%20-%20Iterate%20a%20folder%20and%20process%20all%20sub%20folders%22%20-ForegroundColor%20Green%0AGet-ChildItem%20-Path%20%22C%3A%5Cshare1%22%20%7C%20foreach%7BsetPermissions%20%24_.FullName%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%E2%80%8B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1770455%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20a%20parent's%20parent%20for%20a%20folder%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1770455%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F806703%22%20target%3D%22_blank%22%3E%40suren424%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CBLOCKQUOTE%3E%3CHR%20%2F%3E%3CP%3E%3CU%3E%3CSTRONG%3ECase%201%3A%26nbsp%3B%20'C%3A%5CShare2%5Ca%5Ctest'%3C%2FSTRONG%3E%3C%2FU%3E%3C%2FP%3E%3CP%3EThe%20parent%20is%20%3CSTRONG%3E'a'%3C%2FSTRONG%3E%20and%20the%20length%3D1.%20So%2C%20i%20need%20to%20get%20it's%20parent%2C%20which%20is%20%3CSTRONG%3E'Share2'%3C%2FSTRONG%3E%20and%20set%20the%20permission%20on%20%3CSTRONG%3E'test'%3C%2FSTRONG%3E%20folder%20to%20inherit%20from%20'Share2'.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20can%20i%20get%20the%20parent%20parent%20folder%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CHR%20%2F%3E%3C%2FBLOCKQUOTE%3E%3CP%3EJust%20a%20quick%20tip%2C%20%3CSTRONG%3Etest%3C%2FSTRONG%3E%20folder%20cannot%20get%20its%20permission%20from%20%3CSTRONG%3EShare2%2C%26nbsp%3B%3C%2FSTRONG%3Eit%20will%20get%20its%20permission%20only%20from%26nbsp%3B%3CSTRONG%3Ea%3C%2FSTRONG%3E%20unless%20you%20chose%20to%20copy%20the%20permission%2C%26nbsp%3B%3CSTRONG%3Etest%3C%2FSTRONG%3E%20will%20only%20get%20its%20permission%20from%26nbsp%3B%3CSTRONG%3Ea%3C%2FSTRONG%3E.%3C%2FP%3E%3CP%3ESo%20to%20make%20sure%20that%20the%20script%20work%2C%26nbsp%3B%3CSTRONG%3Ea%26nbsp%3B%3C%2FSTRONG%3Emust%20inherit%20its%20permission%20from%26nbsp%3B%3CSTRONG%3Eshare2%3C%2FSTRONG%3E%20first%20and%20so%20on%26nbsp%3B%3CSTRONG%3Etest%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1772799%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20a%20parent's%20parent%20for%20a%20folder%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1772799%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%20.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tested%20the%20script%20few%20more%20times%20and%20yes%20it%20is%20not%20inheriting%20the%20permissions%20from%20parents%20parent.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%2C%20will%20the%20below%20logic%20work%20%3F%3C%2FP%3E%3CP%3EIf%20the%20parent%20length%20is%20one%2C%20set%20the%20permission%20on%20parent%20to%20inherit%20from%20it's%20parent%20and%20then%20set%20the%20homefolder%20to%20inherit%20from%20it's%20parent.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EExample%20%3A%3C%2FP%3E%3CP%3EHomeFolder%20%3A%20C%3A%5COIMShare2%5Cb%5Ctest%5C%3C%2FP%3E%3CP%3EHere%20the%20'test'%20parent%20would%20be%20'b'.%20so%2C%20inherit%20the%20permission%20from%20OIMShare2%20to%20'b'%20and%20after%20that%20set%20the%20permissions%20on%20'test'%20to%20inherit%20from%20'b'.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%24the_shares%20%3D%20'C%3A%5COIMShare2%5Cb%5Ctest%5C'%0A%24allFolders%3DGet-ItemProperty%20-Path%20%24the_shares%0Aif%20((%24allFolders.Parent.Name).Length%20-eq%201)%7B%0AWrite-Host%20%22My%20Length%20is%20Only%201%22%20%0AWrite-Host%20%22Setting%20the%20permissions%20of%22%20%24allfolders.Name%20%22based%20on%22%20%24allfolders.Parent.Parent.Name%0A%24Permission%3Dget-acl%20-Path%20%24allfolders.Parent.Parent.FullName%0A%24Permission.SetAccessRuleProtection(%24False%2C%24true)%0ASet-Acl%20-Path%20%24allfolders.FullName%20-AclObject%20%24Permission%0A%7D%0AElse%7B%0AWrite-Host%20%22The%20Parent%20Length%20is%20longer%20than%201%22%0AWrite-Host%20%22Setting%20the%20permissions%20of%22%20%24allfolders.Name%20%22based%20on%22%20%24allfolders.Parent.Name%0A%24allFolders.Parent.Name%0A%24Permission%3Dget-acl%20-Path%20%24allfolders.Parent.FullName%0A%24Permission.SetAccessRuleProtection(%24False%2C%24true)%0ASet-Acl%20-Path%20%24allfolders.FullName%20-AclObject%20%24Permission%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1773424%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20a%20parent's%20parent%20for%20a%20folder%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1773424%22%20slang%3D%22en-US%22%3EYes%20surely%20the%20logic%20is%20possible%20and%20doable%20via%20powershell%3CBR%20%2F%3ELet%20me%20ask%20u%20on%20more%20question%3CBR%20%2F%3EThis%20is%20Grandfather%2C%20Father%2C%20son%20relation%3CBR%20%2F%3EThis%20should%20go%20in%20order%3CBR%20%2F%3EMaybe%20ibam%20asking%20the%20question%20again%2C%20but%20i%20am%20a%20bit%20confused.%3CBR%20%2F%3EDo%20u%20want%20to%20enable%20inherent%20in%20all%20the%20chain%20or%20u%20want%20to%20copy%20the%20permission%3F!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1773524%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20a%20parent's%20parent%20for%20a%20folder%20using%20power%20shell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1773524%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EI%20would%20like%20enable%20inheritance%20so%20that%20it%20would%20get%20all%20the%20permissions.%3CBR%20%2F%3E%3CBR%20%2F%3EBut%2C%20What%20is%20the%20difference%20between%20enable%20inherent%20in%20all%20the%20chain%20or%20u%20copying%20the%20permission%3F%3CBR%20%2F%3E%3CBR%20%2F%3EThank%20you%2C%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi,

 

I am using a powershell script to enable inheritance for the folders created in NTFS share. 

In this script, i am getting the folder created for the user by passing a dynamic value and then getting the parent folder and inheriting the permission to the new folder from parent. It is all working good. But i would need to get parent's parent in few of the cases.

 

Use Case :

New Folder : \\localhost\TestShare\s\test6

test6 : is the new folder i am creating.

My current script gives the parent folder as s and test6 inherits the permissions from 's' folder. But i would need to get the parent's parent (in this case it would be TestShare) if the length of the parent is one in few cases.

Is there a way to get it in my current script without interrupting the current logic.

 

 

 

Get-Date
$the_shares = args[0]
$allFolders=Get-ItemProperty -Path $the_shares
$allFolders.Parent.Name
$Permission=get-acl -Path $the_shares
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $the_shares -AclObject $Permission

 

 

 

Thanks in advance.

 

 

17 Replies

@suren424 

 

Hi, 

So just for me to understand a point, so do you need to get the parent parent folder incase the parent length is 1?

if so, I updated your code to be

 

$allFolders=Get-ItemProperty -Path $the_shares
if (($allFolders.Parent.Name).Length -eq 1){
Write-Host "My Length is Only 1, "
Write-Host "Add your condition here :)"
Else{

Write-Host "I am longer than 1 :("
Write-Host "Write another condition here :)"
}

$Permission=get-acl -Path $the_shares
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $the_shares -AclObject $Permission

Thank you@farismalaeb 

 

Yes, if the parent length is 1, i need to get it's parent folder.  Below is the explanation with an example :

 

Example :  $the_shares = ('C:\Share2\a\test', 'C:\Share1\abc1\')

 

Case 1:  'C:\Share2\a\test'

The parent is 'a' and the length=1. So, i need to get it's parent, which is 'Share2' and set the permission on 'test' folder to inherit from 'Share2'.

 

How can i get the parent parent folder ?

 

Case 2:  'C:\Share1\abc1'

 

Here, the parent is 'Share1'. So, i would just need set the permission on 'abc1' folder to inherit from 'Share1'.

 

 

$the_shares = ('C:\Share2\a\test', 'C:\Share1\abc1')
$allFolders=Get-ItemProperty -Path $the_shares
if (($allFolders.Parent.Name).Length -eq 1){
Write-Host "My Length is Only 1," 
Write-Host "The Parent is " -ForegroundColor Green -NoNewline
$allFolders.Parent.Name
Write-Host "Add your condition here :)"

Else{
Write-Host "I am longer than 1 :("
Write-Host "Write another condition here :)"
$Permission=get-acl -Path $the_shares
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $the_shares -AclObject $Permission
}
}

 

 

Thanks in advance.

 

Hi @suren424,

 

Try this:

 

#$the_shares = ('C:\Share2\a\test')
$the_shares = ('C:\Share1\abc1')
$allFolders=Get-ItemProperty -Path $the_shares
if (($allFolders.Parent.Name).Length -eq 1){
Write-Host "The Parent Length is Only 1" 
Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Parent.Name
$Permission=get-acl -Path $allfolders.Parent.Parent.FullName
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $allfolders.FullName -AclObject $Permission
}
Else{
Write-Host "The Parent Length is longer than 1"
Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Name
$Permission=get-acl -Path $allfolders.Parent.FullName
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $allfolders.FullName -AclObject $Permission
}

 

Thank you very much @Wesley Baker

 

The script is working. But if $the_shares has more than 1, it is throwing error. Do i need to use for loop ?

If yes, i am bit confused on what to use in the for loop.could you please help me on this one too.

 

$the_shares = ('C:\OIMShare2\a\test\', 'C:\OIMShare2\test\')

 

Thanks in advance

@suren424 Sure, Here is how I would handle that:

 

function setPermissions([string]$theshare) {
    $allFolders=Get-ItemProperty -Path $theshare
    if (($allFolders.Parent.Name).Length -eq 1){
    Write-Host "The Parent Length is Only 1" 
    Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Parent.Name
    $Permission=get-acl -Path $allfolders.Parent.Parent.FullName
    $Permission.SetAccessRuleProtection($False,$true)
    Set-Acl -Path $allfolders.FullName -AclObject $Permission
    }
    Else{
    Write-Host "The Parent Length is longer than 1"
    Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Name
    $Permission=get-acl -Path $allfolders.Parent.FullName
    $Permission.SetAccessRuleProtection($False,$true)
    Set-Acl -Path $allfolders.FullName -AclObject $Permission
    }
}


#Method 1 - Hardcoded array
Write-Host "Method 1 - Hardcoded array" -ForegroundColor Green
$the_shares = ('C:\Share2\a\test','C:\Share1\abc1')
foreach ($f in $the_shares) {
    setPermissions $f
}

#Method 2 - Iterate a folder and process all sub folders
Write-Host "Method 2 - Iterate a folder and process all sub folders" -ForegroundColor Green
Get-ChildItem -Path "C:\share1" | foreach{setPermissions $_.FullName}

 

@suren424 


Case 1:  'C:\Share2\a\test'

The parent is 'a' and the length=1. So, i need to get it's parent, which is 'Share2' and set the permission on 'test' folder to inherit from 'Share2'.

 

How can i get the parent parent folder ?

 

 


Just a quick tip, test folder cannot get its permission from Share2, it will get its permission only from a unless you chose to copy the permission, test will only get its permission from a.

So to make sure that the script work, must inherit its permission from share2 first and so on test

Thanks@farismalaeb .

 

I tested the script few more times and yes it is not inheriting the permissions from parents parent.

 

So, will the below logic work ?

If the parent length is one, set the permission on parent to inherit from it's parent and then set the homefolder to inherit from it's parent.

 

Example :

HomeFolder : C:\OIMShare2\b\test\

Here the 'test' parent would be 'b'. so, inherit the permission from OIMShare2 to 'b' and after that set the permissions on 'test' to inherit from 'b'.

 

$the_shares = 'C:\OIMShare2\b\test\'
$allFolders=Get-ItemProperty -Path $the_shares
if (($allFolders.Parent.Name).Length -eq 1){
Write-Host "My Length is Only 1" 
Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Parent.Name
$Permission=get-acl -Path $allfolders.Parent.Parent.FullName
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $allfolders.FullName -AclObject $Permission
}
Else{
Write-Host "The Parent Length is longer than 1"
Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Name
$allFolders.Parent.Name
$Permission=get-acl -Path $allfolders.Parent.FullName
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $allfolders.FullName -AclObject $Permission
}

 

Thanks in advance

 

Yes surely the logic is possible and doable via powershell
Let me ask u on more question
This is Grandfather, Father, son relation
This should go in order
Maybe ibam asking the question again, but i am a bit confused.
Do u want to enable inherent in all the chain or u want to copy the permission?!
@farismalaeb

I would like enable inheritance so that it would get all the permissions.

But, What is the difference between enable inherent in all the chain or u copying the permission?

Thank you,

@Wesley Baker  @farismalaeb 

 

While going through the script, i had a question. How is the script getting the permission from the parent and inheriting the same. I couldn't figure it out. Am i giving the $the_shares variable in the wrong position?

 

Please advise.

 

I tried the below 2 scripts, both seem to give the same result. couldn't get the difference.

 

Script 1:

$the_shares = 'C:\OIMShare3\test1'
$allFolders=Get-ItemProperty -Path $the_shares
Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Name
$allFolders.Parent.Name
$Permission=get-acl -Path $the_shares
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $the_shares -AclObject $Permission

 

Script 2:

$the_shares = 'C:\OIMShare3\test2'
$allFolders=Get-ItemProperty -Path $the_shares
Write-Host "Setting the permissions of" $allfolders.Name "based on" $allfolders.Parent.Name
$allFolders.Parent.Name
$Permission=get-acl -Path $allFolders.Parent.FullName
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $allFolders.FullName -AclObject $Permission

 

Thanks in advance. 

@Wesley Baker  @farismalaeb

Any inputs on this please ?

 

Thanks in advance

@suren424 I am no expert on ACL's, inheritance and permissions, and have never used get-acl in anger, but I can see the difference in your two scripts is on line 5

 

$Permission=get-acl -Path $the_shares

 

vs

 

$Permission=get-acl -Path $allfolders.Parent.FullName

 

$the_shares is a simple text constant, and PS know nothing more about it.  $allfolders is an object that represents the folder named in the text constant.  This allows us to access it's properties dynamically, such as it's parent folder.

$allFolders=Get-ItemProperty -Path $the_shares

In Script 2, we are getting (into $permission variable) the ACL on the Parent's Path, but in Script 1, we are getting the ACL on $the_shares text value.  Therefore, Script 1 does nothing except copy the permissions from $the_shares, and then applies it back to itself without changing it...so it is redundant.

 

If get-acl set-acl is indeed the correct way to set inheritance, then I think you would find Script 2 does what you wanted if the parent folder has a length greater than 1. 

 

But as @farismalaeb mentioned  Here in his post on Monday, if the length is one, you would want to first get-acl of $allfolders.Parent.Parent.FullName, and set-acl on $allfolders.Parent.FullName, THEN you would get-acl of $allfolders.Parent.FullName, and set-acl on $allfolders.FullName, so that you respect the Grandfather, Father, Son relationship  (apologies if the choice of pronoun offends ;) ) and apply the relationship in the correct order

@suren424 

Hi, I am back :)

$Permission=get-acl -Path $allFolders.Parent.FullName

will get the parent folder name, so for C:\Folder1\Folder2, the above script will get the name of Folder1.

$Permission=get-acl -Path $the_shares

Will get the folder you set in the variable

The Different between copying the permission and inheriting it is, let's assume that you got the following folder structure

C:\Folder1\Folder2\Folder3

Inheriting the permission will copy all the permission as set in Folder1 (for example) down below the tree (other family members)

So if Folder1 has permission for User1 full control, this permission will go down the other folders.

Copying the permission won't inherit the permission from the top, but if user1 have full control in Folder1, it doesn't mean that this user will have the permission on the below folder (Folder2), but the same user User1 may have permission on Folder3.

In your case, I guess you care about inheriting more than copying.

 

Why a person may go for Copying the permission,

lets assume that you have the following folder structure

C:\OfficeFolder\Departments\IT

C:\OfficeFolder\Departments\Accounting

C:\OfficeFolder\Departments\HR

User1 need to access the root of OfficeFolder and the IT folder only but should not access the accounting and HR, so technically, (inheritance is not enabled and you can copy the permission to the below folder with some modification. small example is the home folder.

anyway, I can write a script which will do the following if you are intersted.

you set the root folder and from there it will go down the tree, and if the folder length is 1 then it will enable inheritance, or else do nothing, seems good or its something else

sorry, maybe I forget as I was away :)

 

Thank you@farismalaeb and @Wesley Baker for the great explanations.

 

@farismalaebYes, as of now my requirement is to just inherit permissions from the parent folder.

 

So, if the share is '\\MyShare\home\s\abcd\',  the script should display the parent folder in the comments and set permissions on 'abcd' by inheriting from it's parent 's'. Will the below script work

 

$the_shares = '\\MyShare\home\s\abcd\'
$allFolders=Get-ItemProperty -Path $the_shares
Write-Host "Setting permissions on " $allfolders.Name " by inhertiing from " $allfolders.Parent.Name
$allFolders.Parent.Name
$Permission=get-acl -Path $allFolders.Parent.FullName
$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $allFolders.FullName -AclObject $Permission

 

I understood on this : It gets the acl of the parent folder  : $Permission=get-acl -Path $allFolders.Parent.FullName

 

If you don't mind, can you explain me on the below 2 commands on what it does ?

 

$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $allFolders.FullName -AclObject $Permission

 

Thanks in advance.

Best Response confirmed by suren424 (Occasional Contributor)
Solution

@suren424 

the script seems to be working,

I try it on my side and the result was correct, good job.

$Permission.SetAccessRuleProtection($False,$true)
Set-Acl -Path $allFolders.FullName -AclObject $Permission

In the first line and as the $Permission is holding the folder ACL information, there is one of the methods called SetAccessRuleProtection, you can find all other methods and properties by using $Permission | Get-Member.

More info here

The SetAccessRuleProtection accepts 2 input IsProtected and PreservedInheritance 

SetAccessRuleProtection (bool isProtected, bool preserveInheritance);

IsProtected: Type is Bool ($True/$false) , $False= Enable Inheritance

PreservedInheritance : This parameter is actually ignored as the IsProtected is set to false

 

Thanks

 

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

Feel free to respond and request more information, if you find the answer that satisfy your need, Please make sure to mark it as Best Response and like the other.

 

Hi, Any update on the case, if its OK and no more input is require please select the best response

Thank you@farismalaeb and @Wesley Baker  for your inputs and guidance.

 

Appreciate all your help and support.