https://blog.csdn.net/enweitech/article/details/51982114
在CMD和Powershell中 使用WMIC
先決條件:
a. 啟動Windows Management Instrumentation服務,開放TCP135端口。
b. 本地安全策略的“網絡訪問: 本地帳戶的共享和安全模式”應設為“經典-本地用戶以自己的身份驗證”。
第一次執行WMIC命令時,Windows首先要安裝WMIC,然后顯示出WMIC的命令行提示符。在WMIC命令行提示符上,命令以交互的方式執行。
執行“wmic”命令啟動WMIC命令行環境。這個命令可以在XP或 .NET Server的標准命令行解釋器(cmd.exe)、Telnet會話或“運行”對話框中執行。這些啟動方法可以在本地使用,也可以通過.NET Server終端服務會話使用。
wimic的運行方式可以有兩種法:
1、搞入wimic進入后輸入命令運行,鍵入wimic后出現wmic:root\cli>時你就可以輸入命令了,如輸入process顯示所有的進程。不知道有什么命令時可以輸入用/?來顯示幫助。exit 是退出交互模式。
2、用wimic 后面直接跟命令運行,如wmic process 就顯示了所有的進程了。這兩種運行方法就是:交互模式(Interactive mode)和非交互模式(Non-Interactive mode)
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.1.10 /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
檢測是否插入U盤的批處理
復制代碼代碼如下:
@echo off
((wmic logicaldisk where "drivetype=2" get name|find "無可用范例")>nul 2>nul)||for /f "skip=1 tokens=* delims=" %%i in ('wmic logicaldisk where "drivetype=2" get name') do echo U盤盤符是 %%i
pause
rem 查看cpu
wmic cpu list brief
rem 查看物理內存
wmic memphysical list brief
rem 查看邏輯內存
wmic memlogical list brief
rem 查看緩存內存
wmic memcache list brief
rem 查看虛擬內存
wmic pagefile list brief
rem 查看網卡
wmic nic list brief
rem 查看網絡協議
wmic netprotocal list brief
【例】將當前系統BIOS,CPU,主板等信息輸出到一個HTML網頁文件,命令如下:
::得到系統信息.bat,運行bat文件即可
::系統信息輸出到HTML文件,查看幫助: wmic /?
::wmic [系統參數名] list [brief|full] /format:hform >|>> [文件名]
wmic bios list brief /format:hform > PCinfo.html
wmic baseboard list brief /format:hform >>PCinfo.html
wmic cpu list full /format:hform >>PCinfo.html
wmic os list full /format:hform >>PCinfo.html
wmic computersystem list brief /format:hform >>PCinfo.html
wmic diskdrive list full /format:hform >>PCinfo.html
wmic memlogical list full /format:hform >>PCinfo.html
PCinfo.html
【經典案例語句】
- wmic /node:"192.168.1.20" /user:"domain\administrator" /password:"123456"
2.【硬件管理】:
獲取磁盤資料:
wmic DISKDRIVE get deviceid,Caption,size,InterfaceType
獲取分區資料:
wmic LOGICALDISK get name,Description,filesystem,size,freespace
獲取CPU資料:
wmic cpu get name,addresswidth,processorid
獲取主板資料:
wmic BaseBoard get Manufacturer,Product,Version,SerialNumber
獲取內存數:
wmic memlogical get totalphysicalmemory
獲得品牌機的序列號:
wmic csproduct get IdentifyingNumber
獲取聲卡資料:
wmic SOUNDDEV get ProductName
獲取屏幕分辨率
wmic DESKTOPMONITOR where Status='ok' get ScreenHeight,ScreenWidth
- PROCESS【進程管理】:
列出進程
wmic process list brief
(Full顯示所有、Brief顯示摘要、Instance顯示實例、Status顯示狀態)
wmic 獲取進程路徑:
wmic process where name="jqs.exe" get executablepath
wmic 創建新進程
wmic process call create notepad
wmic process call create "C:\Program Files\Tencent\QQ\QQ.exe"
wmic process call create "shutdown.exe -r -f -t 20"
wmic 刪除指定進程:
wmic process where name="qq.exe" call terminate
wmic process where processid="2345" delete
wmic process 2345 call terminate
wmic 刪除可疑進程
wmic process where "name='explorer.exe' and executablepath<>'%SystemDrive%\windows\explorer.exe'" delete
wmic process where "name='svchost.exe' and ExecutablePath<>'C:\WINDOWS\system32\svchost.exe'" call Terminate
- USERACCOUNT【賬號管理】:
更改當前用戶名
WMIC USERACCOUNT where "name='%UserName%'" call rename newUserName
WMIC USERACCOUNT create /?
- SHARE【共享管理】:
建立共享
WMIC SHARE CALL Create "","test","3","TestShareName","","c:\test",0
(可使用 WMIC SHARE CALL Create /? 查看create后的參數類型)
刪除共享
WMIC SHARE where name="C$" call delete
WMIC SHARE where path='c:\test' delete
- SERVICE【服務管理】:
更改telnet服務啟動類型[Auto|Disabled|Manual]
wmic SERVICE where name="tlntsvr" set startmode="Auto"
運行telnet服務
wmic SERVICE where name="tlntsvr" call startservice
停止ICS服務
wmic SERVICE where name="ShardAccess" call stopservice
刪除test服務
wmic SERVICE where name="test" call delete
- FSDIR【目錄管理】
列出c盤下名為test的目錄
wmic FSDIR where "drive='c:' and filename='test'" list
刪除c:\good文件夾
wmic fsdir "c:\test" call delete
重命名c:\test文件夾為abc
wmic fsdir "c:\test" rename "c:\abc"
wmic fsdir where (name='c:\test') rename "c:\abc"
復制文件夾
wmic fsdir where name='d:\test' call copy "c:\test"
7.datafile【文件管理】
重命名
wmic datafile "c:\test.txt" call rename c:\abc.txt
8.【任務計划】:
wmic job call create "notepad.exe",0,0,true,false,154800.000000+480
wmic job call create "explorer.exe",0,0,1,0,154600.000000+480