minor improvements
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
# Guard to exit script if WMI does not appear to be installed at all
|
||||
If ( !(Test-Path "${Env:SystemRoot}\System32\wbem") )
|
||||
{ Write-Error "WMI components are not present on this device. Please install them manually." ; Exit }
|
||||
If ( !(Test-Path "${Env:SystemRoot}\System32\wbem") ) { Write-Error "WMI components are not present on this device. Please install them manually." ; Exit 1 }
|
||||
|
||||
# Function to test if WMI is functioning correctly
|
||||
Function Verify-WMIFunctionality {
|
||||
Function Test-WMIFunctionality {
|
||||
# Track the number of failed functionality tests
|
||||
$WMIErrorLevel = 0
|
||||
|
||||
@@ -14,7 +13,7 @@ Function Verify-WMIFunctionality {
|
||||
$Property = 'LastBootUpTime'
|
||||
|
||||
# Test that our property has a value, and that the WBEM folder exists.
|
||||
If ( $(Get-CimInstance -ClassName $Class -ErrorAction SilentlyContinue | Select-Object $Property -ErrorAction SilentlyContinue).$Property -eq $null )
|
||||
If ( $null -eq $(Get-CimInstance -ClassName $Class -ErrorAction SilentlyContinue | Select-Object $Property -ErrorAction SilentlyContinue).$Property )
|
||||
{ Write-Error "Failed to query ${Class} for the value of ${Property}" ; $WMIErrorLevel++ }
|
||||
|
||||
# Test that the WMI repository is consistent
|
||||
@@ -26,17 +25,15 @@ Function Verify-WMIFunctionality {
|
||||
}
|
||||
|
||||
# Guard to exit script if no repair is necessary
|
||||
If ( Verify-WMIFunctionality ) { Exit }
|
||||
|
||||
Write-Output "Attempting to salvage WMI repository"
|
||||
If ( Test-WMIFunctionality ) { Write-Output "WMI components are functioning correctly" ; Exit 0 }
|
||||
|
||||
# Attempt to salvage WMI repository and exit script if no further repair is necessary
|
||||
Write-Output "Attempting to salvage WMI repository"
|
||||
$WMIRepoSalvage = (winmgmt.exe /salvagerepository)
|
||||
If ( !($WMIRepoSalvage -contains "failed") -and (Verify-WMIFunctionality) ) { Exit }
|
||||
Write-Error "Failed to salvage WMI repository"
|
||||
If ( ($WMIRepoSalvage -notcontains "failed") -and (Test-WMIFunctionality) ) { Write-Output "WMI repository successfully salvaged" ; Exit 0 }
|
||||
|
||||
Try {
|
||||
Write-Output "Attempting manual reinitialization of WMI"
|
||||
Write-Output "Failed to salvage WMI repository, attempting manual reinitialization of WMI"
|
||||
|
||||
# Disable and stop the winmgmt service
|
||||
Write-Output "Stopping and disabling WMI service"
|
||||
@@ -46,34 +43,49 @@ Try {
|
||||
# Set the WBEM directory for manual refresh of WMI components
|
||||
$TargetDir = "${Env:WinDir}\System32\wbem"
|
||||
|
||||
# Rename the Repository directory to Rep_bak
|
||||
If ( Test-Path "${TargetDir}\Rep_bak" ) { Remove-Item "${TargetDir}\Rep_bak" -Recurse -Force -ErrorAction SilentlyContinue }
|
||||
If ( Test-Path "${TargetDir}\Repository" ) { Rename-Item -Path "${TargetDir}\Repository" -NewName "${TargetDir}\Rep_bak" -Force }
|
||||
# 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 }
|
||||
|
||||
# Register all *.dll files
|
||||
Write-Output "Registering all DLL files in ""${TargetDir}"""
|
||||
$WMIDlls = Get-ChildItem -Path "${TargetDir}\*" -File -Include *.dll
|
||||
ForEach ( $dll in $WMIDlls ) { $path = $dll.FullName ; Start-Process "regsvr32.exe" -ArgumentList "/s ""${path}""" -Wait }
|
||||
ForEach ( $dll in $WMIDlls ) {
|
||||
$path = $dll.FullName
|
||||
Write-Output " ""${path}"""
|
||||
Start-Process "regsvr32.exe" -ArgumentList "/s ""${path}""" -Wait
|
||||
}
|
||||
|
||||
|
||||
#Register all *.exe files
|
||||
Write-Output "Registering all EXE files in ""${TargetDir}"""
|
||||
$WMIExes = Get-ChildItem -Path "${TargetDir}\*" -File -Include *.exe | Where { ($_.BaseName.ToLower() -ne "wbemcntl") -and ($_.BaseName.ToLower() -ne "wbemtest") -and ($_.BaseName.ToLower() -ne "mofcomp") }
|
||||
ForEach ( $exe in $WMIExes ) { $path = $exe.FullName ; Start-Process $path -ArgumentList "/regserver" -Wait }
|
||||
# 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
|
||||
}
|
||||
|
||||
# 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
|
||||
ForEach ( $mof in $WMIMofs ) { $path = $mof.FullName ; Start-Process "${TargetDir}\mofcomp.exe" -ArgumentList """${path}""" -Wait }
|
||||
ForEach ( $mof in $WMIMofs ) {
|
||||
$path = $mof.FullName
|
||||
Write-Output " ""${path}"""
|
||||
Start-Process "${TargetDir}\mofcomp.exe" -ArgumentList """${path}""" -Wait
|
||||
}
|
||||
}
|
||||
|
||||
Catch { Write-Error $_.Exception.Message }
|
||||
|
||||
Finally {
|
||||
# Start the winmgmt service
|
||||
Write-Output "Enabling and starting WMI service"
|
||||
Set-Service -Name winmgmt -StartupType Automatic
|
||||
Start-Service -Name winmgmt
|
||||
}
|
||||
|
||||
# Final test of WMI
|
||||
If ( !(Verify-WMIFunctionality) ) { Write-Output "WMI repair failed. Please restart this device and run this script again." }
|
||||
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!"
|
||||
Reference in New Issue
Block a user