From 32b33cee34e01f9fc351aeadf22b739a8f8b9cca Mon Sep 17 00:00:00 2001 From: David Yoder Date: Wed, 4 Oct 2023 16:38:02 -0400 Subject: [PATCH] update with guards, improved output, head --- Fix-WindowsManagementInsturmentationWMI.ps1 | 45 ++++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/Fix-WindowsManagementInsturmentationWMI.ps1 b/Fix-WindowsManagementInsturmentationWMI.ps1 index c76973c..fc7f025 100644 --- a/Fix-WindowsManagementInsturmentationWMI.ps1 +++ b/Fix-WindowsManagementInsturmentationWMI.ps1 @@ -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!" \ No newline at end of file + +# 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}" \ No newline at end of file