Extract a portion in powershell result

AXISHK
AXISHK used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016
Commented:
Pls try this -

$path.SubString(($path.LastIndexOf('D:\'),100)
$AccessToString.SubString(($AccessToString.LastIndexOf('Domain Admins'),100)    

Open in new window


you can also replace 100 with $path.length.

Author

Commented:
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
Technical Expert
Commented:
You already know your $path variable, so just include this at the beginning as a set variable. This means that manipulating the output from get-acl becomes much easier, as we are no longer trying to recreate $path from the powershell path entity.

As you are only concerned about the username portion of the username we can now split the output on '\' and then select the 2nd object in the split array. I do need to point out, however, that this has the potential to throw up duplicates if users have local admin permissions as well as domain permissions you will not be able to differentiate between the two (such as Administrator). For the most part it should be fine though.

(get-acl c:\temp).Access |select -expandproperty identityreference |% {$_.value.split('\')[1]}

Open in new window


Full script..

$path = "C:\temp"
$users = @()
$users = (get-acl $path).Access |select -expandproperty identityreference |% {$_.value.split('\')[1]}
$details = @()
$details = "" |select path, users
$details.path = $path
$details.users = $users

Open in new window

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

Commented:
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 BamforthTechnical Expert

Commented:
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?

Author

Commented:
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 BamforthTechnical Expert

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial