Advertisement
Guest User

Untitled

a guest
Jul 9th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. $logFile = "C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\EPMregistration.txt"
  2.  
  3. function Write-Log {
  4. param (
  5. [string]$Message,
  6. [string]$Level = "INFO"
  7. )
  8. $timestamp = Get-Date -Format "dd-MM-yyyy HH:mm:ss"
  9. $logMessage = "$timestamp [$Level] $Message"
  10. Add-Content -Path $logFile -Value $logMessage
  11. Write-Host $logMessage
  12. }
  13.  
  14. function Check-EPMStatus {
  15. $appxPackage = Get-AppxPackage -Name "Microsoft.EpmShellExtension"
  16. $service = Get-Service -Name "MEMEPMSvc" -ErrorAction SilentlyContinue
  17. $dllPath = "C:\Program Files\Microsoft EPM Agent\EPMShellExtension\EpmShellExtension.dll"
  18.  
  19. if ($null -eq $appxPackage) {
  20. Write-Log "EPM Shell Extension is not installed." -Level "ERROR"
  21. return $false
  22. }
  23. Write-Log "EPM Shell Extension is installed." -Level "INFO"
  24.  
  25. if ($null -eq $service) {
  26. Write-Log "MEMEPMSvc service is not installed." -Level "ERROR"
  27. return $false
  28. }
  29. Write-Log "MEMEPMSvc service is installed." -Level "INFO"
  30.  
  31. if (-not (Test-Path $dllPath)) {
  32. Write-Log "EpmShellExtension.dll not found at expected location: $dllPath" -Level "ERROR"
  33. return $false
  34. }
  35. Write-Log "EpmShellExtension.dll is present at the expected location." -Level "INFO"
  36.  
  37. Write-Log "All required components are present. Proceeding with registry modifications." -Level "INFO"
  38. return $true
  39. }
  40.  
  41. # Check EPM status before proceeding
  42. if (-not (Check-EPMStatus)) {
  43. Write-Log "Exiting script due to missing EPM components." -Level "ERROR"
  44. exit
  45. }
  46.  
  47. # Mount the HKCR drive
  48. try {
  49. New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT -ErrorAction Stop | Out-Null
  50. Write-Log "HKCR drive mounted successfully." -Level "INFO"
  51. } catch {
  52. Write-Log "Failed to mount HKCR drive: $($_.Exception.Message)" -Level "ERROR"
  53. exit
  54. }
  55.  
  56. # Function to check and add registry key/value
  57. function Ensure-RegistryValue {
  58. param (
  59. [string]$Path,
  60. [string]$Name,
  61. [object]$Value,
  62. [string]$Type
  63. )
  64.  
  65. try {
  66. if (!(Test-Path $Path)) {
  67. New-Item -Path $Path -Force | Out-Null
  68. }
  69.  
  70. $existingValue = Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue
  71.  
  72. if ($null -eq $existingValue -or $existingValue.$Name -ne $Value) {
  73. Set-ItemProperty -Path $Path -Name $Name -Value $Value -Type $Type -Force
  74. Write-Log "Added/Updated: $Path\$Name" -Level "INFO"
  75. } else {
  76. Write-Log "Already exists and correct: $Path\$Name" -Level "INFO"
  77. }
  78.  
  79. # Verify the value after setting
  80. $verifiedValue = Get-ItemPropertyValue -Path $Path -Name $Name -ErrorAction Stop
  81. if ($verifiedValue -ne $Value) {
  82. throw "Verification failed: $Path\$Name. Expected '$Value', but got '$verifiedValue'"
  83. }
  84. }
  85. catch {
  86. Write-Log "Error processing $Path\$Name`: $($_.Exception.Message)" -Level "ERROR"
  87. }
  88. }
  89.  
  90. # Array of registry entries to check/add
  91. $registryEntries = @(
  92. @{Path="HKCR:\CLSID\{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Name="(Default)"; Value=""; Type="String"},
  93. @{Path="HKCR:\CLSID\{02D0A1D6-0F3A-4534-BE54-5178737324E0}\InprocServer32"; Name="(Default)"; Value="C:\Program Files\Microsoft EPM Agent\EPMShellExtension\EpmShellExtension.dll"; Type="String"},
  94. @{Path="HKCR:\CLSID\{02D0A1D6-0F3A-4534-BE54-5178737324E0}\InprocServer32"; Name="ThreadingModel"; Value="Apartment"; Type="String"},
  95. @{Path="HKCR:\exefile\shellex\ContextMenuHandlers\MEMEPMShExt"; Name="(Default)"; Value="{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Type="String"},
  96. @{Path="HKCR:\PackagedCom\ClassIndex\{02D0A1D6-0F3A-4534-BE54-5178737324E0}\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe"; Name="(Default)"; Value=""; Type="String"},
  97. @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Class\{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Name="ServerId"; Value=0; Type="DWord"},
  98. @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Class\{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Name="DllPath"; Value="EpmShellExtension.dll"; Type="String"},
  99. @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Class\{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Name="Threading"; Value=0; Type="DWord"},
  100. @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="ApplicationId"; Value="EpmShellIdentity"; Type="String"},
  101. @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="ApplicationDisplayName"; Value="EpmShellIdentity"; Type="String"},
  102. @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="DisplayName"; Value="Microsoft EPM Agent"; Type="String"},
  103. @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="SurrogateAppId"; Value="{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Type="String"},
  104. @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\ClassIndex\{02D0A1D6-0F3A-4534-BE54-5178737324E0}\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe"; Name="(Default)"; Value=""; Type="String"},
  105. @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Class\{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Name="ServerId"; Value=0; Type="DWord"},
  106. @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Class\{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Name="DllPath"; Value="EpmShellExtension.dll"; Type="String"},
  107. @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Class\{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Name="Threading"; Value=0; Type="DWord"},
  108. @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="ApplicationId"; Value="EpmShellIdentity"; Type="String"},
  109. @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="ApplicationDisplayName"; Value="EpmShellIdentity"; Type="String"},
  110. @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="DisplayName"; Value="Microsoft EPM Agent"; Type="String"},
  111. @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="SurrogateAppId"; Value="{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Type="String"}
  112. )
  113.  
  114. # Process each registry entry
  115. foreach ($entry in $registryEntries) {
  116. Ensure-RegistryValue @entry
  117. }
  118.  
  119. # Final verification
  120. $allSuccessful = $true
  121. foreach ($entry in $registryEntries) {
  122. try {
  123. $value = Get-ItemPropertyValue -Path $entry.Path -Name $entry.Name -ErrorAction Stop
  124. if ($value -eq $entry.Value) {
  125. Write-Log "Verified: $($entry.Path)\$($entry.Name)" -Level "INFO"
  126. } else {
  127. Write-Log "Verification failed: $($entry.Path)\$($entry.Name). Expected '$($entry.Value)', but got '$value'" -Level "ERROR"
  128. $allSuccessful = $false
  129. }
  130. }
  131. catch {
  132. Write-Log "Verification failed: $($entry.Path)\$($entry.Name). Error: $($_.Exception.Message)" -Level "ERROR"
  133. $allSuccessful = $false
  134. }
  135. }
  136.  
  137. if ($allSuccessful) {
  138. Write-Log "All registry entries were successfully added and verified." -Level "INFO"
  139. } else {
  140. Write-Log "Some registry entries could not be added or verified. Please check the log for details." -Level "WARN"
  141. }
  142.  
  143. # Remove the HKCR drive
  144. Remove-PSDrive -Name HKCR
  145. Write-Log "HKCR drive removed." -Level "INFO"
  146.  
  147. Write-Log "Script execution completed." -Level "INFO"
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement