天天看點

使用WMI對象收集計算機資訊

WMI(windows管理規範),是Windows 2K/XP管理系統的核心;對于其他的Win32作業系統,WMI是一個有用的插件。WMI以CIMOM為基礎。

學習目标

擷取所有的WMI對象

WMI類詳細資訊顯示

使用WMI對計算機資訊進行收集

使用WQL語句

WMI對象

WMI對象:WmiObject

WMI:Windows管理規範

WMI将資料視為有一個或多個項目的集合中的對象

通過WMI可以使用較少的工作量執行進階工作

擷取WMI對象:Get-WmiObject

顯示所有的WMI對象

Get-WmiObject-List

可以使用-commputer選項擷取遠端計算機上的參數

擷取本機所有WmiObject 資訊

1

<code>Get-WmiObject</code> <code>--list</code>

<a href="http://s5.51cto.com/wyfs02/M00/8A/49/wKiom1gsZFuh0RCaAASyLGJQ73g308.png" target="_blank"></a>

擷取磁盤資訊

<code>Get-WmiObject win32_logicaldisk</code>

<a href="http://s3.51cto.com/wyfs02/M01/8A/53/wKiom1gtUibiuhLPAAFQmOSsecA555.png-wh_500x0-wm_3-wmp_4-s_918693413.png" target="_blank"></a>

擷取指定磁盤資訊

<code>Get-WmiObject</code> <code>win32_logicaldisk | ?{</code><code>$_</code><code>.DeviceID </code><code>-like</code> <code>"C:"</code><code>}</code>

<a href="http://s4.51cto.com/wyfs02/M01/8A/50/wKioL1gtVJrzySteAABqiD-0WQg982.png-wh_500x0-wm_3-wmp_4-s_1063004630.png" target="_blank"></a>

擷取C槽剩餘空間

<code>(</code><code>Get-WmiObject</code> <code>win32_logicaldisk | ?{</code><code>$_</code><code>.DeviceID </code><code>-like</code> <code>"C:"</code><code>}).freespace</code>

<a href="http://s2.51cto.com/wyfs02/M02/8A/50/wKioL1gtVR3hILHmAAA8nZLg3lM592.png-wh_500x0-wm_3-wmp_4-s_4276775996.png" target="_blank"></a>

擷取CPU資訊

<code>Get-WmiObject</code> <code>win32_processor</code>

<a href="http://s2.51cto.com/wyfs02/M02/8A/53/wKiom1gtVdXiwNyLAAB7wqfuCO8882.png-wh_500x0-wm_3-wmp_4-s_3582419787.png" target="_blank"></a>

注意:上面是我在虛拟機中執行指令的結果,實體機中資訊要比這多很多

檢視計算機制造商資訊

<code>Get-WmiObject</code> <code>win32_computersystem</code>

<a href="http://s2.51cto.com/wyfs02/M00/8A/50/wKioL1gtVmmC8HKSAAD_JTGVpdQ358.png-wh_500x0-wm_3-wmp_4-s_483180661.png" target="_blank"></a>

擷取記憶體總大小

<code>(</code><code>Get-WmiObject</code> <code>win32_computersystem).TotalPhysicalmemory</code>

<a href="http://s1.51cto.com/wyfs02/M02/8A/50/wKioL1gtVveCWDJsAACBObFto5Y810.png-wh_500x0-wm_3-wmp_4-s_2653881580.png" target="_blank"></a>

檢視系統共享目錄

<code>Get-WmiObject win32_share</code>

檢視共享相信資訊

<code>Get-WmiObject</code> <code>win32_share | </code><code>Format-list</code> <code>*</code>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

<code>PS C:\Users\Administrator&gt; </code><code>Get-WmiObject</code> <code>win32_share | </code><code>Format-list</code> <code>*</code>

<code>PSComputerName   : OPS01-WINTEST01</code>

<code>Status           : OK</code>

<code>Type             : 2147483648</code>

<code>Name             : ADMIN$</code>

<code>__GENUS          : 2</code>

<code>__CLASS          : Win32_Share</code>

<code>__SUPERCLASS     : CIM_LogicalElement</code>

<code>__DYNASTY        : CIM_ManagedSystemElement</code>

<code>__RELPATH        : Win32_Share.Name=</code><code>"ADMIN$"</code>

<code>__PROPERTY_COUNT : 10</code>

<code>__DERIVATION     : {CIM_LogicalElement, CIM_ManagedSystemElement}</code>

<code>__SERVER         : OPS01-WINTEST01</code>

<code>__NAMESPACE      : root\cimv2</code>

<code>__PATH           : \\OPS01-WINTEST01\root\cimv2:Win32_Share.Name=</code><code>"ADMIN</code>

<code>AccessMask       :</code>

<code>AllowMaximum     : True</code>

<code>Caption          : Remote Admin</code>

<code>Description      : Remote Admin</code>

<code>InstallDate      :</code>

<code>MaximumAllowed   :</code>

<code>Path             : C:\Windows</code>

<code>Scope            : System.Management.ManagementScope</code>

<code>Options          : System.Management.ObjectGetOptions</code>

<code>ClassPath        : \\OPS01-WINTEST01\root\cimv2:Win32_Share</code>

<code>Properties       : {AccessMask, AllowMaximum, Caption, Description...}</code>

<code>SystemProperties : {__GENUS, __CLASS, __SUPERCLASS, __DYNASTY...}</code>

<code>Qualifiers       : {dynamic, Locale, provider, UUID}</code>

<code>Site             :</code>

<code>Container        :</code>

<code>Name             : C$</code>

<code>__RELPATH        : Win32_Share.Name="</code><code>C$</code><code>"</code>

<code>__PATH           : \\OPS01-WINTEST01\root\cimv2:Win32_Share.Name="</code><code>C$</code><code>"</code>

<code>Caption          : Default share</code>

<code>Description      : Default share</code>

<code>Path             : C:\</code>

<code>Name             : D$</code>

<code>__RELPATH        : Win32_Share.Name="</code><code>D$</code><code>"</code>

<code>__PATH           : \\OPS01-WINTEST01\root\cimv2:Win32_Share.Name="</code><code>D$</code><code>"</code>

<code>Path             : D:\</code>

<code>Type             : 2147483651</code>

<code>Name             : IPC$</code>

<code>__RELPATH        : Win32_Share.Name="</code><code>IPC$</code><code>"</code>

<code>__PATH           : \\OPS01-WINTEST01\root\cimv2:Win32_Share.Name="</code><code>IPC$"</code>

<code>Caption          : Remote IPC</code>

<code>Description      : Remote IPC</code>

<code>Path             :</code>

手動建立的共享type為0,系統預設的共享,type值不為0,是以可以用以下方法過濾出手動建立的共享目錄

<code>Get-WmiObject win32_share | ?{$_.</code><code>type</code> <code>-</code><code>eq</code> <code>0}</code>

<a href="http://s1.51cto.com/wyfs02/M01/8A/54/wKiom1gtW1SwIVRVAABToy34WL8455.png-wh_500x0-wm_3-wmp_4-s_1884653672.png" target="_blank"></a>

使用WQL語句可以用于篩選WMI對象

文法:

<code>Get-WmiObject -Query "</code><code>select</code> <code>[對象] from WMI對象  WHERE 條件“</code>

上面的指令可以分解成兩條:

get-wmiobject -query "WQL語句" 

WQL= ”select * from Wmi對象 where 條件“

練習:

列出本機的WmiObject對象

隻擷取win32_開頭的對象

擷取所有的Win32_Share對象,并用列舉的方式表示

查找狀态為running的win32_service對象

查找startMode為Auto的win32_service對象

檢視所有的本地磁盤

查找每個磁盤的剩餘空間

例如: 檢視所有由使用者建立的共享目錄

<code>Get-WmiObject -query </code><code>"select * from win32_share where type=0"</code>

<a href="http://s4.51cto.com/wyfs02/M00/8A/51/wKioL1gtagiSuCfLAADBuYyhIpY658.png-wh_500x0-wm_3-wmp_4-s_656976905.png" target="_blank"></a>

練習1:擷取win32_開頭的對象

<code>Get-WmiObject -list win32_*</code>

練習2:查找狀态為running的win32_service對象

<code>Get-WmiObject -query </code><code>"select * from win32_service where state='running'"</code>

<a href="http://s1.51cto.com/wyfs02/M01/8A/55/wKiom1gtbIXw0xFQAACsPGzvi6c282.png-wh_500x0-wm_3-wmp_4-s_57628740.png" target="_blank"></a>

以清單形式顯示:

<a href="http://s5.51cto.com/wyfs02/M00/8A/55/wKiom1gtbO-jxI-hAAI_yCtxdXQ190.png-wh_500x0-wm_3-wmp_4-s_4146323203.png" target="_blank"></a>

很多WMI對象可以使用cmdlet擷取,例如:

Get-WmiObject win32_service  == Get-Service

Get-WmiObject win32_Process  == Get-Process

本文轉自 曾哥最愛 51CTO部落格,原文連結:http://blog.51cto.com/zengestudy/1873738,如需轉載請自行聯系原作者

上一篇: yum安裝ansible
下一篇: 反射

繼續閱讀