Powershell - compliance detection for multiple locations
$FVer = "20"
$users = @(Get-ChildItem -Path "C:\Users")
$users += @(Get-ChildItem -Path "C:\datapath") #any directory from here down
$users | ForEach {
$counter = Get-ChildItem -Path "C:\Users\$($_.Name)\AppData\Local" -Recurse -Include "Filename.exe" -ea 0 | Measure-Object | Select-Object -ExpandProperty Count
If ($counter -gt 0)
{
$fileLoc = @(Get-ChildItem -Path "C:\Users\$($_.Name)\AppData\Local" -Recurse -Include "Filename.exe")
$fileLoc += @(Get-ChildItem -Path "C:\datapath" -Recurse -Include "Filename.exe")
$SysFileVer = [system.Diagnostics.FileVersionInfo]::GetVersionInfo($fileLoc).FileVersion
if ($SysFileVer -lt $Fver)
{ }
else
{
Write-host "Compliance version Installed"
}}}
Answers (1)
Top Answer
$FVer = "20"
$SearchName = "FileName.exe"
$SearchLocations =@("C:\Users","C:\Datapath")
$FoundFileList =@()
#get list of files matching SearchName
$SearchLocations|foreach{
$FoundFileList += Get-ChildItem -Path $_ -Recurse -Include $SearchName
}
#Add a compliance property for each file
$FoundFileList|foreach{
if($_.VersionInfo.FileVersion -lt $FVer){
$_ |Add-Member -MemberType NoteProperty -Name Compliance -Value "NonCompliant"
}
else{
$_ |Add-Member -MemberType NoteProperty -Name Compliance -Value "Compliant"
}
}
#display path and compliance to screen
$FoundFileList |select -Property FullName, Compliance
Comments:
-
I added a property and then displayed the total list at the end. This makes it easy to do something like pipe to export-csv or out-gridview.
I had it all tabbed out for readability, but the site stripped that. - Thorvin 7 years ago-
Hi Thorvin,
I will have a look at it today. It looks better than the script I managed to do up after the one in my question.
I have already quickly ran it up in ISE, and it looks good.
Thanks - Tempril 7 years ago
-
with some minor modification, was able to use this script.
Thanks Thorvin - Tempril 7 years ago-
Awesome, when I re-wrote it, I also restructured it slightly (like putting the search locations into a single variable at the top) to make it easier to adjust.
Also if you don't need it, you can omit the else statement which might help performance depending on the amount of data you are weeding through. - Thorvin 7 years ago