/build/static/layout/Breadcrumb_cap_w.png

Powershell - compliance detection for multiple locations

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


0 Comments   [ + ] Show comments

Answers (1)

Answer Summary:
Posted by: Thorvin 7 years ago
Senior White Belt
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

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

View more:

Share

 
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