Avatar of AXISHK
AXISHK
 asked on

Split item in Powershell

Is it possible to separate the users in the following command line and join them in a single line with "," as separator ?  Thx

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

Avatar of undefined
Last Comment
Alan

8/22/2022 - Mon
SOLUTION
Qlemo

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.
ASKER CERTIFIED SOLUTION
Alan

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

Alan, I might be overlooking something, but I don't know why you use -join ',' -split ',' here. Unless the data itself contains comma-separated values (which it does not, AFAIK), this is redundant, and can be removed.
I cannot find a reason for IdentityReference (or its split) being empty unless the ACL itself is empty, or the split being empty, which means the ACL is not following the namespace\object format?

As I see it, the whole expression, removing dups, can be significantly simplified (changing much of the original command):
((Get-ACL D:\Dept-Home).Access.IdentityReference.Value -replace '.*\' | Sort -Unique | ? { $_ }) -join ','

Open in new window

This assumes IdentityReference is never empty.
AXISHK

ASKER
The powershell has error...

The regular expression pattern .*\ is not valid.
At line:1 char:1
+ ((Get-ACL C:\).Access.IdentityReference.Value -replace '.*\' | Sort -Unique | ?
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (.*\:String) [], RuntimeException
    + FullyQualifiedErrorId : InvalidRegularExpression
Alan

I get the same issue on the machine I just tested it on (PSVersion = 2.0)

Edit:  Also on PSVersion 5.1

To answer your more general query about how I constructed the command, I was seeking to take the original command supplied in the OP, and 'fix' it, rather than re-write it, hence I left:

(get-acl D:\dept-home).Access |select -expandproperty identityreference

Open in new window


then converted it to get all on one line, and remove the blanks / duplicates.

I figured the OP could then generalise if they need to do the same to other similar outputs.

I'm sure most of my code could be improved to look better, be shorter, or to run faster - I tend to stop once it works :-)

Alan.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Qlemo

Sorry for  the (copy&paste) error, it should be -replace '.*\\'.

Alan, the main point is that "stop once it works" is no good base for code. Something obvious like a join split combo needs to trigger some alerts.
Blanks or empty values are often an indication of an error made somewhere. As said, it would be important to know why you (think to) see those.
Alan

Hi Qlemo,

If you run the command as it was posted in the OP and
Just enclose the whole expression in     ( ... ) -join ','
do you ever see any blanks or duplicates?

Alan.
Qlemo

Dups yes, but no blanks. The dups are a good point, but already contained in the original question, so removing them is "not necessary" and an improvement (I should have considered myself).
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Alan

Interesting - I see blanks too sometimes.

This is what I get on a 'test' folder I created under T:\Company\

First, using the original command:

PS C:\Temp> (get-acl T:\Company\Test\).Access |select -expandproperty identityreference |% {$_.value.split('\')[1]}
Example_SecGroup_TSRemoteUsers
BackupOperator
SYSTEM
Domain Admins
Domain Users
Administrators
Users

Open in new window


Which looks to be what you would expect.

Now with your command:

PS C:\Temp> ((get-acl T:\Company\Test\).Access |select -expandproperty identityreference |% {$_.value.split('\')[1]}) -join ','
Example_SecGroup_TSRemoteUsers,BackupOperator,,SYSTEM,Domain Admins,Domain Users,Administrators,Users

Open in new window


So with the original command, I don't get a 'blank', but with your command, I get a 'blank' (two commas in a row) between 'BackupOperator' and 'SYSTEM'.

Also interesting is if I right-click on the folder, select properties, and security, I also see 'Creator Owner' listed, which is missing from all of the above.

I do not see any 'blank' line or any other line that is not listed above that would explain the blank in the list from your command.

Alan.