My first attempt at a GUI to select objects properties demonstrated that I didn’t have a firm grasp on how to pipeline an object through an advanced function. The problem I had at the time was not understanding how to have the form only appear once. Why is that difficult? Because the Begin Block won’t accept an variable created as an argument to the function. If you put the form block into the Process Block, you get it popping up once for each item in the pipeline. The desired result is to run the form just once. The solution in my new version of Select-PropertyForm.ps1 is to create a variable to cause the form to be created only one time:
Begin{$FirstRun=$True} Process{ if ($firstRun){ # ... Form created here for first item only $firstRun = $False } #If they choose nothing or close, return all if ($Script:NewList[0].Count -eq 0){ If ($ExitOnCancel) {Break}ELSE{$InputObject} }ELSE{ $InputObject | select $Script:NewList } } End{} } #End Function
Note from above that $script:NewList is a list of the selected properties. The selection form looks just like the previous version:
.
I have added a parameter to the function to set the title. This has been changed to a full advanced function. You must include it in your own code or “dot source” it to run.
An example:
Get-aduser -Filter ('sn -eq "Smith"') -properties * | Select-PropertiesForm | Convert-ADValues | Export-CSV -NoTypeInformation "$env:userprofile\desktop\Smiths.csv"
This user the Get-User AD cmdlet to get all users with the last name of “Smith”, returning AD properties. I then pipe to Convert-ADValues to ensure that dates and other values export okay, send results to CSV file. The output for this is Selected.Microsoft.ActiveDirectory.Management.ADUser