计算机信息:简单快速



通常,您需要快速,简短而有信息地获取有关固定式计算机或膝上型计算机的信息,而无需其他软件,也无需“打开机盖”。

例如,可以使用Windows或PowerShell的命令行工具来实现。
CMD-Windows的任何版本中经过时间考验的功能。
此外,对于简单的管理任务,cmd更为常用,并且更方便。

哪个更好-CMD或PowerShell?我还没有准备好明确回答这个问题。
但是,没有什么阻止我们同时使用它们,这完全取决于任务。

我们不会收集有关PC的所有信息-为此,有许多专用软件!


使用CMD实现。


我们将使用Windows环境的变量和WMI脚本的执行来收集信息。

要显示Windows中的所有环境变量及其值,请使用set命令。

使用WMIC命令可以获取有关硬件和系统的信息,管理进程及其组件以及使用Windows Management Instrumentation(Windows Management Instrumentation或WMI)的功能更改设置。

您可以通过以下命令获得有关使用wmic.exe实用工具的提示:
  • WMIC /?-显示一般帮助。
  • wmic /?:简要-显示简要帮助。
  • wmic /?:全-显示完整帮助。


我们将使用:
  • BASEBOARD(主板管理);
  • COMPUTERSYSTEM(计算机控制);
  • CPU(CPU控制);
  • DISKDRIVE(物理磁盘管理);
  • MEMORYCHIP(内存芯片信息)。


该脚本包含许多FOR循环。
FOR / F的一个独特功能是能够处理标记,并支持其他关键字:
  • 跳过(从文件的开头跳过一定数量的已处理行);
  • delims(默认情况下,指定其他定界符,空格和制表符);
  • 令牌(循环主体中接收到的令牌(子字符串)的数量以及分隔符的细分限制)。您还可以设置一个特定的令牌号,该令牌号将属于第一个循环变量。
  • usebackq(更改在IN(...)内使用引号的规则)。


下面是一个脚本。
@echo off
:    
set fname=pcinfo.txt
: 
Echo pcname: %computername% >>%fname%
:IP-    
FOR /F "usebackq tokens=2 delims=[]" %%i IN (`ping %Computername% -n 1 -4`) DO if not "%%i"=="" Set ip=%%i
Echo IP %computername%: %ip% >>%fname%
:  
Echo username: %username%  >>%fname%
: 
set cmd=wmic computersystem get model
for /f "skip=1 delims=" %%Z in ('%cmd%') do (
    set _pn=%%Z
	GOTO BREAK1
)
:BREAK1
echo CS Model: %_pn% >>%fname%
:
SETLOCAL ENABLEDELAYEDEXPANSION
set mmr=0
for /f "skip=1 delims=" %%i in ('wmic cpu get name') do (
for /f "tokens=1-2 delims=" %%A in ("%%i") do (
set CPULbl=%%A
set /a mmr=!mmr!+1
echo CPU !mmr!: !CPULbl! >>%fname%
))
: 
set cmd=wmic baseboard get product
for /f "skip=1 delims=" %%Z in ('%cmd%') do (
    set _mb=%%Z
    GOTO BREAK2
)
:BREAK2
echo MB: %_mb% >>%fname%
: 
SETLOCAL ENABLEDELAYEDEXPANSION
set mmr=0
for /f "skip=1 delims=" %%i in ('WMIC MemoryChip get BankLabel^,DeviceLocator^,PartNumber^,Speed^,Capacity') do (
for /f "tokens=1-5 delims=" %%A in ("%%i") do (
set BnkLbl=%%A
set /a mmr=!mmr!+1
echo Memory !mmr!: !BnkLbl! >>%fname%
wmic MEMORYCHIP get banklabel, partnumber, capacity, speed,  manufacturer
))
:
SETLOCAL ENABLEDELAYEDEXPANSION
set mmr=0
for /f "skip=1 delims=" %%i in ('wmic diskdrive get model^,size') do (
for /f "tokens=1-2 delims=" %%A in ("%%i") do (
set HDDLbl=%%A
set /a mmr=!mmr!+1
echo DISK !mmr!: !HDDLbl! >>%fname%
))


使用PowerShell实施。


在PowerShell中,在运行脚本之前,需要运行一个命令,该命令允许为当前shell会话执行未签名的脚本:
Set-ExecutionPolicy RemoteSigned -Scope Process

信息将主要使用Get-WmiObject -Class win32收集,一切都很简单,使用循环即可。

我们将使用:
  • Get-WmiObject-类win32_processor;
  • Get-WmiObject-类win32_baseboard;
  • Get-WmiObject Win32_PhysicalMemory;
  • Get-PhysicalDisk;
  • Get-WmiObject-Win32_ComputerSystem类;
  • Get-WmiObject Win32_NetworkAdapter;
  • Win32_NetworkAdapterConfiguration。


确定活动网卡的IP地址有一些小困难。为此,在获取Win32_NetworkAdapter类的mac地址信息和Win32_NetworkAdapterConfiguration类的IP地址并将其组合为一个请求之后,我们使用NetConnectionStatus筛选器-“ 2”。
PS C:\Users\admin> Get-WmiObject Win32_NetworkAdapter -Filter 'NetConnectionStatus=2'
ServiceName      : Qcamain10x64
MACAddress       : 58:00:E3:7D:87:3F
AdapterType      : Ethernet 802.3
DeviceID         : 1
Name             : Qualcomm Atheros QCA61x4A Wireless Network Adapter
NetworkAddresses :
Speed            : 144400000

为了获得活动网卡的MACAddress的网络参数,我们另外阅读Win32_NetworkAdapterConfiguration。

下面是一个脚本。
#    
$fname = "pcinfo.txt"
$CPU = Get-WmiObject  -Class win32_processor
$MB = Get-WmiObject  -Class win32_baseboard
$MEM = Get-WmiObject Win32_PhysicalMemory
$DD = Get-PhysicalDisk
$pcn = Get-WmiObject -Class Win32_ComputerSystem

# 
"pcname:	"+$pcn.Name | Out-File -FilePath $fname -Append -Encoding Default
#IP-    
Get-WmiObject Win32_NetworkAdapter -Filter 'NetConnectionStatus=2' |
ForEach-Object {
$pcip = 1 | Select-Object IP
$config = $_.GetRelated('Win32_NetworkAdapterConfiguration') 
$pcip.IP = $config | Select-Object -expand IPAddress
$pcip
}
foreach($aip in $pcip) {
"IP:	"+$aip.IP | Out-File -FilePath $fname -Append -Encoding Default
}
#  
"username:	"+$pcn.PrimaryOwnerName | Out-File -FilePath $fname -Append -Encoding Default
# 
"CS Model:	"+$pcn.Model | Out-File -FilePath $fname -Append -Encoding Default
#
$num = 0
foreach($processor in $CPU) {
$num = $num+1
"CPU "+$num+":	"+$processor.Name | Out-File -FilePath $fname -Append -Encoding Default
}
# 
"MB:	"+$MB.Product | Out-File -FilePath $fname -Append -Encoding Default
# 
$num = 0
foreach($memory in $MEM) {
$num = $num+1
"MEMORY "+$num+":	"+$memory.PartNumber+"	"+$memory.Capacity+"	"+$memory.Speed | Out-File -FilePath $fname -Append -Encoding Default
}
#
$num = 0
foreach($disk in $DD) {
$num = $num+1
"DISK "+$num+":	"+$disk.FriendlyName+"	"+$disk.Size+"	"+$disk.MediaType | Out-File -FilePath $fname -Append -Encoding Default
}

All Articles