Created
July 8, 2019 15:21
-
-
Save masyanru/3f400dbc7932e4efcc6e1d5681866038 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## WARNING!! DO NOT MANUALLY EDIT THIS SCRIPT IN THE ADMINISTRATOR CONSOLE. | |
## IT WILL BREAK THE SCRIPT EXECUTION ON THE CLIENT IF IT IS A SIGNED SCRIPT. | |
<# | |
.Synopsis | |
Script used for application detection methods | |
.Notes | |
FileName: SoftwareDetectionScript.PS1 | |
Author: PatchMyPC | |
Contact: support@patchmypc.com | |
Created: 2019-07-03 | |
Updated: 2019-07-03 | |
License: Copyright Patch My PC, LLC all rights reserved | |
Version 1.0 | |
- Initial release | |
#> | |
#Set variables# | |
$AppToSearch = 'Adobe Flash Player*' # A pattern used to search for displayName in the uninstall registry key. | |
$AppToAvoid = '' # A pattern used to reject similar applications. | |
$AppMSICodeToSearch = '{01D02EF1-C348-4B64-BACF-860A69CBCADF}' # A MSI code used to search for in the uninstall registry key. | |
$AppVersionToSearch = '32.0.0.207' # Version >= check to determine if application is installed and greater than or equal to this version. | |
$ScriptLogFilePath = 'C:\Windows\Temp\PatchMyPC-SoftwareDetectionScript.log' | |
Function Set-LogPath | |
# Configures the full path to the log file depending on whether or not the CCM folder exists. | |
{ | |
$LogFile = 'PatchMyPC-SoftwareDetectionScript.log' | |
$LogPath = 'C:\Windows\Temp' | |
if(Test-Path -Path 'C:\Windows\CCM\Logs') | |
{ | |
$LogPath = 'C:\Windows\CCM\Logs' | |
} | |
$global:ScriptLogFilePath = "$LogPath\$LogFile" | |
} | |
Function Write-Log | |
#Write the log file if the global variable is set | |
{ | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string]$Message, | |
[Parameter()] | |
[ValidateSet(1, 2, 3)] | |
[string]$LogLevel=1 | |
) | |
$TimeGenerated = "$(Get-Date -Format HH:mm:ss).$((Get-Date).Millisecond)+000" | |
$Line = '<![LOG[{0}]LOG]!><time="{1}" date="{2}" component="{3}" context="" type="{4}" thread="" file="">' | |
$LineFormat = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), "$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)", $LogLevel | |
$Line = $Line -f $LineFormat | |
Add-Content -Value $Line -Path $global:ScriptLogFilePath | |
} | |
Function Clear-Log | |
# Delete the log file if bigger than 2mb | |
{ | |
param ( | |
[Parameter(Mandatory = $true)][string]$maxSize | |
) | |
try | |
{ | |
if(Test-Path -Path $global:ScriptLogFilePath) | |
{ | |
if ((Get-Item $global:ScriptLogFilePath).length -gt $maxSize) | |
{ | |
Remove-Item -Path $global:ScriptLogFilePath | |
Start-Sleep -Seconds 1 | |
} | |
} | |
} | |
catch {Write-Log -Message "Unable to delete log file."} | |
} | |
Function Get-InstalledSoftwares{ | |
$regpath = @('HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*') | |
if (-not ([IntPtr]::Size -eq 4)) | |
{ | |
$regpath += 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | |
} | |
Get-ItemProperty $regpath | .{process{if($_.DisplayName -and $_.UninstallString) { $_ } }} | Select-Object DisplayName, DisplayVersion, UninstallString, PSChildName, Publisher, InstallDate | Sort DisplayName | |
} | |
Function IsInstalled | |
{ | |
param([string]$appName, [string]$except, [string]$appVersion, [string]$msiCode) | |
$InstalledSoftwares = Get-InstalledSoftwares | Where-Object {(($_.DisplayName -like $appName) -and -not($_.DisplayName -like $except) -and ($_.DisplayVersion -ge $appVersion)) -or ($_.PSChildname -eq $msiCode)} #Search | |
If ($InstalledSoftwares -eq $null) # No match found for DisplayName and DisplayVersion check | |
{ | |
Write-Log -Message "No detection for $($appName) with version $($appVersion)" | |
Return $false | |
} | |
Else | |
{ | |
foreach($Software in $InstalledSoftwares) # Found match | |
{ | |
Write-Log -Message "Found $($Software.DisplayName) version $($Software.DisplayVersion) installed on $($Software.InstallDate)" -LogLevel 2 | |
} | |
Return $true | |
} | |
} | |
# Main program | |
Set-LogPath | |
Clear-Log 2mb | |
Write-Log -Message "*** Starting detection for $($AppToSearch) $(if($AppToAvoid -ne """") {"except $AppToAvoid"}) with version $($AppVersionToSearch)" | |
$detectionResult = IsInstalled $AppToSearch $AppToAvoid $AppVersionToSearch $AppMSICodeToSearch | |
if($detectionResult -eq $true) | |
{ | |
Write-Host 'Installed' # Used as output in SCCM for Installed detection method | |
} | |
Exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment