2023-01-18 12:56:50 -05:00
|
|
|
# Guard to exit script if WMI does not appear to be installed at all
|
2023-10-03 16:23:51 -04:00
|
|
|
If ( !(Test-Path "${Env:SystemRoot}\System32\wbem") ) { Write-Error "WMI components are not present on this device. Please install them manually." ; Exit 1 }
|
2023-01-18 12:56:50 -05:00
|
|
|
|
|
|
|
|
# Function to test if WMI is functioning correctly
|
2023-10-03 16:23:51 -04:00
|
|
|
Function Test-WMIFunctionality {
|
2023-01-18 12:56:50 -05:00
|
|
|
# Track the number of failed functionality tests
|
|
|
|
|
$WMIErrorLevel = 0
|
|
|
|
|
|
|
|
|
|
# Specify the WMI class name to query
|
|
|
|
|
$Class = 'Win32_OperatingSystem'
|
|
|
|
|
|
|
|
|
|
# Specify a property in the WMI class that we can test for a value
|
|
|
|
|
$Property = 'LastBootUpTime'
|
|
|
|
|
|
|
|
|
|
# Test that our property has a value, and that the WBEM folder exists.
|
2023-10-03 16:23:51 -04:00
|
|
|
If ( $null -eq $(Get-CimInstance -ClassName $Class -ErrorAction SilentlyContinue | Select-Object $Property -ErrorAction SilentlyContinue).$Property )
|
2023-01-18 12:56:50 -05:00
|
|
|
{ Write-Error "Failed to query ${Class} for the value of ${Property}" ; $WMIErrorLevel++ }
|
2023-03-14 21:58:20 -04:00
|
|
|
|
2023-01-18 12:56:50 -05:00
|
|
|
# Test that the WMI repository is consistent
|
|
|
|
|
$WMIRepoStatus = (winmgmt.exe /verifyrepository)
|
|
|
|
|
If ( ($WMIRepoStatus -contains "inconsistent") -or ($WMIRepoStatus -contains "failed" ) )
|
|
|
|
|
{ Write-Error "WMI repository is in an inconsistent state" ; $WMIErrorLevel++ }
|
|
|
|
|
|
|
|
|
|
If ( $WMIErrorLevel -eq 0 ) { Return $true } Else { Return $false }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Guard to exit script if no repair is necessary
|
2023-10-03 16:23:51 -04:00
|
|
|
If ( Test-WMIFunctionality ) { Write-Output "WMI components are functioning correctly" ; Exit 0 }
|
2023-01-18 12:56:50 -05:00
|
|
|
|
|
|
|
|
# Attempt to salvage WMI repository and exit script if no further repair is necessary
|
2023-10-03 16:23:51 -04:00
|
|
|
Write-Output "Attempting to salvage WMI repository"
|
2023-01-18 12:56:50 -05:00
|
|
|
$WMIRepoSalvage = (winmgmt.exe /salvagerepository)
|
2023-10-03 16:23:51 -04:00
|
|
|
If ( ($WMIRepoSalvage -notcontains "failed") -and (Test-WMIFunctionality) ) { Write-Output "WMI repository successfully salvaged" ; Exit 0 }
|
2023-01-18 12:56:50 -05:00
|
|
|
|
|
|
|
|
Try {
|
2023-10-03 16:23:51 -04:00
|
|
|
Write-Output "Failed to salvage WMI repository, attempting manual reinitialization of WMI"
|
2023-01-18 12:56:50 -05:00
|
|
|
|
|
|
|
|
# Disable and stop the winmgmt service
|
|
|
|
|
Write-Output "Stopping and disabling WMI service"
|
|
|
|
|
Set-Service -Name winmgmt -StartupType Disabled
|
|
|
|
|
Stop-Service -Name winmgmt -Force
|
|
|
|
|
|
|
|
|
|
# Set the WBEM directory for manual refresh of WMI components
|
|
|
|
|
$TargetDir = "${Env:WinDir}\System32\wbem"
|
|
|
|
|
|
2023-10-03 16:23:51 -04:00
|
|
|
# Rename the Repository directory to Repo_backup
|
|
|
|
|
If ( Test-Path "${TargetDir}\Repo_backup" ) { Remove-Item "${TargetDir}\Repo_backup" -Recurse -Force -ErrorAction SilentlyContinue }
|
|
|
|
|
If ( Test-Path "${TargetDir}\Repository" ) { Rename-Item -Path "${TargetDir}\Repository" -NewName "${TargetDir}\Repo_backup" -Force }
|
2023-01-18 12:56:50 -05:00
|
|
|
|
|
|
|
|
# Register all *.dll files
|
|
|
|
|
Write-Output "Registering all DLL files in ""${TargetDir}"""
|
|
|
|
|
$WMIDlls = Get-ChildItem -Path "${TargetDir}\*" -File -Include *.dll
|
2023-10-03 16:23:51 -04:00
|
|
|
ForEach ( $dll in $WMIDlls ) {
|
|
|
|
|
$path = $dll.FullName
|
|
|
|
|
Write-Output " ""${path}"""
|
|
|
|
|
Start-Process "regsvr32.exe" -ArgumentList "/s ""${path}""" -Wait
|
|
|
|
|
}
|
2023-01-18 12:56:50 -05:00
|
|
|
|
|
|
|
|
|
2023-10-03 16:23:51 -04:00
|
|
|
# Register all *.exe files except for:
|
|
|
|
|
# wbemcntl.exe, wbemtest.exe, mofcomp.exe
|
|
|
|
|
Write-Output "Registering all supported EXE files in ""${TargetDir}"""
|
|
|
|
|
$WMIExes = Get-ChildItem -Path "${TargetDir}\*" -File -Include *.exe | Where-Object { ($_.BaseName.ToLower() -ne "wbemcntl") -and ($_.BaseName.ToLower() -ne "wbemtest") -and ($_.BaseName.ToLower() -ne "mofcomp") }
|
|
|
|
|
ForEach ( $exe in $WMIExes ) {
|
|
|
|
|
$path = $exe.FullName
|
|
|
|
|
Write-Output " ""${path}"""
|
|
|
|
|
Start-Process $path -ArgumentList "/regserver" -Wait
|
|
|
|
|
}
|
2023-01-18 12:56:50 -05:00
|
|
|
|
|
|
|
|
# Register all *.mof and *.mfl files
|
|
|
|
|
Write-Output "Compiling all MOF and MFL files in ""${TargetDir}"""
|
|
|
|
|
$WMIMofs = Get-ChildItem -Path "${TargetDir}\*" -File -Include *.mof,*.mfl
|
2023-10-03 16:23:51 -04:00
|
|
|
ForEach ( $mof in $WMIMofs ) {
|
|
|
|
|
$path = $mof.FullName
|
|
|
|
|
Write-Output " ""${path}"""
|
|
|
|
|
Start-Process "${TargetDir}\mofcomp.exe" -ArgumentList """${path}""" -Wait
|
|
|
|
|
}
|
2023-01-18 12:56:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Catch { Write-Error $_.Exception.Message }
|
|
|
|
|
|
|
|
|
|
Finally {
|
|
|
|
|
# Start the winmgmt service
|
2023-10-03 16:23:51 -04:00
|
|
|
Write-Output "Enabling and starting WMI service"
|
2023-01-18 12:56:50 -05:00
|
|
|
Set-Service -Name winmgmt -StartupType Automatic
|
|
|
|
|
Start-Service -Name winmgmt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Final test of WMI
|
2023-10-03 16:23:51 -04:00
|
|
|
If ( !(Test-WMIFunctionality) ) { Write-Output "WMI repair failed. Please restart this device and run this script again" ; Exit 1 }
|
|
|
|
|
Write-Output "WMI has bee successfully repaired!"
|