Many of my scripts are written for other people to do Active Directory reporting. One of my goals in supporting them is to give them as much flexibility in the data returned with the minimum amount of custom coding required. Convert-ADValues (which I will update soon), outputs friendly dates and expanded information for certificates, groups and the object’s security. I have also published a Windows forms based GUI to select object properties.
This script, Select-ItemsWithListBoxes.ps1, is an WPF advanced function which for selecting items. I designed the form using the method described in Practical Lessons on WPF Forms and PowerShell.
A simple example of this function presents a list of ASCII characters as items to select, created with this command:
#Get list of characters. $list = (33..126 | ForEach-Object{[char][byte]$_}) # Note that with a simple list you cannot use the pipeline. Select-ItemswithListboxes -inputobject $list
The result is form to select a list of the characters. On “Accept” the items in the right column are returned.
Here we select properties from Get-ADUser, splatting the parameters:
#Get all users with all properties, send to function to select properties. Splatting params $params = @{ Filter = '*' properties = '*' SearchScope = 'Subtree' } #Here you can use the pipeline get-aduser @params | Select-ItemsWithListBoxes -SelectProperties
This presents the properties from the object in the pipeline. When the user accepts the list, the items in the right column are used to filter the properties of the object in the pipeline. In this example, the pipeline would contain user objects with the Name, Description and Office Phone properties.
Note the difference in the two examples. When you use the script to select a list of items, you cannot use the pipeline. Create the list and set it as the argument for the inputobject variable.
The script has the current version of my Dot Source Reminder for advanced functions. In my experience, new PowerShell users often try to run advanced and find that noting happens when they are loaded. I add this bit of code at the end of my advanced functions so that when they are executed, you get a notification like this:
You have loaded the advanced function "Select-ItemsWithListBoxes" Use "Get-Help" for information and usage, Ex: PS C:\> Get-Help Select-ItemsWithListBoxes -detailed
The downside of the notification is that you need to hide it when manually loading the functions. Here is a generalized AD reporting script which lets the user select any value from AD for reporting:
#Load the scripts. Redirect output to null to hide the dot source reminder . "C:\PowerShellScripts\Select-ItemsWithListBoxes.ps1" |out-null . "C:\PowerShellScripts\Convert-ADValues.ps1" |out-null $params = @{ Filter = '*' properties = '*' SearchScope = 'Subtree' } get-aduser @params | Select-ItemsWithListBoxes -SelectProperties | Convert-ADValues | Export-CSV -Path $env:userprofile\desktop\Example.csv -NoTypeInformation
I first presented this script at the January meeting of the Charlotte Powershell User’s Group. Find (or start) a PowerShell user group. In my experience, it has been a very worthwhile experience.