> For the complete documentation index, see [llms.txt](https://kythuat.dtechvn.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kythuat.dtechvn.com/windows-server/wmi-repository.md).

# 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="/files/Fuwtw938KgRuGsYZR1pU" 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="/files/XOBFEPPito5fxjjSMup4" 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="/files/M1oxIJ1iWegMrGUfMuUn" 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="/files/MzuIwhtRyE1qtJyHtP01" 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="/files/XN5AvVhzfBSDq3jrLBgS" 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="/files/2QX5JzsWQZzcomcmhTt0" 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="/files/cxAXjgMZ1lks1qtslnGQ" 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/
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://kythuat.dtechvn.com/windows-server/wmi-repository.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
