Purpose:
You wish to understand how to effectively use the PowerShell Scanner in PDQ Inventory.
Resolution:
The PowerShell scanner is capable of running any PowerShell script on your target machines. These scripts will be copied to the target machine and run locally, and their output will be converted to a new table of data in PDQ Inventory. This is a powerful feature for gathering data that PDQ Inventory is otherwise unable to effectively scan for, but there are some important caveats to be aware of when designing scripts for the PowerShell Scanner as opposed to other more general PowerShell scripts. We advise reviewing the Custom Scanner Best Practices article and thoroughly testing scans on a few machines before scanning your entire environment.
The most prominent difference in designing a script for the PowerShell Scanner compared to most other PowerShell scripts is the importance of formatting the script's output. Any output from a PowerShell Scanner script will be returned to PDQ Inventory, so it is important to make sure that the output is formatted to only return the data you wish to see in PDQ Inventory. This is most easily accomplished by creating custom objects for your output using a PSCustomObject. In this example, three columns of data would be shown in PDQ Inventory. Multiple rows may be available if multiple objects of this type are returned.
Because the PowerShell scanner will create tables in PDQ Inventory based on the columns of data it receives, it is especially important to ensure that the columns being returned are consistent between computers. If the results of one computer's scan omit a column that was returned for another computer, the values of that column for the first scanned computer may be lost as that table adjusts to the new data format. It is important to always include a column even if the value may not apply to all computers to avoid data loss.
[PSCustomObject]@{
Column1 = $Value[0]
Column2 = $Value[1]
Column3 = $Value[2]
}
In addition to formatting your own output, it is important to be sure that any output from the cmdlets used in your scripts is suppressed. Some common PowerShell cmdlets such as Install-PackageProvider which may be helpful in your scripts return output of their own which will be displayed in PDQ Inventory if not properly suppressed. The simplest and most efficient way to suppress unwanted output is to redirect the output to a variable. The $null variable is not writable and can be used effectively drop output entirely without changing the behavior of the cmdlet being suppressed.
$null = Install-PackageProvider "Nuget" -Force
For more information on the creation and use of the PowerShell scanner, we have a short introductory video here:
Further examples of how to use the PowerShell Scanner and a collection of pre-built scanners can be found in our public GitHub repository.