无法使用PowerShell获取物理内存信息

时间:2009-09-25 17:01:16

标签: powershell powershell-v1.0

我无法使用PowerShell从Windows管理对象获取物理内存信息。列在excel电子表格中生成,但Winmgmt对象中没有找到数据。

$strComputer = “.”

$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()

$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,2) = “Bank Label”
$Sheet.Cells.Item(1,3) = “Capacity”
$Sheet.Cells.Item(1,4) = “Caption”
$Sheet.Cells.Item(1,5) = “Data Width”
$Sheet.Cells.Item(1,6) = “Description”
$Sheet.Cells.Item(1,7) = “Device Locator”
$Sheet.Cells.Item(1,8) = "Form Factor"

$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 8
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True

$intRow = 2
$colItems = get-wmiobject -class “Win32_PhysicalMemory” -namespace “root CIMV2" -computername $strComputer

foreach ($objItem in $colItems) {
$Sheet.Cells.Item($intRow,1)= $strComputer
$Sheet.Cells.Item($intRow,2) = $objItem.BankLabel
$Sheet.Cells.Item($intRow,3) = $objItem.Capacity
$Sheet.Cells.Item($intRow,4) = $objItem.Caption
$Sheet.Cells.Item($intRow,5) = $objItem.DataWidth
$Sheet.Cells.Item($intRow,6) = $objItem.Description
$Sheet.Cells.Item($intRow,7) = $objItem.DeviceLocator
$Sheet.Cells.Item($intRow,8) = $objItem.FormFactor

$intRow = $intRow + 1

}


$WorkBook.EntireColumn.AutoFit()
Clear

1 个答案:

答案 0 :(得分:2)

我认为您对Get-WmiObject的来电不正确。以下似乎有效:

$strComputer = “.”

$colItems = Get-WmiObject Win32_PhysicalMemory -namespace root\CIMV2 -computername $strComputer
#$colItems = get-wmiobject -class “Win32_PhysicalMemory” -namespace “root CIMV2" -computername $strComputer

foreach ($objItem in $colItems) {
    Write-Host "BankLabel    " $objItem.BankLabel
    Write-Host "Capacity     " $objItem.Capacity
    Write-Host "Caption      " $objItem.Caption
    Write-Host "DataWidth    " $objItem.DataWidth
    Write-Host "Description  " $objItem.Description
    Write-Host "DeviceLocator" $objItem.DeviceLocator
    Write-Host "FormFactor   " $objItem.FormFactor
}