天天看點

WMIC指令使用詳解

執行“wmic”指令啟動WMIC指令行環境。這個指令可以在XP或 .NET Server的标準指令行解釋器(cmd.exe)、Telnet會話或“運作”對話框中執行。這些啟動方法可以在本地使用,也可以通過.NET Server終端服務會話使用。

  第一次執行WMIC指令時,Windows首先要安裝WMIC,然後顯示出WMIC的指令行提示符。在WMIC指令行提示符上,指令以互動的方式執行。

wimic的運作方式可以有兩種法:

1、搞入wimic進入後輸入指令運作,鍵入wimic後出現wmic:root\cli>時你就可以輸入指令了,如輸入process顯示所有的程序。不知道有什麼指令時可以輸入用/?來顯示幫助。exit 是退出互動模式。具體幫助用法如下:

指令行幫助

  指令 例子 說明

  /? 或 -? 顯示所有全局開關和别名的文法

  / /? /user /? 顯示指定全局開關的資訊

  /? class /? 顯示某個指令的資訊

  /? memcache /? 顯示某個别名的資訊

  /? temperature get /? 顯示别名與動詞組合的資訊

  /?:Full irq get /?:Full 顯示動詞的幫助資訊

如:我要檢視process指令的幫助,鍵入:process/?後顯示如下:

wmic:root\cli>process /?

PROCESS - 程序管理。

提示: BNF 的别名用法。

(<alias> [WMIObject] | <alias> [<path where>] | [<alias>] <path where>) [<verb clause>].

用法:

PROCESS ASSOC [<format specifier>]

PROCESS CALL <method name> [<actual param list>]

PROCESS CREATE <assign list>

PROCESS DELETE

PROCESS GET [<property list>] [<get switches>]

PROCESS LIST [<list format>] [<list switches>]

2、用wimic 後面直接跟指令運作,如wmic process 就顯示了所有的程序了。這兩種運作方法就是:互動模式(Interactive mode)和非互動模式(Non-Interactive mode)

下面我們能過一些執行個體來說明用法:

=====================================================================

顯示程序的詳細資訊

輸入 process where name="maxthon.exe" list full

将顯示出mxathon.exe程序所有的資訊如下:

CommandLine="D:\mytools\Maxthon2\Maxthon.exe"

CSName=CHINA-46B1E8590

Description=Maxthon.exe

ExecutablePath=D:\mytools\Maxthon2\Maxthon.exe

ExecutionState=

Handle=684

HandleCount=2296

InstallDate=

KernelModeTime=3495000000

MaximumWorkingSetSize=1413120

MinimumWorkingSetSize=204800

Name=Maxthon.exe

OSName=Microsoft Windows XP Professional|C:\WINDOWS|

OtherOperationCount=307814

OtherTransferCount=60877207

PageFaults=1367971

PageFileUsage=89849856

ParentProcessId=1924

PeakPageFileUsage=90091520

PeakVirtualSize=385802240

PeakWorkingSetSize=94031872

Priority=8

PrivatePageCount=89849856

ProcessId=684

QuotaNonPagedPoolUsage=43496

QuotaPagedPoolUsage=257628

QuotaPeakNonPagedPoolUsage=72836

QuotaPeakPagedPoolUsage=271372

ReadOperationCount=85656

ReadTransferCount=121015982

SessionId=0

Status=

TerminationDate=

ThreadCount=57

UserModeTime=1778750000

VirtualSize=353206272

WindowsVersion=5.1.2600

WorkingSetSize=93716480

WriteOperationCount=30940

WriteTransferCount=24169673

******************************************************************************

停止、暫停和運作服務功能

啟動服務startservice,

停止服務stopservice,

暫停服務pauseservice

Service where caption="windows time" call stopservice ------停止服務

Service where caption="windows time" call startservice ------啟動服務

Service where name="w32time" call stopservice          ------停止服務,注意name和caption的差別。

caption 顯示服務名name服務名稱,如: telnet服務的顯示名稱是telnet 服務名稱是tlntsvr,還有Windows Time服務的名稱是w32time 顯示名稱是"Windows Time"要用引号引起來,主要是有一個空格。

好了具體看一下:輸入Service where caption="windows time" call startservice後有一個确認輸入y就可以了,傳回ReturnValue = 0;表示成功

wmic:root\cli>Service where caption="windows time" call startservice

執行 (startservice'>\\CHINA-46B1E8590\ROOT\CIMV2:Win32_Service.Name="W32Time")->startservice()

方法執行成功。

輸出參數:

instance of __PARAMETERS

{

        ReturnValue = 0;

};

wmic:root\cli>

================================================================================================

顯示出BIOS資訊 wmic bios list full

大家可能注意到了上面指令行中還有兩個參數list和full。list決定顯示的資訊格式與範圍,它有Brief、Full、Instance、 Status、System、Writeable等多個參數,full隻是它的一個參數,也是list的預設參數,表示顯示所有的資訊。其他幾個參數顧名思義,如Brief表示隻顯示摘要資訊,Instance表示隻顯示對象執行個體,Status表示顯示對象狀态,Writeable表示隻顯示該對象的可寫入的屬性資訊等。

************************************************************************=====================

停止程序的操作

例如,執行下面的指令将關閉正在運作的QQ.exe:

例1、wmic process where name='QQ.exe' call terminate

指令運作結束後,WMIC指令行提示出如下結果:

C:\>wmic process where name='QQ.exe' call terminate

執行 (terminate'>\\CHINA-46B1E8590\ROOT\CIMV2:Win32_Process.Handle="728")->terminate()

方法執行成功。

輸出參數:

instance of __PARAMETERS

{

        ReturnValue = 0;

};

例2、wmic process where name="qq.exe" delete

指令運作結束後,WMIC指令行提示出如下結果:

C:\>wmic process where name="qq.exe" delete

删除範例 \\CHINA-46B1E8590\ROOT\CIMV2:Win32_Process.Handle="2820"

範例删除成功。

======================================================================

列出所有的程序   wmic process

==================================================================

連接配接遠端電腦

★★連接配接遠端的電腦,不過好象對要開一些相應的服務

wmic /node:"192.168.203.131" /password:"" /user:"administrator"

BIOS - 基本輸入/輸出服務 (BIOS) 管理

★★檢視bios版本型号

wmic bios get Manufacturer,Name

WMIC設定IP位址

★★配置或更新IP位址:

wmic nicconfig where index=0 call enablestatic("192.168.1.5"), ("255.255.255.0") ;index=0說明是配置網絡接口1。

配置網關(預設路由):

wmic nicconfig where index=0 call setgateways("192.168.1.1"),(1)

COMPUTERSYSTEM - 計算機系統管理

★★檢視系統啟動選項,boot的内容

wmic COMPUTERSYSTEM get SystemStartupOptions

★★檢視工作組/域

wmic computersystem get domain

★★更改計算機名abc為123

wmic computersystem where "name='abc'" call rename 123

★★更改工作組google為MyGroup

wmic computersystem where "name='google'" call joindomainorworkgroup "","","MyGroup",1

CPU - CPU 管理

★★檢視cpu型号

wmic cpu get name

DATAFILE - DataFile 管理

★★查找e盤下test目錄(不包括子目錄)下的cc.cmd檔案

wmic datafile where "drive='e:' and path='\\test\\' and FileName='cc' and Extension='cmd'" list

★★查找e盤下所有目錄和子目錄下的cc.cmd檔案,且檔案大小大于1K

wmic datafile where "drive='e:' and FileName='cc' and Extension='cmd' and FileSize>'1000'" list

★★删除e盤下檔案大小大于10M的.cmd檔案

wmic datafile where "drive='e:' and Extension='cmd' and FileSize>'10000000'" call delete

★★删除e盤下test目錄(不包括子目錄)下的非.cmd檔案

wmic datafile where "drive='e:' and Extension<>'cmd' and path='test'" call delete

★★複制e盤下test目錄(不包括子目錄)下的cc.cmd檔案到e:\,并改名為aa.bat

wmic datafile where "drive='e:' and path='\\test\\' and FileName='cc' and Extension='cmd'" call copy "e:\aa.bat"

★★改名c:\hello.txt為c:\test.txt

wmic datafile "c:\\hello.txt" call rename c:\test.txt

★★查找h盤下目錄含有test,檔案名含有perl,字尾為txt的檔案

wmic datafile where "drive='h:' and extension='txt' and path like '%\\test\\%' and filename like '%perl%'" get name

DESKTOPMONITOR - 螢幕管理

★★擷取螢幕分辨率

wmic DESKTOPMONITOR where Status='ok' get ScreenHeight,ScreenWidth

DISKDRIVE - 實體磁盤驅動器管理

★★擷取實體磁盤型号大小等

wmic DISKDRIVE get Caption,size,InterfaceType

ENVIRONMENT - 系統環境設定管理

★★擷取temp環境變量

wmic ENVIRONMENT where "name='temp'" get UserName,VariableValue

★★更改path環境變量值,新增e:\tools

wmic ENVIRONMENT where "name='path' and username='<system>'" set VariableValue="%path%;e:\tools"

★★新增系統環境變量home,值為%HOMEDRIVE%%HOMEPATH%

wmic ENVIRONMENT create name="home",username="<system>",VariableValue="%HOMEDRIVE%%HOMEPATH%"

★★删除home環境變量

wmic ENVIRONMENT where "name='home'" delete

FSDIR - 檔案目錄系統項目管理

★★查找e盤下名為test的目錄

wmic FSDIR where "drive='e:' and filename='test'" list

★★删除e:\test目錄下除過目錄abc的所有目錄

wmic FSDIR where "drive='e:' and path='\\test\\' and filename<>'abc'" call delete

★★删除c:\good檔案夾

wmic fsdir "c:\\good" call delete

★★重命名c:\good檔案夾為abb

wmic fsdir "c:\\good" rename "c:\abb"

LOGICALDISK - 本地儲存裝置管理

★★擷取硬碟系統格式、總大小、可用空間等

wmic LOGICALDISK get name,Description,filesystem,size,freespace

NIC - 網絡界面控制器 (NIC) 管理

OS - 已安裝的作業系統管理

★★設定系統時間

wmic os where(primary=1) call setdatetime 20070731144642.555555+480

PAGEFILESET - 頁面檔案設定管理

★★更改目前頁面檔案初始大小和最大值

wmic PAGEFILESET set InitialSize="512",MaximumSize="512"

★★頁面檔案設定到d:\下,執行下面兩條指令

wmic pagefileset create name='d:\pagefile.sys',initialsize=512,maximumsize=1024

wmic pagefileset where"name='c:\\pagefile.sys'" delete

PROCESS - 程序管理

★★列出程序的核心資訊,類似任務管理器

wmic process list brief

★★結束svchost.exe程序,路徑為非C:\WINDOWS\system32\svchost.exe的

wmic process where "name='svchost.exe' and ExecutablePath<>'C:\\WINDOWS\\system32\\svchost.exe'" call Terminate

★★建立notepad程序

wmic process call create notepad

PRODUCT - 安裝包任務管理

★★安裝包在C:\WINDOWS\Installer目錄下

★★解除安裝.msi安裝包

wmic PRODUCT where "name='Microsoft .NET Framework 1.1' and Version='1.1.4322'" call Uninstall

★★修複.msi安裝包

wmic PRODUCT where "name='Microsoft .NET Framework 1.1' and Version='1.1.4322'" call Reinstall

SERVICE - 服務程式管理

★★運作spooler服務

wmic SERVICE where name="Spooler" call startservice

★★停止spooler服務

wmic SERVICE where name="Spooler" call stopservice

★★暫停spooler服務

wmic SERVICE where name="Spooler" call PauseService

★★更改spooler服務啟動類型[auto|Disabled|Manual] 釋[自動|禁用|手動]

wmic SERVICE where name="Spooler" set StartMode="auto"

★★删除服務

wmic SERVICE where name="test123" call delete

SHARE - 共享資源管理

★★删除共享

wmic SHARE where name="e$" call delete

★★添加共享

WMIC SHARE CALL Create "","test","3","TestShareName","","c:\test",0

SOUNDDEV - 聲音裝置管理

wmic SOUNDDEV list

STARTUP - 使用者登入到計算機系統時自動運作指令的管理

★★檢視msconfig中的啟動選項

wmic STARTUP list

SYSDRIVER - 基本服務的系統驅動程式管理

wmic SYSDRIVER list

USERACCOUNT - 使用者帳戶管理

★★更改使用者administrator全名為admin

wmic USERACCOUNT where name="Administrator" set FullName="admin"

★★更改使用者名admin為admin00

wmic useraccount where "name='admin" call Rename admin00

================================================擷取更新檔資訊

★★檢視目前系統打了哪些更新檔

/node:legacyhost qfe get hotfixid

檢視CPU目前的速度

★★cpu目前的速度

wmic cpu get CurrentClockSpeed

遠端計算機的遠端桌面連接配接

★★WMIC指令開啟遠端計算機的遠端桌面連接配接

執行wmic /node:192.168.1.2 /USER:administrator

PATH win32_terminalservicesetting WHERE (__Class!="") CALL SetAllowTSConnections 1

具體格式:

wmic /node:"[full machine name]" /USER:"[domain]\[username]"

PATH win32_terminalservicesetting WHERE (__Class!="") CALL SetAllowTSConnections 1

wmic 擷取程序名稱以及可執行路徑:

wmic process get name,executablepath

wmic 删除指定程序(根據程序名稱):

wmic process where name="qq.exe" call terminate

或者用

wmic process where name="qq.exe" delete

wmic 删除指定程序(根據程序PID):

wmic process where pid="123" delete

wmic 建立新程序

wmic process call create "C:\Program Files\Tencent\QQ\QQ.exe"

在遠端機器上建立新程序:

wmic /node:192.168.201.131 /user:administrator /password:123456 process call create cmd.exe

關閉本地計算機

wmic process call create shutdown.exe

重新開機遠端計算機

wmic /node:192.168.1.10/user:administrator /password:123456 process call create "shutdown.exe -r -f -m"

更改計算機名稱

wmic computersystem where "caption='%ComputerName%'" call rename newcomputername

更改帳戶名

wmic USERACCOUNT where "name='%UserName%'" call rename newUserName

wmic 結束可疑程序(根據程序的啟動路徑)

wmic process where "name='explorer.exe' and executablepath<>'%SystemDrive%\\windows\\explorer.exe'" delete

wmic 擷取實體記憶體

wmic memlogical get TotalPhysicalMemory|find /i /v "t"

wmic 擷取檔案的建立、通路、修改時間

@echo off

for /f "skip=1 tokens=1,3,5 delims=. " %%a in ('wmic datafile where name^="c:\\windows\\system32\\notepad.exe" get CreationDate^,LastAccessed^,LastModified') do (

set a=%%a

set b=%%b

set c=%%c

echo 檔案: c:\windows\system32\notepad.exe

echo.

echo 建立時間: %a:~0,4% 年 %a:~4,2% 月 %a:~6,2% 日 %a:~8,2% 時 %a:~10,2% 分 %a:~12,2% 秒

echo 最後通路: %b:~0,4% 年 %b:~4,2% 月 %b:~6,2% 日 %b:~8,2% 時 %b:~10,2% 分 %b:~12,2% 秒

echo 最後修改: %c:~0,4% 年 %c:~4,2% 月 %c:~6,2% 日 %c:~8,2% 時 %c:~10,2% 分 %c:~12,2% 秒

)

echo.

pause

wmic 全盤搜尋某檔案并擷取該檔案所在目錄

for /f "skip=1 tokens=1*" %i in ('wmic datafile where "FileName='qq' and extension='exe'" get drive^,path') do (set "qPath=%i%j"%qPath:~0,-3%)

擷取螢幕分辨率 wmic DESKTOPMONITOR where Status='ok' get ScreenHeight,ScreenWidth

wmic PageFileSet set InitialSize="512",MaximumSize="512"

設定虛拟記憶體到E盤,并删除C槽下的頁面檔案,重新開機計算機後生效

wmic PageFileSet create name="E:\\pagefile.sys",InitialSize="1024",MaximumSize="1024"

wmic PageFileSet where "name='C:\\pagefile.sys'" delete

獲得程序目前占用的記憶體和最大占用記憶體的大小:

wmic process where caption='filename.exe' get WorkingSetSize,PeakWorkingSetSize

以KB為機關顯示

@echo off

for /f "skip=1 tokens=1-2 delims= " %%a in ('wmic process where caption^="conime.exe" get WorkingSetSize^,PeakWorkingSetSize') do (

set /a m=%%a/1024

set /a mm=%%b/1024

echo 程序conime.exe現在占用記憶體:%m%K;最高占用記憶體:%mm%K

)

pause

遠端打開計算機遠端桌面

wmic /node:%pcname% /USER:%pcaccount% PATH win32_terminalservicesetting WHERE (__Class!="") CALL SetAllowTSConnections 1