首页 > 解决方案 > Insert win32_PhysicalMemory PowerShell data into SQL Server table

问题描述

I am trying to insert all the data from Get-WmiObject win32_PhysicalMemory PowerShell code into SQL Server 2012:

$MemoryObjects = Get-WmiObject win32_PhysicalMemory  
foreach($i in
$MemoryObjects) {  
$Sn = $computerBIOS.SerialNumber
$Item = @{label=$_.Label}
$Des = $_.Description
 $InsertQuery=" INSERT INTO [dbo].[MyTable]
        ([SerialNumber]
        ,[Item]
        ,[Value])
        VALUES
        ('$Sn'
        ,'$Item'
        ,'$Des')
        GO
        "
Invoke-SQLcmd -ServerInstance 'ServerName' -query $insertquery -U Username -P 'Password' -Database MyDB  }

The code only inserts the serial number, it does not insert the label and its value. Any suggestion on how to do that? For each memory module, there are around 40 values, some of them are shown in the picture below. My SQL Server table has three columns (all varchar datatype). The first column is for the serial number, the second is for the label and the last one for the value. I am not sure how the last two columns.

enter image description here

标签: powershellsql-server-2012windows-10windows-7-x64

解决方案


推荐阅读