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:

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 (6.72 KiB) Viewed 694 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.

Re: Select-Object output into ListView?

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 (53.04 KiB) Viewed 674 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!

Re: Select-Object output into ListView?

Posted: Fri Aug 03, 2018 9:13 am

by jvierra

Read the fist link I posted to find out what things are available. To have columns just define columns.

Re: Select-Object output into ListView?

Posted: Fri Aug 03, 2018 1:59 pm

by thatsameer

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

I will post screenshots and what I did in a bit, in case anyone else needs some assistance...

Re: Select-Object output into ListView?

Posted: Fri Aug 03, 2018 2:48 pm

by jvierra

Great. Working examples are always useful.

Re: Select-Object output into ListView?

Posted: Mon Aug 06, 2018 7:38 am

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:

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

Thus resulting in the following:

SearchResultsWorking.png (6.1 KiB) Viewed 569 times

Thanks for the assistance.

Re: Select-Object output into ListView?

Posted: Wed Aug 08, 2018 5:06 am

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 (32.65 KiB) Viewed 543 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 (32.05 KiB) Viewed 543 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!

Re: Select-Object output into ListView?

Posted: Wed Aug 08, 2018 5:34 am

by jvierra

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