Avatar of AXISHK
AXISHK
 asked on

Extract a portion in powershell result

Run get-acl d:\dept-home  | Select-Object path, accesstostring

Is it possible to simplify the output to show something like  D:\dept-home    Domain Admin...  ?  Thx

Path                                                                                                               AccessToString                                                                  
----                                                                                                                      --------------                                                                  
Microsoft.PowerShell.Core\FileSystem::D:\dept-home                                MYDOMAIN\Domain Admins Allow  FullControl...                            


Thx
Powershell

Avatar of undefined
Last Comment
Daryl Bamforth

8/22/2022 - Mon
SOLUTION
Pawan Kumar

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.
AXISHK

ASKER
For the Access field, I want to format the output like john, Mary, etc which require to extract the value inside the string like mydomain\john .... mydomain\mary .

How can I archive this result ?

Thx
ASKER CERTIFIED SOLUTION
Daryl Bamforth

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.
AXISHK

ASKER
The below code works fine but I change one line to remove top folder path, it returns with error :

Cannot find an overload for "SubString" and the argument count: "1".
At C:\Scripts\test.ps1:10 char:5
+     $temp.Folder = $_.Fullname.SubString(($_.Fullname.LastIndexOf($path),100))
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

$temp.Folder = $_.Fullname.SubString(($_.Fullname.LastIndexOf($path),100))




$path = @()
$users = @()
$path = "D:\dept-home"

$details = New-Object System.Collections.ArrayList

get-childitem $path  | foreach {If ($_.psiscontainer)  {
    $temp = "" | select "Folder", "Access" 
    $StrPath = 
    $temp.Folder = $_.Fullname 

    $users = (get-acl $_.FullName).Access |select -expandproperty identityreference |% {$_.value.split('\')[1]}
    $temp.Access = $users
    
    $details.Add($temp) | Out-Null
}}
 write-output $details

Open in new window

Daryl Bamforth

Remove
$StrPath = 

Open in new window

no point in it being there since not assigned to anything.

What is the full scope of what you need to achieve?

If you just want ACLs for a named folder the script I put in does that, to export just add this to the bottom.

$details |export-csv -path c:\temp\ACLexport.csv -notypeinformation

Open in new window


Do you want recursive folder ACLs for all sub folders as well?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
AXISHK

ASKER
Export CSV has been fixed but can't trim the path with the following error :

$temp.Folder = $_.Fullname.SubString(($_.Fullname.LastIndexOf($path),100))

Cannot find an overload for "SubString" and the argument count: "1".
At C:\Scripts\test.ps1:10 char:5
+     $temp.Folder = $_.Fullname.SubString(($_.Fullname.LastIndexOf($path),100))
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
Daryl Bamforth

What are you wanting to trim?

At the moment $_.fullname is returning the full path so

d:\dept-home\folder1

If you just want the 'folder1' bit then change fullname, to name, so $_.name, which would return

folder1