您的位置:首页 > 运维架构 > Shell

PowerShell Function之获取ComputerSystem信息

2015-11-08 17:10 405 查看
这是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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: