Odd issue with Get-Acl from $listBox.SelectedItem

by Wayfinder at 2013-04-29 21:09:00

Hi Guys,

Currently I hit a brick wall with this really odd issue.

Code Snippet:

[code2=powershell]$lb_ACLGroups_SelectedIndexChanged=
{
$SelectedGroup = ($lb_ACLGroups.SelectedItem)
$GroupQuery = (Get-Acl “\Server01\ABC”) | select -Expand Access | where { $.IdentityReference -eq “$SelectedGroup”} | Out-File c:\temp\GrpDescript.txt
(get-content “c:\temp\GrpDescript.txt”) | ? {$
.trim() -ne “” } | Set-Content "c:\temp\GrpDescript.txt"
Write-Host $SelectedGroup
$richTextBox2.Text = $SelectedGroup
}
$lb_ACLGroups.add_SelectedIndexChanged($lb_ACLGroups_SelectedIndexChanged) #Must Create before can assign[/code2]

Issue:
- The Listbox is populated with 5 items. e.g.

a
b
c
d
e

- $SelectedGroup = ($lb_ACLGroups.SelectedItem) , Works fine when writing to host or updating $richTextBox2 . You can cycle through each item in the list and it will successfull update $richTextBox2 and output to Powershell console.

- The $GroupQuery ONLY works on the first item withitn the list so being item a. If other items are selected the command is proccessed but there is no contents in the text file.
If I reselect the first item in the list (a) then it writes data to the file.


So why would Write-Host $SelectedGroup work correctly but the (Get-Acl “\Server01\ABC”) | select -Expand Access | where { $.IdentityReference -eq “$SelectedGroup”} Does not.

Is there any debugging tips you could provide? is it possible to pipe the entire command to a text file so you can verify that the variable in where { $
.IdentityReference -eq “$SelectedGroup” is being updated.


Thanks for your support
by ArtB0514 at 2013-04-30 07:19:09
You need to make sure that the $SelectedGroup actually exists in IdentityReference. Here’s the test I’d perform:
“Checking ACLs for $SelectedGroup”
(Get-Acl “\Server01\ABC”) | select-object -ExpandProperty Access | Select-Object IdentityReference
by Wayfinder at 2013-04-30 18:56:22
[quote=“ArtB0514”]You need to make sure that the $SelectedGroup actually exists in IdentityReference. Here’s the test I’d perform:
“Checking ACLs for $SelectedGroup”
(Get-Acl “\Server01\ABC”) | select-object -ExpandProperty Access | Select-Object IdentityReference
[/quote]

Hi ArtB0514,

Could you please elaborate? ,
Your code example doesn not yeild anything.

Thanks mate.
by Wayfinder at 2013-04-30 23:23:31
All is good,

Found it was an issue with .Trim() . As I was not removing all trailing white spaces.