Select-Object output into ListView?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
thatsameer
Posts: 18
Last visit: Thu Apr 18, 2019 7:07 am

Select-Object output into ListView?

Post by thatsameer »

Hi,

I firstly request your patience and politeness as I haven't used message boards before & also I'm not a super expert at PowerShell. So if I post something wrong, apologies, but please be nice.

I created a form on Powershell Studio 2018.
(I'm using the Quest ActiveRoles Management Shell to work with Active Directory.)

I type in the name of a Active Directory user in a richtextbox and click 'Go' (e.g. Sam)

On the 'Go' button I've added this code:

$username = $richtextbox1.Text
$usernameresults = get-qaduser $username | Select-Object ntaccountname, accountislockedout

In a normal Powershell ISE console, running the second line of the above script replacing $username with an actual name found in Active Directory returns a table in the output section with the headings 'NTAccountName' and 'AccountisLockedOut'.
This displays all users with the name I inputted (e.g. Sam) with the account name and locked status. Similar table below:

NTAccountName AccountIsLockedOut
------------- ------------------
Domain1\SamJohn False
Domain1\SammyJohn True

TableIWant.png
TableIWant.png (12.97 KiB) Viewed 3337 times

I put this into a $usernameresults variable in my Sapien Form because I want to push this table into a ListView. Similar to this image:

Details-View.png
Details-View.png (6.72 KiB) Viewed 3337 times

I experimented with the line:

$listview1.Items.Add($usernameresults)

But obviously as it was an experiment it didn't work.

My question is, how do I get that table above (which is put into variable $usernameresults) into a box in Sapien Powershell so that I can then mouse click which user I want to select and do the rest of the code from there? When I click select the username I want to feed the "NTAccountName" for the rest of my script.

I hope this makes sense... I thought ListView is the way but it may or may not be.

Thanks in advance.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Select-Object output into ListView?

Post by jvierra »

Try this to understand how to use a ListView: https://info.sapien.com/index.php/guis/ ... ew-control

To add items use the provided function:

Add-ListViewItem -ListView $listview1 -Items $usernameresults -Group $listview1.Groups[0] -SubItems ntaccountname, accountislockedout
thatsameer
Posts: 18
Last visit: Thu Apr 18, 2019 7:07 am

Re: Select-Object output into ListView?

Post by thatsameer »

Hi, Thank you for the very quick reply.

I have my ListView set to SmallIcon and checkbox on.

I added the line you mentioned below and it's getting there. I had to remove the

| select-object ntaccountname, accountislockedout

for the listview to display properly.

This is what it looks like (code behind)

Gettingthere.png
Gettingthere.png (53.04 KiB) Viewed 3317 times

It's so very close to what I wanted. I would really like to know how to create two columns "NTaccountname" and "Accountislockedout" populated with the information piped in from $usernameresults (just like in the screenshot of the first post).

The -Subitems ntaccountname, accountislockedout doesn't make a different to the current output in the listview.

I tried reading the link but either I missed it or I don't understand how to do it from reading that page.

This help is very much appreciated.

Thanks!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Select-Object output into ListView?

Post by jvierra »

Read the fist link I posted to find out what things are available. To have columns just define columns.
thatsameer
Posts: 18
Last visit: Thu Apr 18, 2019 7:07 am

Re: Select-Object output into ListView?

Post by thatsameer »

Thank you my friend! I got it working exactly how I wanted it by actually spending time reading the spotlight :D

I will post screenshots and what I did in a bit, in case anyone else needs some assistance...
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Select-Object output into ListView?

Post by jvierra »

Great. Working examples are always useful.
thatsameer
Posts: 18
Last visit: Thu Apr 18, 2019 7:07 am

Re: Select-Object output into ListView?

Post by thatsameer »

So to conclude this topic, this is what I did to make the above code work.

I read on the spotlight link mentioned above:
"In the Details view the Subitems will be displayed only if Columns are defined."

That was a critical piece I was missing from my form.

So I added 2 columns in Powershell studio, and modified the script line to read:

Add-ListViewItem -ListView $listview1 -Items $usernameresults.ntaccountname -Group $listview1.Groups[0] -SubItems $usernameresults.accountislockedout

I realised that "-Items" appends to the first defined column and "-Subitems" appends to the column after.

Thus resulting in the following:
SearchResultsWorking.png
SearchResultsWorking.png (6.1 KiB) Viewed 3212 times

Thanks for the assistance.
thatsameer
Posts: 18
Last visit: Thu Apr 18, 2019 7:07 am

Re: Select-Object output into ListView?

Post by thatsameer »

Hmm,

It seems I spoke to soon. I actually need more assistance.

It seems -Subitems only takes the first result and displays it in the column. It does not take all the rows into account. It only takes the first row and shows the whole column with the first row only.

What I mean by this -
If I add the line like this, so the first column shows the username and second column shows locked out status, it looks like this:
Add-ListViewItem -ListView $listview1 -Items $usernameresults.ntaccountname -Group $listview1.Groups[0] -SubItems $usernameresults.accountislockedout

Username.png
Username.png (32.65 KiB) Viewed 3186 times

Looks like everything is working... however, one of those users account is locked so it should return as "True".

How can I confirm this, I swapped -Items with -Subitems so the line now reads:
Add-ListViewItem -ListView $listview1 -Items $usernameresults.accountislockedout -Group $listview1.Groups[0] -SubItems $usernameresults.ntaccountname

UsernameinSubItem.png
UsernameinSubItem.png (32.05 KiB) Viewed 3186 times

As you can see, the first column (-Item) displays each row independently taken from the results and shows a "True" result. But the second column (-Subitems) shows only the first username from the result, copying the 1st line into all the lines in that column.

My question is, how can I get -Subitems to act like -Items? Each row displaying the result and not copying the first row to all rows?

I've read the spotlight many times but I just cant see what I'm doing wrong

After this is solved, my entire script will be complete!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Select-Object output into ListView?

Post by jvierra »

This is one reason why I never use the function. It is not well behaved and doesn't work as advertised.

Here is an example of how to use it in a way that works.
Attachments
Test-AddListItems.psf
(21.99 KiB) Downloaded 161 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Select-Object output into ListView?

Post by jvierra »

Here is a more complete demo of how to use a ListBox.
Attachments
Demo-ListViewIcons.psf
(22.84 KiB) Downloaded 181 times
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked