echo Rebuild WMI

ver | find "6" > nul
if %ERRORLEVEL% == 0 goto ver_2008

ver | find "5.2" > nul
if %ERRORLEVEL% == 0 goto ver_2003

ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp

ver | find "2000" > nul
if %ERRORLEVEL% == 0 goto ver_2000

echo Machine undetermined.
goto Undetermined

:ver_2008
winmgmt /salvagerepository
lodctr /R
goto ResServices

:ver_2003
winmgmt /clearadap
winmgmt /kill
winmgmt /unregserver
winmgmt /regserver
winmgmt /resyncperf
wmiadap.exe /c
wmiadap.exe /f
lodctr /R

goto ResServices

:ver_xp
winmgmt /clearadap
winmgmt /kill
winmgmt /unregserver
winmgmt /regserver
winmgmt /resyncperf
goto ResServices

:ver_2000
winmgmt /clearadap
winmgmt /kill
winmgmt /unregserver
winmgmt /regserver
winmgmt /resyncperf
goto ResServices

:Undetermined
winmgmt /clearadap
winmgmt /kill
winmgmt /unregserver
winmgmt /regserver
winmgmt /resyncperf

wmiadap.exe /c
wmiadap.exe /f

:ResServices

echo Restart Exchange Management and WMI
net stop "Microsoft Exchange Management"
net stop "windows management instrumentation"
net start "windows management instrumentation"
net start "Microsoft Exchange Management"