这是PowerShell Function系列的第二篇,聊聊ComputerSystem的信息。别混淆了哈,上一篇讲的是Operatingsystem。会不会有人傻傻分不清?那么请下以下截图,有个初步的分类吧。
同样的,可以运行以下命令来看看Win32_ComputerSystem到底有哪些ExpandProperty。
Get-CimInstance Win32_ComputerSystem | select -ExpandProperty CimInstanceProperties | Export-Csv system.csv -NoTypeInformation -Encoding UTF8
有59个属性,属性太多了,本次我们只需要获取manufacturer,RAM,Sockets和Cores的信息。同样地,这些信息不能直接get出来,依然需要创建一个数组给他们添加输出的属性。直接上脚本吧。
$cs = Get-WmiObject -class Win32_ComputerSystem -ComputerName $ComputerName
$props = @{'Model'=$cs.model;
'Manufacturer'=$cs.manufacturer;
'RAM (GB)'="{0:N2}" -f ($cs.totalphysicalmemory / 1GB);
'Sockets'=$cs.numberofprocessors;
'Cores'=$cs.numberoflogicalprocessors}
New-Object -TypeName PSObject -Property $props
以上是我的笔记本信息,可以通过以下截图对比看下信息是不是正确的。
最后,写成function
unction Get-InfoCompSystem {
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)][string]$ComputerName
)
$cs = Get-WmiObject -class Win32_ComputerSystem -ComputerName $ComputerName
$props = @{'Manufacturer'=$cs.manufacturer;
'RAM (GB)'="{0:N2}" -f ($cs.totalphysicalmemory / 1GB);
'Sockets'=$cs.numberofprocessors;
'Cores'=$cs.numberoflogicalprocessors}
New-Object -TypeName PSObject -Property $props
}