Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $logFile = "C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\EPMregistration.txt"
- function Write-Log {
- param (
- [string]$Message,
- [string]$Level = "INFO"
- )
- $timestamp = Get-Date -Format "dd-MM-yyyy HH:mm:ss"
- $logMessage = "$timestamp [$Level] $Message"
- Add-Content -Path $logFile -Value $logMessage
- Write-Host $logMessage
- }
- function Check-EPMStatus {
- $appxPackage = Get-AppxPackage -Name "Microsoft.EpmShellExtension"
- $service = Get-Service -Name "MEMEPMSvc" -ErrorAction SilentlyContinue
- $dllPath = "C:\Program Files\Microsoft EPM Agent\EPMShellExtension\EpmShellExtension.dll"
- if ($null -eq $appxPackage) {
- Write-Log "EPM Shell Extension is not installed." -Level "ERROR"
- return $false
- }
- Write-Log "EPM Shell Extension is installed." -Level "INFO"
- if ($null -eq $service) {
- Write-Log "MEMEPMSvc service is not installed." -Level "ERROR"
- return $false
- }
- Write-Log "MEMEPMSvc service is installed." -Level "INFO"
- if (-not (Test-Path $dllPath)) {
- Write-Log "EpmShellExtension.dll not found at expected location: $dllPath" -Level "ERROR"
- return $false
- }
- Write-Log "EpmShellExtension.dll is present at the expected location." -Level "INFO"
- Write-Log "All required components are present. Proceeding with registry modifications." -Level "INFO"
- return $true
- }
- # Check EPM status before proceeding
- if (-not (Check-EPMStatus)) {
- Write-Log "Exiting script due to missing EPM components." -Level "ERROR"
- exit
- }
- # Mount the HKCR drive
- try {
- New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT -ErrorAction Stop | Out-Null
- Write-Log "HKCR drive mounted successfully." -Level "INFO"
- } catch {
- Write-Log "Failed to mount HKCR drive: $($_.Exception.Message)" -Level "ERROR"
- exit
- }
- # Function to check and add registry key/value
- function Ensure-RegistryValue {
- param (
- [string]$Path,
- [string]$Name,
- [object]$Value,
- [string]$Type
- )
- try {
- if (!(Test-Path $Path)) {
- New-Item -Path $Path -Force | Out-Null
- }
- $existingValue = Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue
- if ($null -eq $existingValue -or $existingValue.$Name -ne $Value) {
- Set-ItemProperty -Path $Path -Name $Name -Value $Value -Type $Type -Force
- Write-Log "Added/Updated: $Path\$Name" -Level "INFO"
- } else {
- Write-Log "Already exists and correct: $Path\$Name" -Level "INFO"
- }
- # Verify the value after setting
- $verifiedValue = Get-ItemPropertyValue -Path $Path -Name $Name -ErrorAction Stop
- if ($verifiedValue -ne $Value) {
- throw "Verification failed: $Path\$Name. Expected '$Value', but got '$verifiedValue'"
- }
- }
- catch {
- Write-Log "Error processing $Path\$Name`: $($_.Exception.Message)" -Level "ERROR"
- }
- }
- # Array of registry entries to check/add
- $registryEntries = @(
- @{Path="HKCR:\CLSID\{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Name="(Default)"; Value=""; Type="String"},
- @{Path="HKCR:\CLSID\{02D0A1D6-0F3A-4534-BE54-5178737324E0}\InprocServer32"; Name="(Default)"; Value="C:\Program Files\Microsoft EPM Agent\EPMShellExtension\EpmShellExtension.dll"; Type="String"},
- @{Path="HKCR:\CLSID\{02D0A1D6-0F3A-4534-BE54-5178737324E0}\InprocServer32"; Name="ThreadingModel"; Value="Apartment"; Type="String"},
- @{Path="HKCR:\exefile\shellex\ContextMenuHandlers\MEMEPMShExt"; Name="(Default)"; Value="{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Type="String"},
- @{Path="HKCR:\PackagedCom\ClassIndex\{02D0A1D6-0F3A-4534-BE54-5178737324E0}\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe"; Name="(Default)"; Value=""; Type="String"},
- @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Class\{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Name="ServerId"; Value=0; Type="DWord"},
- @{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"},
- @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Class\{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Name="Threading"; Value=0; Type="DWord"},
- @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="ApplicationId"; Value="EpmShellIdentity"; Type="String"},
- @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="ApplicationDisplayName"; Value="EpmShellIdentity"; Type="String"},
- @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="DisplayName"; Value="Microsoft EPM Agent"; Type="String"},
- @{Path="HKCR:\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="SurrogateAppId"; Value="{02D0A1D6-0F3A-4534-BE54-5178737324E0}"; Type="String"},
- @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\ClassIndex\{02D0A1D6-0F3A-4534-BE54-5178737324E0}\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe"; Name="(Default)"; Value=""; Type="String"},
- @{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"},
- @{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"},
- @{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"},
- @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="ApplicationId"; Value="EpmShellIdentity"; Type="String"},
- @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="ApplicationDisplayName"; Value="EpmShellIdentity"; Type="String"},
- @{Path="HKLM:\SOFTWARE\Classes\PackagedCom\Package\Microsoft.EpmShellExtension_1.0.0.0_neutral__8wekyb3d8bbwe\Server\0"; Name="DisplayName"; Value="Microsoft EPM Agent"; Type="String"},
- @{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"}
- )
- # Process each registry entry
- foreach ($entry in $registryEntries) {
- Ensure-RegistryValue @entry
- }
- # Final verification
- $allSuccessful = $true
- foreach ($entry in $registryEntries) {
- try {
- $value = Get-ItemPropertyValue -Path $entry.Path -Name $entry.Name -ErrorAction Stop
- if ($value -eq $entry.Value) {
- Write-Log "Verified: $($entry.Path)\$($entry.Name)" -Level "INFO"
- } else {
- Write-Log "Verification failed: $($entry.Path)\$($entry.Name). Expected '$($entry.Value)', but got '$value'" -Level "ERROR"
- $allSuccessful = $false
- }
- }
- catch {
- Write-Log "Verification failed: $($entry.Path)\$($entry.Name). Error: $($_.Exception.Message)" -Level "ERROR"
- $allSuccessful = $false
- }
- }
- if ($allSuccessful) {
- Write-Log "All registry entries were successfully added and verified." -Level "INFO"
- } else {
- Write-Log "Some registry entries could not be added or verified. Please check the log for details." -Level "WARN"
- }
- # Remove the HKCR drive
- Remove-PSDrive -Name HKCR
- Write-Log "HKCR drive removed." -Level "INFO"
- Write-Log "Script execution completed." -Level "INFO"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement