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
AXISHKAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
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.
0
AXISHKAuthor 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
0
Daryl BamforthTechnical ExpertCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

AXISHKAuthor 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

0
Daryl BamforthTechnical ExpertCommented:
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?
0
AXISHKAuthor 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
0
Daryl BamforthTechnical ExpertCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.