首先我们要用到Get-WmiObject获取到
WMI中的Win32类监控和管理系统的硬件和特征信息
<##### WMI中的Win32类监控和管理系统的硬件和特征信息
Win32_Processor // CPU 处理器
Win32_PhysicalMemory // 物理内存
Win32_Keyboard // 键盘
Win32_PointingDevice // 点输入设备,如鼠标
Win32_DiskDrive // 硬盘驱动器
Win32_CDROMDrive // 光盘驱动器
Win32_BaseBoard // 主板
Win32_BIOS // BIOS 芯片
Win32_ParallelPort // 并口
Win32_SerialPort // 串口
Win32_SoundDevice // 多媒体设置
Win32_USBController // USB 控制器
Win32_NetworkAdapter // 网络适配器
Win32_NetworkAdapterConfiguration // 网络适配器设置
Win32_Printer // 打印机
Win32_PrinterConfiguration // 打印机设置
Win32_PrintJob // 打印机任务
Win32_TCPIPPrinterPort // 打印机端口
Win32_POTSModem // MODEM
Win32_POTSModemToSerialPort // MODEM 端口
Win32_DesktopMonitor // 显示器
Win32_VideoController // 显卡细节。
Win32_VideoSettings // 显卡支持的显示模式。
Win32_TimeZone // 时区
Win32_SystemDriver // 驱动程序
Win32_DiskPartition // 磁盘分区
Win32_LogicalDisk // 逻辑磁盘
Win32_LogicalMemoryConfiguration // 逻辑内存配置
Win32_PageFile // 系统页文件信息
Win32_PageFileSetting // 页文件设置
Win32_BootConfiguration // 系统启动配置
Win32_OperatingSystem // 操作系统信息
Win32_StartupCommand // 系统自动启动程序
Win32_Service // 系统安装的服务
Win32_Group // 系统管理组
Win32_GroupUser // 系统组帐号
Win32_UserAccount // 用户帐号
Win32_Process // 系统进程
Win32_Thread // 系统线程
Win32_Share // 共享
Win32_NetworkClient // 已安装的网络客户端
Win32_NetworkProtocol // 已安装的网络协议 ######>
function Mypcinfo
{
param([string]$savepath)
cls
$system = Get-WmiObject -Class Win32_ComputerSystem
#获取计算机域名、型号
$domainname = $system.Domain
$model = $system.Model
$user=$system.UserName
#获取计算机IP地址,取IP不为空的网卡IP地址
$ips = gwmi Win32_NetworkAdapterConfiguration |?{ $_.IPAddress -ne $null}
$ipdata=$null
foreach ($ip in $ips )
{
#查找对应网速
$ok=Get-WmiObject -Class Win32_NetworkAdapter -Filter "Description='$($ip.Description)'"|Select-Object Description,speed,MACAddress
$ipdata+="网卡名称:$($ip.Description) IP地址:$($ip.IPAddress[0]) 速率:$($ok.speed/1000/1000)M MAC地址:$($ok.MACAddress)`n`r"
}
$ipdata=$ipdata.Substring(0,$ipdata.Length-2)
#获取操作系统版本
$os = Get-WmiObject -Class Win32_OperatingSystem
#获取操作系统版本
$os_caption = $os.Caption
If ($os_caption.Contains("Server 2008 R2 Enterprise"))
{$os_caption_s = "Win2008"}
ElseIf ($os_caption.Contains("Server 2003 Enterprise"))
{$os_caption_s = "Win2003"}
Else {$os_caption_s = $os.Caption}
$osversion = $os_caption_s + " " + $os.OSArchitecture.Substring(0,2) + "bit"
#获取CPU名称、单颗CPU核心数量*CPU个数
$cpus = Get-WmiObject -Class win32_processor
$cpunamecore=$null
Foreach ($cpu in $cpus)
{
$cpunamecore += "$($cpu.name) $($cpu.NumberOfCores)核 $($cpu.NumberOfLogicalProcessors)线程`n`r"
}
$cpunamecore = $cpunamecore.Substring(0, $cpunamecore.Length-2)
#获取内存大小
$memorys = Get-WmiObject -Class Win32_PhysicalMemory
$memorylist = $null
$memorysize_sum = $null
$memorysize_sum_n=0
Foreach ($memory in $memorys)
{
#$memory|Select-Object * |fc
$memorylist +="品牌:$($memory.Manufacturer.Trim()),"+"主频:$($memory.Speed)MZ,容量:" +($memory.capacity/1024/1024/1024).tostring("F1")+"GB`n`r"
}
$memorylist=$memorylist.Substring(0,$memorylist.Length-2)
#获取磁盘信息
$disks = Get-WmiObject -Class Win32_Diskdrive
$disklist = $null
#$disksize_sum = $null
Foreach ($disk in $disks)
{
$disklist += ($disk.deviceid.replace("\\.\PHYSICALDRIVE","Disk") +":型号,$($disk.model) 容量," + [int]($disk.size/1024/1024/1024)+"GB`n`r")
#$disksize_sum+=$disk.size
}
$disklist= $disklist.Substring(0, $disklist.Length-2)
#获取计算机序列号、制造商
$bios = Get-WmiObject -Class Win32_BIOS
$sn = $bios.SerialNumber
If ($sn.Substring(0,6) -eq "VMware")
{$sn = "VMware"}
If ($bios.Manufacturer.contains("Dell"))
{$manufacturer = "Dell"}
Elseif ($bios.Manufacturer.contains("HP"))
{$manufacturer = "HP"}
Elseif ($bios.Manufacturer.contains("Microsoft"))
{
$manufacturer = "Microsoft"
$sn = ""
}
Else {$manufacturer = $bios.Manufacturer}
$type = $manufacturer + " " + $model
if ($type.contains("Microsoft Virtual Machine"))
{$type = "Hyper-V"}
#获取声卡信息
$sound=Get-WmiObject -Class Win32_SoundDevice
$soundnew=$null
foreach ($i in $sound)
{
$soundnew+="$($i.name)`n`r"
}
$soundnew=$soundnew.Substring(0,$soundnew.Length-2)
$soundnew=($soundnew -split '\n\r'|Sort-Object -Unique) -join "`n`r"
#获取显卡信息
$view=Get-WmiObject -Class Win32_VideoController
$viewnew=$null
foreach ($item in $view)
{
#判断是否小于1G,显示M
$xc=$item.AdapterRAM/1024/1024/1024
switch ($xc)
{
{$_ -lt 1} {$xcdata="$([math]::Ceiling($item.AdapterRAM/1024/1024))M"}
Default {$xcdata="$([math]::Ceiling($item.AdapterRAM/1024/1024/1024))G"}
}
$viewnew+= "名称:$($item.Caption),显存大小:$($xcdata)`n`r"
}
$viewnew=$viewnew.Substring(0,$viewnew.Length-2)
#获取主板信息
$basebord=Get-WmiObject -Class Win32_BaseBoard -Property *
$basebordnew="$($basebord.Manufacturer) $($basebord.Product)"
#获取打印机信息
$aa=Get-WmiObject -Class win32_printer -Filter "PortName like '%USB%' and Local=True"|select @{N="printer";e={
if($_.WorkOffline -eq $true){return "$($_.DriverName)(离线)"} else{ return "$($_.DriverName)(在线)"}
}}
$pris=$aa.printer -join ','
#获取显示器信息
$Screen=Get-WmiObject -Class Win32_DesktopMonitor|Select-Object @{l="显示器";e={$($_.PNPDeviceID -split '\\')[1]}}
$Scr=$Screen.显示器 -join ','
$Infors= [ordered]@{计算机名=$env:ComputerName;当前用户=$user;网络=$ipdata;系统版本= $osversion;内存=$memorylist;
CPU=$cpunamecore;工作组或域=$domainname;硬盘=$disklist;BIOS版本=$type;主板制造商=$basebordnew;声卡=$soundnew;显卡=$viewnew;显示器=$Scr;打印机=$pris}
$news=[pscustomobject]$Infors
$news
#更新CSV
if(Test-Path $savepath){
$list=Import-Csv $savepath -Encoding Default
if($list.计算机名.IndexOf($news.计算机名) -eq -1){
$news|Export-Csv -NoTypeInformation $savepath -Encoding Default -Append -Force
}
}else{
$news|Export-Csv -NoTypeInformation $savepath -Encoding Default -Force}
Write-Host "你的主机最大支持内存:$((Get-WmiObject -Class Win32_PhysicalMemoryarray).MaxCapacity/1024/1024)G" -ForegroundColor Red
}
mypcinfo -savepath C:\统计.csv
得到的结果如下