1. 程式人生 > >WMIC命令使用詳解

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