update with guards,

improved output,
head
This commit is contained in:
2023-10-04 16:38:02 -04:00
parent c50ce1097f
commit 32b33cee34
+43 -2
View File
@@ -1,6 +1,36 @@
# Guard to exit script if WMI does not appear to be installed at all
<#
Title: Fix-WindowsManagementInsturmentationWMI
Copyright (c) Emberkom LLC
Author: David Yoder
Date: 2023-03-14
Intent and Notes:
This script will attempt to salvage the WMI repository. If that fails, it will
rebuild the repository by recompiling all MOF and MFL files and re-registering
all *.dll and *.exe files that exist in the WBEM directory.
This script will take ~7-10min on mid hardware, and >15min on old/slow hardware.
Microsoft documentation recommends using the following command to rebuild the
WMI repository:
winmgmt /resetrepository
However, in my experience the above command doesn't always fix the issue.
#>
# Exit the 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 1 }
$WMIService = Get-Service -Name winmgmt -ErrorAction SilentlyContinue
# Exit the script if we cannot find the winmgmt service
If ( !$WMIService ) { Write-Output "The winmgmt service cannot be found, WMI is likely not the problem on this endpoint, no actions will be performed by this script" }#; Exit 1 }
# Exit the script if the winmgmt service is not configured for automatic startup
If ( $WMIService.StartType -ne 'Automatic' ) { Write-Output "The winmgmt service is not configured to automatically start, this is not a default configuration, no actions will be performed by this script" }#; Exit 1 }
# Exit the script if the winmgmt service is not running
If ( $WMIService.Status -ne 'Running' ) { Write-Output "The winmgmt service is configured to start automatically but is not currently running, WMI is likely not the issue on this endpoint, no actions will be performed by this script" }#; Exit 1}
# Function to test if WMI is functioning correctly
Function Test-WMIFunctionality {
# Track the number of failed functionality tests
@@ -27,6 +57,9 @@ Function Test-WMIFunctionality {
# Guard to exit script if no repair is necessary
If ( Test-WMIFunctionality ) { Write-Output "WMI components are functioning correctly" ; Exit 0 }
# Record the start time of the repair
$StartTime = (Get-Date)
# 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)
@@ -88,4 +121,12 @@ Finally {
# Final test of WMI
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!"
# Remove Repo_backup directory
If ( Test-Path "${TargetDir}\Repo_backup" ) {
Write-Output "Deleting ""${TargetDir}\Repo_backup"""
Remove-Item -Path "${TargetDir}\Repo_backup" -Recurse -Force -ErrorAction SilentlyContinue
}
$ElapsedTime = (New-TimeSpan -Start $StartTime -End (Get-Date)).ToString("hh\:mm\:ss")
Write-Output "WMI has been successfully repaired! Time elapsed: ${ElapsedTime}"