Our office runs an application that is constantly being updated. Unfortunately the program is not easy to update via Group Policy (GPO), so instead of walking to each computer to check if it needs an upgrade or if it is running multiple versions, I wrote this script to do the legwork for me. To modify this script simply change <ProgramVersions> <ProgramFolder> and <ProgramFile> to suit your needs. You will also need to modify the $LogFile variable to a path that exists on your own system. It can take some time to run, especially if you have a lot of systems to check. The end result is a log file that tells you where to find what you are looking for so you can work more efficiently.
# I like a tidy workspace
clear
# Errors happen, just move on. Comment this out if you want to see what broke.
$ErrorActionPreference = "silentlycontinue"
# Make a log,
$LogTime = Get-Date -Format "MM-dd-yyyy_hh.mm.ss"
$LogFile= 'C:\work\<ProgramVersions>.'+$logtime+'.txt'
$logtime | Out-File $LogFile
# Need this to discover what computers are on the network
Import-Module ActiveDirectory
$computer = (Get-ADComputer -Filter 'ObjectClass -eq "Computer"' | Select -Expand DNSHostName)
$drawline= "-----------------------------------------------------------------------------------------------------------------------"
# Get that list of computers and do something with it.
foreach ($computer1 in $computer) {
# Define what we are looking for
$path="\\$computer1\c$\<ProgramFolder>*\"
$filename="<Programfile>"
$FilePath=$path+$filename
# The program we are looking for may have multiple instances. Here's another possible location.
$path2="\\$computer1\c$\<ProgramFolder>*\"
$filename2="<Programfile>"
$FilePath2=$path2+$filename2
# Go find what we are looking for and log it.
if ((Get-ChildItem -Recurse $FilePath) -eq $null){
Out-Null
}
else {
$drawline | Tee-Object $LogFile -Append
$computer1 | Tee-Object $LogFile -Append
Get-ChildItem -Recurse $FilePath | foreach-object { "{0,-80}`t{1}" -f $_.FullName, [System.Diagnostics.FileVersionInfo]::GetVersionInfo($_).FileVersion } | Tee-Object $LogFile -Append
Get-ChildItem -Recurse $FilePath2 | foreach-object { "{0,-80}`t{1}" -f $_.FullName, [System.Diagnostics.FileVersionInfo]::GetVersionInfo($_).FileVersion } | Tee-Object $LogFile -Append
$drawline| Tee-Object $LogFile -Append
}
}
}
The way this works is pretty simple. We first ask Active Directory for a list of computers (this is why it's important to keep active directory tidy folks). Then the script goes around an asks every computer in that list to search it's file system to see if they have this program installed. In my case, I know that the program I am looking for is in multiple locations, so ask again for the other location(s). Then give me a report on what you found.
It is possible to modify this script to find the installed program file in the registry, even multiple instances, but then it will not find those programs that do not make registry entries. Also I wanted this script to be flexible enough that I could look for any file, not just programs.
I try to keep my code as simple and readable as possible. There is a difference between code and cryptogrophy. If you make suggestions to improve this code, please document it. It really helps, even the experienced.
This is a reprint of my original article on my blog xpertnotes.net. Click here to see the original article.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Author
Commented:This script was designed to find a specific file in specific locations, thereby limiting the amount of hard drive activity it causes on the end users computer. I run this once a month or so and have had no complaints.