1. 程式人生 > >wmic命令用法小例

wmic命令用法小例

brief isa 讀者 用戶名 creation not 詳細信息 services cname

wmic就是wmic.exe,位於windows目錄底下,是一個命令行程序。WMIC可以以兩種模式執行:交互模式(Interactive mode)和非交互模式(Non-Interactive mode),經常使用Netsh命令行的讀者應該非常熟悉這兩種模式。

交互模式。如果你在命令提示符下或通過"運行"菜單只輸入WMIC,都將進入WMIC的交互模式,每當一個命令執行完畢後,系統還會返回到WMIC提示符下,如"Root\cli",交互模式通常在需要執行多個WMIC指令時使用。交互模式有時還會對一些敏感的操作要求確認,比如刪除操作,最大限度地防止用戶操作出現失誤。

非交互模式。非交互模式是指將WMIC指令直接作為WMIC的參數放在WMIC後面,當指令執行完畢後再返回到普通的命令提示符下,而不是進入到WMIC上下文環境中。WMIC的非交互模式主要用於批處理或者其他一些腳本文件中,我在本文中一律用●非交互模式●示例。

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

應用舉例:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

顯示進程的詳細信息

輸入 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

執行 (\\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

執行 (\\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"&@echo %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

wmic命令用法小例