Windows: Powershell script to fix and repair WMI

Hi,

this script repairs and recreates the WMI Repository of a Windows. For example, this occurs sometimes if you clone a Windows machine with an installed SCCM/SMS Client. The typical error in this case is “Failed to open to WMI namespace ‘\\.\root\CCM\SoftwareUpdates\DeploymentAgent’ (8007045b)”

Lasts start. Start a Powershell console as Administrator. Stop all services. Sometimes other services depends on the WMI Service and does not accept a stop request. Therefore you cannot stop the WMI service. TrendMicro Office is such a service. You have to kill them by TaskManager.

1
2
3
4
# Stop WMI
# Only if SCCM/SMS Client is installed. Stop ccmexec.
Stop-Service -Force ccmexec -ErrorAction SilentlyContinue
Stop-Service -Force winmgmt

(Re)Register WMI binary components

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[String[]]$aWMIBinaries=@("unsecapp.exe","wmiadap.exe","wmiapsrv.exe","wmiprvse.exe","scrcons.exe")
foreach ($sWMIPath in @(($ENV:SystemRoot+"\System32\wbem"),($ENV:SystemRoot+"\SysWOW64\wbem"))){
    if(Test-Path -Path $sWMIPath){
        push-Location $sWMIPath
        foreach($sBin in $aWMIBinaries){
            if(Test-Path -Path $sBin){
                $oCurrentBin=Get-Item -Path  $sBin
                Write-Host " Register $sBin"
                & $oCurrentBin.FullName /RegServer
            }
            else{
                # Warning only for System32
                if($sWMIPath -eq $ENV:SystemRoot+"\System32\wbem"){
                    Write-Warning "File $sBin not found!"
                }
            }
        }
        Pop-Location
    }
}

Reregister WMI Managed Objects

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
if([System.Environment]::OSVersion.Version.Major -eq 5)
{
   foreach ($sWMIPath in @(($ENV:SystemRoot+"\System32\wbem"),($ENV:SystemRoot+"\SysWOW64\wbem"))){
        if(Test-Path -Path $sWMIPath){
            push-Location $sWMIPath
            Write-Host " Register WMI Managed Objects"
            $aWMIManagedObjects=Get-ChildItem * -Include @("*.mof","*.mfl")
            foreach($sWMIObject in $aWMIManagedObjects){
                $oWMIObject=Get-Item -Path  $sWMIObject
                & mofcomp $oWMIObject.FullName             
            }
            Pop-Location
        }
   }
   if([System.Environment]::OSVersion.Version.Minor -eq 1)
   {
        & rundll32 wbemupgd,UpgradeRepository
   }
   else{
        & rundll32 wbemupgd,RepairWMISetup
   }
}
else{
    # Other Windows Vista, Server 2008 or greater
    Write-Host " Reset Repository"
    & ($ENV:SystemRoot+"\system32\wbem\winmgmt.exe") /resetrepository
    & ($ENV:SystemRoot+"\system32\wbem\winmgmt.exe") /salvagerepository
}

Starting services

1
2
Start-Service -Force winmgmt
Start-Service -Force ccmexec -ErrorAction SilentlyContinue

Michael

Update 04/04/2014: Modified the script. From now, no more WMI queries are done by the script. The OS version is determined by the System.Environent class.

Fix and repair Windows WMI
Fix and repair Windows WMI
fixwmi.ps1
Version: 1.1
5.4 KiB
3557 Downloads
Details...

6 thoughts on “Windows: Powershell script to fix and repair WMI”

  1. Thanks for this script, but I’m having trouble even running it on my windows 2008 host.

    AuthorizationManager check failed.
    At line:1 char:13
    + .\fixwmi.ps1 <<<<
    + CategoryInfo : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

    I had the execution policy set to remote-signed, but now get-execution policy throws an error.

    Get-ExecutionPolicy :
    At line:1 char:20
    + Get-ExecutionPolicy <<<<
    + CategoryInfo : NotSpecified: (:) [Get-ExecutionPolicy], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.PowerShell.Commands.GetExecutionPo
    licyCommand

    I've heard this is because of WMI being broken, but I can't use your script.

    1. Hi John, try the new updated script V1.1. I’ve replaced all WMI queries to determine the OS version with .NET System.Environment function calls.

Leave a Reply