Tài liệu Quản lý mạng Windows bằng Script - Phần 13: Kịch bản trả về tất cả các giá trị doc

11 509 1
Tài liệu Quản lý mạng Windows bằng Script - Phần 13: Kịch bản trả về tất cả các giá trị doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Quản lý mạng Windows Script - Phần 13: Kịch trả tất giá trị Nguồn : quantrimang.com  Trong phần trước loạt đến với kịch có tên gọi DisplayClassProperties.vbs, kịch hiển thị tên thuộc tính lớp WMI Đây nội dung kịch bản, sử dụng Win32_BootConfiguration lớp, kết nối vào biệt danh WMI: Option Explicit On Error Resume Next Dim strComputer Dim strWMINamespace Dim strWMIQuery Dim objWMIService Dim colItems Dim objItem strComputer = "." strWMINamespace = "\root\CIMV2" strWMIQuery = ":Win32_BootConfiguration" Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery) WScript.Echo "Number of properties of " & strWMIQuery & " class is " & objWMIService.Properties_.count For Each objItem in objWMIService.Properties_ Wscript.Echo "Property: " & objItem.name Next Khi chạy kịch (sử dụng thông tin quản trị cục bộ) máy tính Windows XP (với Cscript.exe cấu hình từ trước kịch Windows mặc định), kết cho sau: C:\scripts>DisplayClassProperties.vbs Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved Number of Property: Property: Property: Property: Property: Property: Property: Property: Property: properties of :Win32_BootConfiguration class is BootDirectory Caption ConfigurationPath Description LastDrive Name ScratchDirectory SettingID TempDirectory Như đề cập đến phần cuối, kịch dễ dàng tùy chỉnh để hiển thị tên thuộc tính lớp WMI Ví dụ, muốn hiển thị tất tên thuộc tính lớp Win32_DiskPartition, cần thay đổi dòng: strWMIQuery = ":Win32_BootConfiguration" thành: strWMIQuery = ":Win32_DiskPartition" Khi chạy lại kịch kết thu là: C:\scripts>DisplayClassProperties.vbs Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved Number of properties of :Win32_DiskPartition class is 34 Property: Access Property: Availability Property: BlockSize Property: Bootable Property: BootPartition Property: Caption Property: ConfigManagerErrorCode Property: ConfigManagerUserConfig Property: CreationClassName Property: Description Property: DeviceID Property: DiskIndex Property: ErrorCleared Property: ErrorDescription Property: ErrorMethodology Property: Property: Property: Property: Property: Property: Property: Property: Property: Property: Property: Property: Property: Property: Property: Property: Property: Property: Property: HiddenSectors Index InstallDate LastErrorCode Name NumberOfBlocks PNPDeviceID PowerManagementCapabilities PowerManagementSupported PrimaryPartition Purpose RewritePartition Size StartingOffset Status StatusInfo SystemCreationClassName SystemName Type Hiển thị giá trị thuộc tính Ở quay lại để sử dụng Win32_BootConfiguration lớp, thay đổi để kịch liệt kê không tên tất thuộc tính mà cịn giá trị chúng bạn cần thay đổi dòng: Wscript.Echo "Property: " & objItem.name thành: Wscript.Echo "Property: " & objItem.name & vbTab & "Value: " & objItem.value Kết chạy: C:\scripts>DisplayClassProperties.vbs Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved Number of properties of :Win32_BootConfiguration class is Property: BootDirectory Value: Property: Caption Value: Property: ConfigurationPath Value: Property: Description Value: Property: Property: Property: Property: Property: LastDrive Value: Name Value: ScratchDirectory SettingID Value: TempDirectory Value: Value: Kết giá trị trống Tại lại vậy? Đây tiếp tục, xem dòng này: Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery) Việc đưa vào giá trị biến ghi lại dịng sau: Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2:Win32_BootConfiguration" ) Lưu ý kết nối đến lớp WMI cụ thể (Win32_BootConfiguration) biệt danh WMI để trả gồm có thuộc tính lớp Sau muốn hiển thị tên giá trị thuộc tính Nhưng giá trị trả NULL (trống rỗng) chưa kết nối với nstance cụ thể lớp WMI Glossary nói instance “một thể giới thực quản lý đối tượng kèm với lớp cụ thể” “các instance gồm có giá trị thực” giá trị thực muốn Vậy kết nối đến instance lớp nào? Để kết nối tới instance lớp, bạn cần phải định instance riêng biệt cách sử dụng key property lớp Chúng ta xem key property thuộc tính cung cấp nhận dạng cần thiết cho instance lớp key property đánh dấu Key qualifier tài liệu MSDN Hãy xem trang MSDN cấu hình lớp Win32_BootConfiguration để biết thêm key property cho lớp Hình hiển thị phần trang này, cho bạn cách phân biệt key property lớp: Hình 1: Key property cho lớp Win32_BootConfiguring Từ trang MSDN này, thấy key property cho lớp Win32_BootConfiguration Name Điều nghĩa cần phải định giá trị cho thuộc tính chuỗi biệt danh WMI muốn kết nối đến instance cụ thể lớp để lấy giá trị thuộc tính lớp Hay nói cách khác, tất cần phải thực thay đổi dòng: strWMIQuery = ":Win32_BootConfiguration" thành: strWMIQuery = ":Win32_BootConfiguration.Name='SOMETHING'" Tại “SOMETHING” giá trị thuộc tính tên instance cụ thể lớp Chúng ta tìm thấy giá trị key property instance cụ thể lớp nào? Có cách sử dụng Windows Management Instrumentation Tester (wbemtest.exe) Bắt đầu đánh wbemtest cửa sổ lệnh để bạn mở cửa sổ đây: Hình 2: Windows Management Instrumentation Tester Kích nút Connect kết nối đến khơng gian tên root\cimv2: Hình 3: Kết nối đến lớp Win32_BootConfiguration Kích Connect để quay cửa sổ chính, nơi có tất nút hiển thị đây: Hình 4: Đã kết nối đến lớp Kích nút Enum Instances đánh tên lớp để hiển thị tất instance lớp: Hình 5: Hiển thị instance lớp Cuối cùng, kích OK để hiển thị tất instance lớp liệt kê key property chúng (Name): Hình 6: Các instance Win32_BootConfiguration Sau tất loại cịn lại có instance lớp máy tính thuộc tính Name instance có giá trị "BootConfiguration"! điều có nghĩa để hiển thị giá trị thuộc tính instance lớp Win32_BootConfiguration máy tính tất cần thiết thay đổi dòng này: strWMIQuery = ":Win32_BootConfiguration" thành: strWMIQuery = ":Win32_BootConfiguration.Name='BootConfiguration'" Cách khác, kịch DisplayClassProperties.vbs duyệt lại sau: Option Explicit On Error Resume Next Dim strComputer Dim strWMINamespace Dim strWMIQuery Dim objWMIService Dim colItems Dim objItem strComputer = "." strWMINamespace = "\root\CIMV2" strWMIQuery = ":Win32_BootConfiguration.Name='BootConfiguration'" Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery) WScript.Echo "Number of properties of " & strWMIQuery & " class is " & objWMIService.Properties_.count For Each objItem in objWMIService.Properties_ Wscript.Echo "Property: " & objItem.name & vbTab & "Value: " & objItem.value Next Khi chạy kịch này, khơng hiển thị tên thuộc tính mà cịn giá trị chúng nữa: C:\scripts>DisplayClassProperties.vbs Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved Number of properties of :Win32_BootConfiguration.Name='BootConfiguration' class is Property: BootDirectory Value: \WINDOWS Property: Caption Value: \Device\Harddisk0\Partition1 Property: ConfigurationPath Value: \WINDOWS Property: Description Value: \Device\Harddisk0\Partition1 Property: LastDrive Value: C: Property: Name Value: BootConfiguration Property: ScratchDirectory Value: C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp Property: SettingID Value: Property: TempDirectory Value: C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp Đặt thông tin vào bảng để dễ đọc hơn: BootDirectory \WINDOWS Caption \Device\Harddisk0\Partition1 ConfigurationPath \WINDOWS Description \Device\Harddisk0\Partition1 LastDrive C: Name BootConfiguration ScratchDirectory C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp SettingID Value: TempDirectory C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp Kết luận Chúng ta thấy kịch “trả tất giá trị” đơn giản cho thơng tin hữu dụng máy tính Đây tập mà bạn tự thực hiện: thay kết nối đến instance lớp Win32_BootConfiguration (chỉ có instance lớp này), bạn cố gắng kết nối đến instance lớp Win32_DiskPartition (lớp có vài instance máy tính bạn có nhiều partition) Để thực điều đó, trước tiên bạn cần sử dụng wbemtest để hiển thị instance lớp (tìm hiểu thêm key property khác với instance này) sau thay đổi kịch DisplayClassProperties.vbs để hiển thị thuộc tính giá trị instance cụ thể lớp (nghĩa phân vùng đĩa bạn định) % Hết %   ... để trả gồm có thuộc tính lớp Sau muốn hiển thị tên giá trị thuộc tính Nhưng giá trị trả NULL (trống rỗng) chưa kết nối với nstance cụ thể lớp WMI Glossary nói instance “một thể giới thực quản lý. .. Type Hiển thị giá trị thuộc tính Ở quay lại để sử dụng Win32_BootConfiguration lớp, thay đổi để kịch liệt kê không tên tất thuộc tính mà cịn giá trị chúng bạn cần thay đổi dòng: Wscript.Echo "Property:... kịch này, khơng hiển thị tên thuộc tính mà cịn giá trị chúng nữa: C:\scripts>DisplayClassProperties.vbs Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 199 6-2 001

Ngày đăng: 12/12/2013, 04:15

Hình ảnh liên quan

Hình 1: Key property cho lớp Win32_BootConfiguring - Tài liệu Quản lý mạng Windows bằng Script - Phần 13: Kịch bản trả về tất cả các giá trị doc

Hình 1.

Key property cho lớp Win32_BootConfiguring Xem tại trang 5 của tài liệu.
Hình 2: Windows Management Instrumentation Tester Kích nút Connect và kết nối đến không gian tên root\cimv2:  - Tài liệu Quản lý mạng Windows bằng Script - Phần 13: Kịch bản trả về tất cả các giá trị doc

Hình 2.

Windows Management Instrumentation Tester Kích nút Connect và kết nối đến không gian tên root\cimv2: Xem tại trang 6 của tài liệu.
Hình 3: Kết nối đến lớp Win32_BootConfiguration - Tài liệu Quản lý mạng Windows bằng Script - Phần 13: Kịch bản trả về tất cả các giá trị doc

Hình 3.

Kết nối đến lớp Win32_BootConfiguration Xem tại trang 7 của tài liệu.
Hình 4: Đã kết nối đến lớp - Tài liệu Quản lý mạng Windows bằng Script - Phần 13: Kịch bản trả về tất cả các giá trị doc

Hình 4.

Đã kết nối đến lớp Xem tại trang 8 của tài liệu.
Hình 5: Hiển thị các instance của lớp - Tài liệu Quản lý mạng Windows bằng Script - Phần 13: Kịch bản trả về tất cả các giá trị doc

Hình 5.

Hiển thị các instance của lớp Xem tại trang 8 của tài liệu.
Hình 6: Các instance của Win32_BootConfiguration - Tài liệu Quản lý mạng Windows bằng Script - Phần 13: Kịch bản trả về tất cả các giá trị doc

Hình 6.

Các instance của Win32_BootConfiguration Xem tại trang 9 của tài liệu.

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan