Back to Basics: Use PowerShell to Search Servers for Specific Software
An old acquaintance, Mr. Rupert Torquil-Smythe Esq. (yes, it is he), recently wanted to know whether a certain piece of software was installed on a particular list of servers in his test lab.
I asked him for the list of servers (servers.txt) and the name of the software (Python). I then wrote and ran this:
$Servers = Get-Content -Path .\servers.txt
foreach ($Server in $Servers) {
$Found = (Get-WmiObject -ComputerName $Server -Query {select name from win32_product where name like '%Python%'} -ErrorAction SilentlyContinue).Name
if ($Found) {
Add-Content -Path .\output.txt -Value "================"
Add-Content -Path .\output.txt -Value "$Server"
Add-Content -Path .\output.txt -Value "================"
foreach ($Entry in $Found) {
Add-Content -Path .\output.txt -Value $Entry
}
Add-Content -Path .\output.txt -Value " "
}
}
We end up with a text file (output.txt) containing details of each server that has Python installed.
Things to note:
- servers.txt is read and stored as a variable ( $Servers)
- we use WMI to query each of the remote servers, from $Servers, in a foreach loop
- the query string is WQL
- we only ask for the name property to be returned (select name is more efficient than select * )
- % allows for a wildcard search, e.g. any number of characters before and after Python
- Win32reg_AddRemovePrograms rather than Win32_Product should be used in production environments