Author Topic: A WMI rebuild script I had some success with.  (Read 486 times)

0 Members and 1 Guest are viewing this topic.

Offline Jimbo93

  • Newbie
  • *
  • Join Date: May 2020
  • Posts: 2
  • Karma: 0
    • View Profile
A WMI rebuild script I had some success with.
« on: May 13, 2020, 06:43:07 PM »
Howdy,
I had some  issues with my WMI configuration (Win 10 Home up to date at the time, upgraded from win7 pro), not sure they were severe but the old WMIDiag script showed quite a few WMI get errors (nearly 200). Some of these I identified as being connected to software that I intentionally removed. Others I could not identify, didn't know what MOF file might be missing or needed recompiling. So I used the WMI repair from recent versions of tweaking.com windows repair. It seemed to make no difference.  Later on I discovered that the autorecover mofs registry list was full of multiple entries of same mof files. Not sure how that happened nor the possible ramifications. I hand edited out the duplicates taking care to keep the order of MOFS the way they were After making those changes I rebuilt the repository with the script below. Then ran WMIDiag again, the diag script executed WAY faster, and there were many fewer WMI Get errors. The script I used is here: https://docs.microsoft.com/en-us/archive/blogs/fieldcoding/resetting-wmi-repository-dos-and-donts.

I am noticing some things seemingly work now that I didn't know were broke, an example is if I boot safe mode I now get a (not that helpful) alert that I am not connected to the internet, lol. Also I am pretty sure the perfmon /report has info it did not b4. And some event log activity (not bad) that I don't recall seeing b4. I think I am in good shape at this point even though there are still 33 Get errors reported by WMIDiag and some WMI-activity log entries that are puzzling.

Happy windows tweaking, all. The important powershell script part follows:

Code: [Select]
[code lang="powershell"]
function DisableService([System.ServiceProcess.ServiceController]$svc)
{ Set-Service -Name $svc.Name -StartupType Disabled }

function EnableServiceAuto([System.ServiceProcess.ServiceController]$svc)
{ Set-Service -Name $svc.Name -StartupType Automatic }

function StopService([System.ServiceProcess.ServiceController]$svc)
{
[string]$dep = ([string]::Empty)

foreach ($depsvc in $svc.DependentServices)
{ $dep += $depsvc.DisplayName + ", " }

Write-Host "Stopping $($svc.DisplayName) and its dependent services ($dep)"

$svc.Stop()

$svc.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Stopped)

Write-Host "Stopped $($svc.DisplayName)"
}

function StartService([System.ServiceProcess.ServiceController]$svc, [bool]$handleDependentServices)
{
if ($handleDependentServices)
{ Write-Host "Starting $($svc.DisplayName) and its dependent services" }

else
{ Write-Host "Starting $($svc.DisplayName)" }

if (!$svc.Status -ne [System.ServiceProcess.ServiceControllerStatus]::Running)
{
try
{
$svc.Start()

$svc.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running)
}

catch { }
}

Write-Host "Started $($svc.DisplayName)"

if ($handleDependentServices)
{
[System.ServiceProcess.ServiceController]$depsvc = $null;

foreach ($depsvc in $svc.DependentServices)
{
if ($depsvc.StartType -eq [System.ServiceProcess.ServiceStartMode]::Automatic)
{ StartService $depsvc $handleDependentServices }
}
}
}

function RegSvr32([string]$path)
{
Write-Host "Registering $path"

regsvr32.exe $path /s
}

function RegisterMof([System.IO.FileSystemInfo]$item)
{
[bool]$register = $true

Write-Host "Inspecting: $($item.FullName)"

if ($item.Name.ToLowerInvariant().Contains('uninstall'))
{
$register = $false
Write-Host "Skipping - uninstall file: $($item.FullName)"
}

elseif ($item.Name.ToLowerInvariant().Contains('remove'))
{
$register = $false
Write-Host "Skipping - remove file: $($item.FullName)"
}

else
{
$txt = Get-Content $item.FullName

if ($txt.Contains('#pragma autorecover'))
{
$register = $false
Write-Host "Skipping - autorecover: $($item.FullName)"
}

elseif ($txt.Contains('#pragma deleteinstance'))
{
$register = $false
Write-Host "Skipping - deleteinstance: $($item.FullName)"
}

elseif ($txt.Contains('#pragma deleteclass'))
{
$register = $false
Write-Host "Skipping - deleteclass: $($item.FullName)"
}
}

if ($register)
{
Write-Host "Registering $($item.FullName)"
mofcomp $item.FullName
}
}

function HandleFSO([System.IO.FileSystemInfo]$item, [string]$targetExt)
{
if ($item.Extension -ne [string]::Empty)
{
if ($targetExt -eq 'dll')
{
if ($item.Extension.ToLowerInvariant() -eq '.dll')
{ RegSvr32 $item.FullName }
}

elseif ($targetExt -eq 'mof')
{
if (($item.Extension.ToLowerInvariant() -eq '.mof') -or ($item.Extension.ToLowerInvariant() -eq '.mfl'))
{ RegisterMof $item }
}
}
}

# get Winmgmt service
[System.ServiceProcess.ServiceController]$wmisvc = Get-Service 'winmgmt'

# disable winmgmt service
DisableService $wmisvc

# stop winmgmt service
StopService $wmisvc

# get wbem folder
[string]$wbempath = [Environment]::ExpandEnvironmentVariables("%windir%\system32\wbem")

[System.IO.FileSystemInfo[]]$itemlist = Get-ChildItem $wbempath -Recurse | Where-Object { $_.FullName.Contains('AutoRecover') -ne $true}

[System.IO.FileSystemInfo]$item = $null

# walk dlls
foreach ($item in $itemlist)
{ HandleFSO $item 'dll' }

# call /regserver method on WMI private server executable
wmiprvse /regserver

# call /resetrepository method on WinMgmt service executable
winmgmt /resetrepository

# enable winmgmt service
EnableServiceAuto $wmisvc

# start winmgmt service
StartService $wmisvc $true

# walk MOF / MFLs
foreach ($item in $itemlist)
{ HandleFSO $item 'mof' }
« Last Edit: May 13, 2020, 08:05:23 PM by Jimbo93 »

Offline Boggin

  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Jul 2014
  • Posts: 9179
  • Location: UK
  • Karma: 117
    • View Profile
Re: A WMI rebuild script I had some success with.
« Reply #1 on: May 14, 2020, 06:54:12 AM »
These cmds have also proven useful, although I don't know if they are incorporated into the repair program.

winmgmt /standalonehost

winmgmt /resetrepository

regsvr32 wmisvc.dll

winmgmt /resyncperf

However, I'll bring your thread to Shane's attention.
Tom.

Offline Jimbo93

  • Newbie
  • *
  • Join Date: May 2020
  • Posts: 2
  • Karma: 0
    • View Profile
Re: A WMI rebuild script I had some success with.
« Reply #2 on: May 14, 2020, 12:30:23 PM »
Thank you. I might post more info about my computer issues in the designated forum. Still investigating them.

Offline Boggin

  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Jul 2014
  • Posts: 9179
  • Location: UK
  • Karma: 117
    • View Profile
Re: A WMI rebuild script I had some success with.
« Reply #3 on: May 14, 2020, 03:24:19 PM »
Two useful cmds run from a Command Prompt (Admin) for Win 8.1 and Win 10 can resolve most problems.

dism /online /cleanup-image /restorehealth

sfc /scannow

When a sfc /scannow reports it is unable to repair all corruption in those two versions of Windows, running that dism cmd followed by another sfc /scannow will usually repair all corruption.

If you have Windows Update problems, then run the following cmd before those two as well as including and is -

dism /online /cleanup-image /startcomponentcleanup

This cleans out obsolete updates from the WinSxS folder, which Win 10 is supposed to do automatically.
Tom.