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.
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 ($Serverin$Servers) {
$Found= (Get-WmiObject-ComputerName$Server-Query {selectnamefromwin32_productwherenamelike'%Python%'} -ErrorActionSilentlyContinue).Name
if ($Found) {
Add-Content-Path.\output.txt-Value"================"
Add-Content-Path.\output.txt-Value"$Server"
Add-Content-Path.\output.txt-Value"================"
foreach ($Entryin$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