From 34deac14833399ac55e775f49e80d4152db3c645 Mon Sep 17 00:00:00 2001 From: Martien Date: Tue, 10 Feb 2026 11:42:31 +0100 Subject: [PATCH] add WMI fix script --- Misc/Fix-WMI-Windows2022.ps1 | 105 +++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 Misc/Fix-WMI-Windows2022.ps1 diff --git a/Misc/Fix-WMI-Windows2022.ps1 b/Misc/Fix-WMI-Windows2022.ps1 new file mode 100644 index 0000000..65eec2b --- /dev/null +++ b/Misc/Fix-WMI-Windows2022.ps1 @@ -0,0 +1,105 @@ +Write-Host "Running System File Checker (sfc /scannow)" +$DefaultProcessParam = @{ + 'Wait' = $true + 'ErrorAction' = 'Stop' +} +$ProcessParam = @{ + 'FilePath' = 'sfc' + 'ArgumentList' = '/scannow' +} +Start-Process @ProcessParam @DefaultProcessParam +Write-Host 'Stopping ConfigManager service if it exists' +Stop-Service -Force 'ccmexec' -ErrorAction 'SilentlyContinue' +Write-Host 'Stopping Windows Management Instrumentation service' +Stop-Service -Force 'winmgmt' +Write-Host 'Temporarily disabling Windows Management Instrumentation service' +Set-Service -Name 'winmgmt' -StartupType 'Disabled' +Write-Host 'Determining system architecture' +if ([System.Environment]::Is64BitOperatingSystem) { + $WbemBinaries = @( + "$($env:SystemRoot)\System32\wbem\unsecapp.exe" + "$($env:SystemRoot)\System32\wbem\WMIADAP.exe" + "$($env:SystemRoot)\System32\wbem\WMIApSrv.exe" + "$($env:SystemRoot)\System32\wbem\WmiPrvSE.exe" + "$($env:SystemRoot)\System32\wbem\scrcons.exe" + "$($env:SystemRoot)\SysWOW64\wbem\WMIADAP.exe" + "$($env:SystemRoot)\SysWOW64\wbem\WmiPrvSE.exe" + ) + $WbemPaths = @( + "$($env:SystemRoot)\System32\wbem" + "$($env:SystemRoot)\SysWOW64\wbem" + ) +} +else { + $WbemBinaries = @( + "$($env:SystemRoot)\System32\wbem\unsecapp.exe" + "$($env:SystemRoot)\System32\wbem\WMIADAP.exe" + "$($env:SystemRoot)\System32\wbem\WMIApSrv.exe" + "$($env:SystemRoot)\System32\wbem\WmiPrvSE.exe" + "$($env:SystemRoot)\System32\wbem\scrcons.exe" + ) + $WbemPaths = @( + "$($env:SystemRoot)\System32\wbem" + ) +} +Write-Host "Registering WBEM DLLs" +foreach ($WbemPath in $WbemPaths){ + $WmiObjects = Get-ChildItem -Path "$WbemPath\*" -Include '*.dll' + foreach ($WmiObject in $WmiObjects) { + Write-Host " Registering WBEM DLL $($WmiObject.FullName)" + $ProcessParam = @{ + 'FilePath' = 'regsvr32' + 'ArgumentList' = "/s `"$($WmiObject.FullName)`"" + } + Start-Process @ProcessParam @DefaultProcessParam + } +} +Write-Host "Registering WBEM EXEs" +foreach ($WbemBinary in $WbemBinaries) { + if (Test-Path -Path "$WbemPath\$WbemBinary") { + $CurrentBin = Get-Item -Path "$WbemPath\$WbemBinary" + Write-Host " Registering $WbemBinary" + $ProcessParam = @{ + 'FilePath' = $CurrentBin.FullName + 'ArgumentList' = '/RegServer' + } + Start-Process @ProcessParam @DefaultProcessParam + } + else { + Write-Warning "File $WbemBinary not found in $WbemPath!" + } +} +Write-Host "Registering WMI Managed Objects" +$FileExtensions = @( + '*.mof' + '*.mfl' +) +foreach ($WbemPath in $WbemPaths){ + $WbemDlls = Get-ChildItem -Path "$WbemPath\*" -Include $FileExtensions + foreach ($WbemDll in $WbemDlls) { + Write-Host " Registering WBEM DLL $($WbemDll.FullName)" + $ProcessParam = @{ + 'FilePath' = "$WbemPath\mofcomp.exe" + 'ArgumentList' = $WbemDll.FullName + } + Start-Process @ProcessParam @DefaultProcessParam + } +} +Write-Host "Resetting Repository" +$ProcessParam = @{ + 'FilePath' = "$($env:SystemRoot)\System32\wbem\winmgmt.exe" + 'ArgumentList' = '/resetrepository' +} +Start-Process @ProcessParam @DefaultProcessParam +Write-Host "Salvaging Repository" +$ProcessParam = @{ + 'FilePath' = "$($env:SystemRoot)\System32\wbem\winmgmt.exe" + 'ArgumentList' = '/salvagerepository' +} +Start-Process @ProcessParam @DefaultProcessParam +Write-Host 'Setting Windows Management Instrumentation service back to Automatic' +Set-Service -Name 'winmgmt' -StartupType 'Automatic' +Write-Host 'Starting Windows Management Instrumentation service' +Start-Service -Force winmgmt +Write-Host 'Starting ConfigManager service if it exists' +Start-Service -Force ccmexec -ErrorAction SilentlyContinue \ No newline at end of file