# WMI Repository

## TẠI SAO PHẢI QUAN TÂM WMI

### Tình trạng theo dõi

* Một ngày đẹp trời bạn chạy truy vấn SQL Query thấy rất chậm, không chạy được thông số phần cứng của máy tính....
* Chạy các tệp VBS hoặc PowerShell có dùng các hàm hệ thống như Get-WmiObject thấy không thực thi được

### Kiểm tra trạng thái WMI

Vào Run gõ vào lệnh sau: **service.msc** để hiện thị ra các dịch vụ hoặc dùng PowerShell:&#x20;

```
Get-Service Winmgmt | Select DisplayName,Status,ServiceName
```

<figure><img src="https://3934179848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpqWC051kMhpZgqZaU9Oy%2Fuploads%2FwUzobPyN5CK10kHdLHe5%2Fimage.png?alt=media&#x26;token=d0fcab61-8cd4-438f-82c5-a9f153085a02" alt=""><figcaption></figcaption></figure>

Nếu thấy thông số đang hoạt động running thì thử vài câu lệnh test xem có thực thi được hay không như:

```
// Kiểm tra những ứng dụng đã cài đặt trên máy tính windows
//Mở Run lên gõ vào cmd, copy lệnh sau paste vào rồi Enter để chạy
wmic product get name,version

//Hoặc sử dụng Powershell
get-wmiobject Win32_OperatingSystem
```

<figure><img src="https://3934179848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpqWC051kMhpZgqZaU9Oy%2Fuploads%2FKJNLJKUi52UayncxXcdZ%2Fimage.png?alt=media&#x26;token=6ef6f5ca-d3b6-4228-979d-385bde76d178" alt=""><figcaption></figcaption></figure>

Chạy các câu lệnh kiểm tra vẫn ra kết quả nhanh và chính xác thì VMI vẫn ổn, nếu trường hợp không chạy ra lâu, không chính xác hoặc lỗi vào Run gõ lệnh **compmgmt.msc** để kiểm tra WMI Control

<figure><img src="https://3934179848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpqWC051kMhpZgqZaU9Oy%2Fuploads%2FaYKHqatNba6neNX3zMn5%2Fimage.png?alt=media&#x26;token=bdf6eab9-5f4e-4e1c-aab9-4e237d72cd45" alt=""><figcaption></figcaption></figure>

Thông báo lỗi khi xem:

```
Failed to initialize all required WMI classes
Win32_Processor. WMI: Invalid namespace
Win32_WMISetting. WMI: Invalid namespace
Win32_OperationSystem. WMI: Invalid namespace
```

## SỬA LỖI TỰ ĐỘNG WMI

Mở Run gõ vào lệnh cmd sau:&#x20;

```
winmgmt /verifyrepository
```

<figure><img src="https://3934179848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpqWC051kMhpZgqZaU9Oy%2Fuploads%2FyOO8ou7IZxgyTeGVuepc%2Fimage.png?alt=media&#x26;token=6aa9b08c-5571-4c5e-af9d-3795c85e410e" alt=""><figcaption></figcaption></figure>

Nếu chạy câu lệnh trên ra kết quả là **INCONSISTENT** hoặc **WMI repository verification failed** thì phải tiến hành chạy câu lệnh sau

```
Winmgmt /salvagerepository
```

Câu lệnh chạy sẽ kiểm tra lại WMI và sửa lỗi, sau đó hãy dừng dịch vụ và chạy lại bằng lệnh sau

```
net stop Winmgmt
net start Winmgmt
```

Kiểm ra kết quả WMI, nếu không hết lỗi tiếp tục lần lượt chạy câu lệnh sau, chú ý nếu windows 64Bit (cd %windir%\SysWOW64\wbem)  thì đổi đường dẫn

```
sc config winmgmt start= disabled
net stop winmgmt
cd %windir%\system32\wbem
for /f %s in ('dir /b *.dll') do regsvr32 /s %s
wmiprvse /regserver
sc config winmgmt start= auto
net start winmgmt
for /f %s in ('dir /b *.mof') do mofcomp %s
for /f %s in ('dir /b *.mfl') do mofcomp %s
```

<figure><img src="https://3934179848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpqWC051kMhpZgqZaU9Oy%2Fuploads%2FtU9WMojmu064FOST1NCZ%2Fimage.png?alt=media&#x26;token=6479ede2-4e2c-4470-90dc-4dd27dd4b23a" alt=""><figcaption></figcaption></figure>

Kiểm tra lại hoạt động của WMI, nếu không được chuyển sang hướng xây dựng lại WMI. Vào Run gõ vào cmd:

```
winmgmt /verifyrepository
```

<figure><img src="https://3934179848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpqWC051kMhpZgqZaU9Oy%2Fuploads%2FrWdO8o3DbWcsFpUMeodS%2Fimage.png?alt=media&#x26;token=a98a20ba-8275-4b1c-8625-8f8107a42038" alt=""><figcaption></figcaption></figure>

Hiện ra thông báo như sau là OK rồi.

```
WMI repository is consistent
```

## XÂY DỰNG LẠI WMI

WMI được lưu trữ tại %windir%\System32\Wbem\Repository, vì một lý do nào đó không thể sửa lỗi được thì phải tạo lại (xây dưng lại kho WMI)

Vào Run gõ vào câu lệnh cmd

```
Winmgmt /resetrepository
```

Kiểm tra lại dịch vụ WMI&#x20;

```
winmgmt /verifyrepository
```

Nếu vẫn chưa ổn thì chạy lần lượt tiếp lệnh sau, chú ý windows 64BIT (%windir%\sysWOW64\wbem)

```
sc config winmgmt start= disabled
sc config winmgmt start= disabled
net stop winmgmt
cd %windir%\system32\wbem
winmgmt /resetrepository
winmgmt /resyncperf
if exist Repos_bakup rd Repos_bakup /s /q
rename Repository Repos_bakup
regsvr32 /s %systemroot%\system32\scecli.dll
regsvr32 /s %systemroot%\system32\userenv.dll
for /f %s in ('dir /b *.dll') do regsvr32 /s %s
for /f %s in ('dir /b *.mof') do mofcomp %s
for /f %s in ('dir /b *.mfl') do mofcomp %s
sc config winmgmt start= auto
net start winmgmt
wmiprvse /regserver
```

<figure><img src="https://3934179848-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpqWC051kMhpZgqZaU9Oy%2Fuploads%2F4u8jNZEtkygvXGfGeiXF%2Fimage.png?alt=media&#x26;token=f8120cf4-2135-440f-9b4e-360b65aa7f76" alt=""><figcaption></figcaption></figure>

```
// Tài liệu tham khảo:
https://woshub.com/wmi-troubleshooting/
https://techcommunity.microsoft.com/t5/ask-the-performance-team/wmi-rebuilding-the-wmi-repository/ba-p/373846
https://techcommunity.microsoft.com/t5/ask-the-performance-team/wmi-repository-corruption-or-not/ba-p/375484
https://licensesoft.vn/wmi-la-gi.htm
https://sec.vnpt.vn/2024/02/review-wmi-tool/
```
