Scripting Question

Powershell - compliance detection for multiple locations

08/21/2017 1409 views
Hi All,
I don't like asking too much, I prefer finding the answers myself, but I need to detect the presence of a file in multiple locations for an SCCM Assets and Compliance reason.  I am new to Powershell and still learning. 
The script below is suppose to be looking for a file in two variable locations.  I have tried many ways of actioning this.
The script must not echo anything if the $FVer is greater than the file being found.
if the file is not present, or the file version is greater than the $FVer, write-echo "something"
I also have tried nesting ForEach statements, but I didn't know how to output the response from nested statements to the bottom as a collective answer. 
Could some please help me to rewrite this to search both locations and provide a write-echo response if the conditions are met.
Please ask questions if I have missed something

$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)
{ }
Write-host "Compliance version Installed"
Thanks in advance

Answer Summary:
0 Comments   [ + ] Show comments


Answer Chosen by the Author


$FVer = "20"

$SearchName = "FileName.exe"

$SearchLocations =@("C:\Users","C:\Datapath")

$FoundFileList =@()

#get list of files matching SearchName


$FoundFileList += Get-ChildItem -Path $_ -Recurse -Include $SearchName


#Add a compliance property for each file


if($_.VersionInfo.FileVersion -lt $FVer){

$_ |Add-Member -MemberType NoteProperty -Name Compliance -Value "NonCompliant"



$_ |Add-Member -MemberType NoteProperty -Name Compliance -Value "Compliant"



#display path and compliance to screen

$FoundFileList |select -Property FullName, Compliance

Answered 09/08/2017 by: Thorvin
Senior White Belt

  • 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.
    • 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.
  • with some minor modification, was able to use this script.
    Thanks Thorvin
    • 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.

All Answers

This content is currently hidden from public view.
Reason: Removed by member request For more information, visit our FAQ's.
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ