Computer >> Hướng Dẫn Máy Tính >  >> Hệ Thống >> Máy Chủ Windows

Sử dụng DISM kiểm tra và sửa chữa Windows Image

Bạn có thể dùng lệnh SFC (System File Checker) và DISM (Deployment Image Servicing and Management) để kiểm tra và sửa chữa tính toàn vẹn file hệ thống và Component Store của Windows image (Windows Server). Các công cụ này cực kỳ hữu ích nếu Windows không ổn định, không boot, lỗi xuất hiện khi chạy app hoặc dịch vụ tích hợp, sau nhiễm virus, v.v.

Trong bài này, chúng ta xem cách dùng lệnh SFC /ScanNow, DISM /Online /Cleanup-Image /RestoreHealth, hoặc Repair-WindowsImage -Online -RestoreHealth để sửa chữa image và file hệ thống trên Windows 10/11 và Windows Server 2022/2019/2016.

SFC /ScanNow: Dùng System File Checker sửa file hệ thống Windows

Khuyến khích dùng lệnh DISM khôi phục Windows sau khi kiểm tra tính toàn vẹn file hệ thống dùng công cụ SFC. Lệnh sfc /scannow quét file hệ thống được bảo vệ và nếu chúng thiếu hoặc hỏng, nó cố khôi phục bản sao gốc từ Windows Component Store (thư mục C:\Windows\WinSxS).

Công cụ SFC ghi tất cả hoạt động vào %windir%\logs\cbs\cbs.log. Tất cả entry SFC trong CBS.log được gắn thẻ [SR]. Để chọn chỉ entry liên quan SFC từ log, chạy lệnh:

findstr /c:"[SR]" %windir%\Logs\CBS\CBS.log >"%userprofile%\Desktop\sfc.txt"

Nếu lệnh sfc /scannow trả về lỗi “Windows Resource Protection found corrupt files but was unable to fix some of them“, nhiều khả năng công cụ không lấy được file cần thiết từ Windows Component Store (xem ảnh dưới).

Sử dụng DISM kiểm tra và sửa chữa Windows Image

Trong trường hợp này, bạn có thể thử sửa Component Store của Windows image dùng lệnh DISM.exe.

Công cụ DISM (Deployment Image Servicing and Management) có sẵn trong mọi phiên bản Windows từ Vista.

Sau khi sửa Windows image, bạn có thể thử dùng SFC khôi phục file hệ thống.

Kiểm tra sức khỏe Windows Component Store dùng DISM

Công tắc DISM /Cleanup-Image /CheckHealth dùng quét Windows image tìm lỗi và sửa chúng. Lệnh DISM phải chạy từ command prompt quyền admin.

Chạy lệnh sau kiểm tra xem có cờ hỏng hóc nào của Windows image Component Store không (không áp dụng cho Windows 7/Server 2008R2). Lệnh này kiểm tra CBS flag được đặt bởi một trong các tiến trình bảo trì hệ thống.

DISM /Online /Cleanup-Image /CheckHealth

Lệnh này không thực hiện quét đầy đủ Component Store. Lệnh chỉ kiểm tra xem Windows image có bị gắn cờ hỏng không và có thể sửa không. Không thay đổi image.

Sử dụng DISM kiểm tra và sửa chữa Windows Image

Trong ví dụ này, lệnh trả về Windows 10 image không hỏng:

No component store corruption detected. The operation completed successfully.

Để chạy quét đầy đủ sức khỏe Windows Component Store, chạy lệnh:

DISM /Online /Cleanup-Image /ScanHealth

Lệnh kiểm tra Windows image có thể mất khá lâu (10-30 phút). Và trả về một trong ba kết quả:

  • No component store corruption detected – DISM không tìm thấy lỗi trong component store;
  • The component store is repairable – DISM gặp lỗi trong Component Store và có thể sửa;
  • The component store is not repairable – DISM không thể sửa Windows Component Store (thử dùng phiên bản DISM mới hơn hoặc bạn phải khôi phục Windows image từ backup, reset hoặc cài lại hoàn toàn Windows instance).
Sử dụng DISM kiểm tra và sửa chữa Windows Image

Để dùng công tắc DISM /ScanHealth trên Windows 7 và Windows Server 2008, bạn phải cài update KB2966583. Nếu không, bạn sẽ thấy thông báo: “Error 87. The ScanHealth option is not recognized in this context”.

Trong một số trường hợp, DISM /ScanHealth trả về lỗi sau:

  • DISM Error 1726 – “The remote procedure call failed”;
  • DISM Error 1910 – “The object exporter specified was not found”.

Chắc chắn nghĩa là Windows image của bạn bị hỏng và cần sửa.

Sửa chữa Windows Image dùng DISM /RestoreHealth

Để sửa hỏng hóc trong Windows image Component Store, bạn phải dùng tùy chọn RestoreHealth của lệnh DISM. Tùy chọn này cho phép sửa lỗi tìm thấy trong Windows image, tự động tải và thay thế file thành phần hỏng hoặc thiếu bằng bản gốc từ Windows Update (máy tính phải có Internet trực tiếp). Chạy lệnh:

DISM /Online /Cleanup-Image /RestoreHealth

Trong Windows 7/2008 R2, lệnh này trông khác:
DISM.exe /Online /Cleanup-Image /ScanHealth

Quá trình quét và sửa Component Store có thể mất khá lâu (30 phút trở lên). DISM tự động tải và thay thế file thành phần hỏng/thiếu bằng phiên bản file gốc từ server Windows Update.

Nếu sửa thành công, thông báo sau xuất hiện:

The restore operation completed successfully.
Sử dụng DISM kiểm tra và sửa chữa Windows Image

DISM /RestoreHealth: Không tìm thấy file nguồn

Nếu máy tính (server) không có Internet trực tiếp (ở sau proxy, hoặc dùng WSUS nội bộ nhận update bảo mật và build) hoặc dịch vụ Windows Update bị tắt/hỏng (cách sửa Windows Update client), thì các lỗi sau xuất hiện khi sửa Component Store:

  • 0x800f0906 – The source files could not be downloaded. Use the source option to specify the location of the files that are required to restore the feature;
  • 0x800f0950 – DISM failed. No operation was performed;
  • 0x800F081F – The source files could not be found. Use the “Source” option to specify the location of the files that are required to restore the feature.
Sử dụng DISM kiểm tra và sửa chữa Windows Image

Trong tất cả các trường hợp này, bạn có thể dùng cách替代 để lấy file nguồn Component Store. Có thể là:

  • Đĩa cài/USB boot/ISO image;
  • File wim/esd đã mount;
  • Thư mục \sources\SxS từ đĩa cài;
  • File install.wim (esd) với ảnh cài Windows.

Bạn có thể chỉ định file WIM hoặc ESD với ảnh cài Windows gốc để dùng làm nguồn sửa file hệ thống. Giả sử bạn mount ISO Windows 11 cài vào ổ ảo D:.

Lưu ý: Để sửa file hỏng trong Component Store từ nguồn local, build và phiên bản Windows trong ảnh WIM/ESD phải khớp với hệ thống của bạn.

Kiểm tra phiên bản Windows cài trên máy tính bằng lệnh PowerShell:

Get-ComputerInfo |select WindowsProductName,WindowsEditionId,WindowsVersion, OSDisplayVersion
Sử dụng DISM kiểm tra và sửa chữa Windows Image

Liệt kê các phiên bản Windows có sẵn trong file wim cài đặt:

Get-WindowsImage -ImagePath "D:\sources\install.wim"

Trong trường hợp này, ảnh Windows 11 Pro trong file install.wim có ImageIndex = 6.

Sử dụng DISM kiểm tra và sửa chữa Windows Image

Để sửa Component Store từ file WIM/ESD local dùng file nguồn local (không dùng dịch vụ Windows Update online), chạy lệnh sau (nhớ chỉ định index phiên bản Windows trong file image):

DISM /online /cleanup-image /restorehealth /source:WIM:D:\sources\install.wim:6 /limitaccess

hoặc:
DISM /online /cleanup-image /restorehealth /source:ESD:D:\sources\install.esd:6 /limitaccess Sử dụng DISM kiểm tra và sửa chữa Windows Image

Các lỗi sau có thể xuất hiện khi chạy lệnh DISM /RestoreHealth:

  • Error: 50: DISM does not support servicing Windows PE with the /Online option – nghĩa là DISM nghĩ bạn dùng image WinPE. Để sửa, xóa khóa registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\MiniNT;
  • DISM Error 87: đảm bảo lệnh DISM viết đúng, đảm bảo bạn dùng phiên bản DISM cho phiên bản Windows của bạn (thường khi boot vào WinPE/WinRE).

Bạn có thể tìm log DISM quét và sửa file hệ thống tại: C:\Windows\Logs\CBS.log.

Sau khi component store đã sửa, bạn có thể chạy system file checker tool (sfc /scannow). Có khả năng nó sẽ khôi phục file hệ thống hỏng hoặc thiếu (Windows Resource Protection found corrupt files and successfully repaired them).

Sử dụng DISM kiểm tra và sửa chữa Windows Image

Nếu SFC.exe không phát hiện hỏng hóc file hệ thống, thông báo xuất hiện:

Windows Resource Protection did not find any integrity violations.

Repair-WindowsImage: Sửa chữa Windows Image Component Store bằng PowerShell

Phiên bản PowerShell trong Windows 10/11 và Windows Server 2016/2019/2022 có cmdlet tương tự lệnh DISM thảo luận trên. Để quét Windows component store tìm hỏng hóc, chạy lệnh:

Repair-WindowsImage -Online –ScanHealth
Sử dụng DISM kiểm tra và sửa chữa Windows Image

Nếu không tìm thấy lỗi trong Component Store, thông báo sau xuất hiện:

ImageHealth State: Healthy

Để sửa file Windows Component Store, chạy:

Repair-WindowsImage -Online -RestoreHealth

Nếu không có Internet trực tiếp, lệnh này có thể treo trong quá trình sửa image. Bạn có thể khôi phục thành phần hệ thống từ file ảnh Windows local (install.wim/install.esd) copy từ ISO cài Windows 10. Ở đây cũng cần chỉ định index phiên bản Windows trong file wim làm nguồn khôi phục:

Repair-WindowsImage -Online -RestoreHealth -Source F:\sources\install.wim:5 -LimitAccess

Dùng DISM Offline sửa chữa Windows Image

Nếu Windows không boot đúng, bạn có thể dùng DISM kiểm tra và sửa file hệ thống Windows image offline.

Nếu Windows không boot sau cài update, thử gỡ update mới nhất trước theo hướng dẫn.

  1. Khởi động thiết bị từ ảnh cài Windows (có thể dùng Media Creation Tool tạo USB boot Windows) và bấm Shift + F10 trên màn hình cài Windows ban đầu;
  2. Kiểm tra ký tự ổ đĩa gán trong WinPE, chạy lệnh diskpart → list vol (trong ví dụ, ký tự ổ C:\ gán cho ổ đĩa cài Windows, và tôi sẽ dùng nó trong lệnh sau); Sử dụng DISM kiểm tra và sửa chữa Windows Image
  3. Kiểm tra file hệ thống và sửa file hỏng bằng lệnh: sfc /scannow /offbootdir=C:\ /offwindir=C:\Windows
    Sử dụng DISM kiểm tra và sửa chữa Windows Image
  4. Để sửa offline Windows image, dùng lệnh sau (tôi dùng file WIM với ảnh cài Windows 10 từ đó boot máy tính làm nguồn khôi phục offline Windows image):
    Dism /image:C:\ /Cleanup-Image /RestoreHealth /Source:D:\sources\install.wim
    Sử dụng DISM kiểm tra và sửa chữa Windows Image
  5. Nếu không đủ dung lượng trống trên ổ đĩa đích, bạn cần ổ riêng, ví dụ F:\, tạo thư mục trống mkdir F:\scratch. Thực hiện sửa component store dùng scratch dir với lệnh: Dism /image:C:\ /Cleanup-Image /RestoreHealth /Source:D:\sources\install.wim /ScratchDir:F:\scratch

Mẹo: Dưới đây là một số tham số DISM hữu ích quản trị viên phải biết:

  • DISM /Add-Package – cài file update MSU/CAB, tích hợp update bảo mật vào Windows image;
  • DISM /Get-Drivers – lấy danh sách driver đã cài;
  • DISM /Add-Driver – inject driver vào ảnh cài Windows;
  • DISM /Add-Capability – cài tính năng Windows thêm qua Features on Demand (FoD). Ví dụ: RSAT, OpenSSH server, hoặc Windows SSH client);
  • DISM /Enable-Features và /Disable-Features – bật/tắt thành phần Windows (ví dụ: giao thức SMBv1);
  • Dism.exe /StartComponentCleanup – dọn dẹp Component Store và xóa phiên bản component cũ (từ thư mục WinSxS);
  • Dism /set-edition – nâng cấp từ bản evaluation sang phiên bản Windows đầy đủ không cần cài lại.