大网数据广告活动
启动与系统组件

Scripts to compile all registered MOF files

· 52CE 我爱测

Provides a script to compile all registered Managed Object Format (MOF) files.

This article provides a script to compile all registered Managed Object Format (MOF) files.

The Windows Management Instrumentation (WMI) repository, with the registration of classes and providers, is created by compiling several MOF files.

Most MOF files are located in the C:\Windows\System32\wbem folder, but many others might also be located in the folder of the application registering additional classes or providers.

The list of compiled MOFs is kept in the registry value HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Wbem\CIMOM\Autorecover MOFs.

If WMI detects that the repository is corrupted and unusable, it will create a new empty repository and automatically compile all the MOFs listed in the registry value to restore all the expected objects.

If you encounter strange behavior that might be related to some classes or providers not being registered properly, you can recompile all the MOF files with the following script. The script exports a copy of the registry value before starting the compilation and recording a log of all executed operations and their outcomes.

Important: This sample script is not supported under any Microsoft standard support program or service.

The sample script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose.

The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.

Function Write-Log {
  param( [string] $msg )
  $msg = (get-date).ToString("yyyyMMdd HH:mm:ss.fff") + " " + $msg
  Write-Host $msg
  $msg | Out-File -FilePath $outfile -Append
}

$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal = new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator
if (-not $myWindowsPrincipal.IsInRole($adminRole)) {
  Write-Output "This script needs to be run as Administrator"
  exit
}

$Root = Split-Path (Get-Variable MyInvocation).Value.MyCommand.Path

$resName = "CompMOF-" + $env:computername +"-" + $(get-date -f yyyyMMdd_HHmmss)
$resDir = $Root + "\" + $resName
$outfile = $resDir + "\script-output.txt"
$errfile = $resDir + "\script-errors.txt"
$RdrOut =  " >>""" + $outfile + """"
$RdrErr =  " 2>>""" + $errfile + """"

New-Item -itemtype directory -path $resDir | Out-Null

New-PSDrive -PSProvider registry -Root HKEY_LOCAL_MACHINE -Name HKLM -ErrorAction SilentlyContinue | Out-Null
$mof = (get-itemproperty -ErrorAction SilentlyContinue -literalpath ("HKLM:\SOFTWARE\Microsoft\Wbem\CIMOM")).'Autorecover MOFs'

if ($mof.length -eq 0) {
  Write-Log ("The registry key ""HKLM:\SOFTWARE\Microsoft\Wbem\CIMOM\Autorecover MOFs"" is missing or empty")
  exit
}

$mof | Out-File ($resDir + "\Autorecover MOFs.txt")

foreach ($line in $mof) {
  if ($line.ToLower().contains("uninstall")) {
    Write-Log ("Skipping " + $line) 
  } else {
    $line = $line.Replace("%windir%", $env:windir) 
    $line = $line.Replace("%ProgramFiles%", $env:ProgramFiles) 
    if ($line -gt "") {
      if (Test-path $line) {
        Write-Log ("Compiling " + $line)
        $cmd = "mofcomp """ + $line + """"+ $RdrErr
        Write-Log $cmd
        Invoke-Expression ($cmd) | Out-File -FilePath $outfile -Append
      } else {
        Write-Log ("Missing file " + $line)
      }
    }
  }
}

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
- Supported versions of Windows Server · Source date 02/12/2026
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。