Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. ## WARNING!! DO NOT MANUALLY EDIT THIS SCRIPT IN THE ADMINISTRATOR CONSOLE.
  2. ## IT WILL BREAK THE SCRIPT EXECUTION ON THE CLIENT IF IT IS A SIGNED SCRIPT.
  3.  
  4. <#
  5. .Synopsis
  6. Script used for application detection methods
  7. .Notes
  8. FileName: SoftwareDetectionScript.PS1
  9. Author: PatchMyPC
  10. Contact: support@patchmypc.com
  11. Created: 2019-07-03
  12. Updated: 2019-07-03
  13. License: Copyright Patch My PC, LLC all rights reserved
  14.  
  15. Version 1.0
  16. - Initial release
  17. #>
  18.  
  19. #Set variables#
  20. $AppToSearch = 'Adobe Flash Player*' # A pattern used to search for displayName in the uninstall registry key.
  21. $AppToAvoid = '' # A pattern used to reject similar applications.
  22. $AppMSICodeToSearch = '{01D02EF1-C348-4B64-BACF-860A69CBCADF}' # A MSI code used to search for in the uninstall registry key.
  23. $AppVersionToSearch = '32.0.0.207' # Version >= check to determine if application is installed and greater than or equal to this version.
  24.  
  25. $ScriptLogFilePath = 'C:\Windows\Temp\PatchMyPC-SoftwareDetectionScript.log'
  26.  
  27. Function Set-LogPath
  28. # Configures the full path to the log file depending on whether or not the CCM folder exists.
  29. {
  30. $LogFile = 'PatchMyPC-SoftwareDetectionScript.log'
  31. $LogPath = 'C:\Windows\Temp'
  32. if(Test-Path -Path 'C:\Windows\CCM\Logs')
  33. {
  34. $LogPath = 'C:\Windows\CCM\Logs'
  35. }
  36. $global:ScriptLogFilePath = "$LogPath\$LogFile"
  37. }
  38. Function Write-Log
  39. #Write the log file if the global variable is set
  40. {
  41. param (
  42. [Parameter(Mandatory = $true)]
  43. [string]$Message,
  44. [Parameter()]
  45. [ValidateSet(1, 2, 3)]
  46. [string]$LogLevel=1
  47. )
  48.  
  49. $TimeGenerated = "$(Get-Date -Format HH:mm:ss).$((Get-Date).Millisecond)+000"
  50. $Line = '<![LOG[{0}]LOG]!><time="{1}" date="{2}" component="{3}" context="" type="{4}" thread="" file="">'
  51. $LineFormat = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), "$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)", $LogLevel
  52. $Line = $Line -f $LineFormat
  53. Add-Content -Value $Line -Path $global:ScriptLogFilePath
  54. }
  55.  
  56. Function Clear-Log
  57. # Delete the log file if bigger than 2mb
  58. {
  59. param (
  60. [Parameter(Mandatory = $true)][string]$maxSize
  61. )
  62. try
  63. {
  64. if(Test-Path -Path $global:ScriptLogFilePath)
  65. {
  66. if ((Get-Item $global:ScriptLogFilePath).length -gt $maxSize)
  67. {
  68. Remove-Item -Path $global:ScriptLogFilePath
  69. Start-Sleep -Seconds 1
  70. }
  71. }
  72. }
  73. catch {Write-Log -Message "Unable to delete log file."}
  74. }
  75.  
  76. Function Get-InstalledSoftwares{
  77. $regpath = @('HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*')
  78.  
  79. if (-not ([IntPtr]::Size -eq 4))
  80. {
  81. $regpath += 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
  82. }
  83. Get-ItemProperty $regpath | .{process{if($_.DisplayName -and $_.UninstallString) { $_ } }} | Select-Object DisplayName, DisplayVersion, UninstallString, PSChildName, Publisher, InstallDate | Sort DisplayName
  84. }
  85.  
  86. Function IsInstalled
  87. {
  88. param([string]$appName, [string]$except, [string]$appVersion, [string]$msiCode)
  89.  
  90. $InstalledSoftwares = Get-InstalledSoftwares | Where-Object {(($_.DisplayName -like $appName) -and -not($_.DisplayName -like $except) -and ($_.DisplayVersion -ge $appVersion)) -or ($_.PSChildname -eq $msiCode)} #Search
  91. If ($InstalledSoftwares -eq $null) # No match found for DisplayName and DisplayVersion check
  92. {
  93. Write-Log -Message "No detection for $($appName) with version $($appVersion)"
  94. Return $false
  95. }
  96. Else
  97. {
  98. foreach($Software in $InstalledSoftwares) # Found match
  99. {
  100. Write-Log -Message "Found $($Software.DisplayName) version $($Software.DisplayVersion) installed on $($Software.InstallDate)" -LogLevel 2
  101. }
  102. Return $true
  103. }
  104. }
  105.  
  106. # Main program
  107. Set-LogPath
  108. Clear-Log 2mb
  109. Write-Log -Message "*** Starting detection for $($AppToSearch) $(if($AppToAvoid -ne """") {"except $AppToAvoid"}) with version $($AppVersionToSearch)"
  110.  
  111. $detectionResult = IsInstalled $AppToSearch $AppToAvoid $AppVersionToSearch $AppMSICodeToSearch
  112.  
  113. if($detectionResult -eq $true)
  114. {
  115. Write-Host 'Installed' # Used as output in SCCM for Installed detection method
  116. }
  117. Exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement