将数组插入SQL和检索格式

时间:2019-07-18 17:43:56

标签: sql-server powershell

我正在将自定义对象的集合发送到SQL。我的对象看起来像这样:

aa                              : True
phone_number                    :
manual_scan_time                : 0
ip_addr                         : 10.50.0.147
last_logon_user_guid            : 286c1410eb470e4ea31dd17e9c1eee31
urlfilter_violated              : 0
last_connect_time               : 1563379779
id                              : 0542c82cada09040be79bfb6e54fd119
spyware_detected                : 0
arch                            : x64
sched_start_time                : 1562922019
virus_detected                  : 0
platform                        : Win Server 2012 R2
version                         : 6.6.2457/14.1.1516
manual_aggressive_complete_time : 0
online                          : True
type                            : 1
status                          : 1
spam_detected                   : 0
sched_complete_time             : 1562926028
sched_scan_time                 : 1559294629
pop3_scan                       : False
manual_aggressive_start_time    : 0
last_logon_user_account         : jdoe
name                            : server01
ss_service                      : True
scan_mode                       : 0
manual_complete_time            : 0
mac_addr                        : 2B:41:38:B4:34:46
components                      : {@{version=0; type=1}, @{version=11.000.1006; type=3}, @{version=15.239.00; type=14}}
manual_start_time               : 0
last_logon_user_domain          : acme
created_at                      : 1510519578
last_connect_time_human         : 7/17/2019 4:09:39 PM
CustomerName                    : Acme Corp

看“ components”属性,我看到它是一个数组,没有在数据库中设置。因此,我开始研究如何将其转换为字符串,以便以后提取它并将其转换回该数组。

我正在遍历这样的对象:

$sqlValues = New-Object Collections.ArrayList
$allComputers.computers | ForEach-Object {
    $_ | ForEach-Object {
        $_.PsObject.Properties | ForEach-Object {
            If ($_.Value -match "'") {
                $_.Value = $_.Value.Replace("'", "''")
            }
        }
    }

    $sqlValues.Add("('$($_.aa)', '$($_.phone_number)', '$($_.manual_scan_time)', '$($_.ip_addr)', '$($_.last_logon_user_guid)', '$($_.urlfilter_violated)', '$($_.last_connect_time)', '$($_.id)', '$($_.spyware_detected)', '$($_.arch)', '$($_.sched_start_time)', '$($_.virus_detected)', '$($_.platform)', '$($_.version)', '$($_.manual_aggressive_complete_time)', '$($_.online)', '$($_.type)', '$($_.status)', '$($_.spam_detected)', '$($_.sched_complete_time)', '$($_.sched_scan_time)', '$($_.pop3_scan)', '$($_.manual_aggressive_start_time)', '$($_.last_logon_user_account)', '$($_.name)', '$($_.ss_service)', '$($_.scan_mode)', '$($_.manual_complete_time)', '$($_.mac_addr)', '$($_.components)', '$($_.manual_start_time)', '$($_.last_logon_user_domain)', '$($_.created_at)', '$($_.last_connect_time_human)', '$($_.CustomerName)', '$(Get-Date)')")
}

我的第一个想法是使用:

$sqlValues.Add("('$($_.components | Out-String)'")

那行得通,但该字段是一个看起来像表的字符串。然后我想:

$sqlValues.Add("('$($($_.components | ForEach-Object {$_ -join ','}).TrimEnd())')")

该输出看起来正确,但是当我查询SQL以取回对象时,在“ components”和“ QueryDate”之后还有多余的行。那些从哪里来?或更确切地说,我该如何摆脱它们?

aa                              : True
phone_number                    :
manual_scan_time                : 0
ip_addr                         : 10.50.0.147
last_logon_user_guid            : 286c1410eb470e4ea31dd17e9c1eee31
urlfilter_violated              : 0
last_connect_time               : 1563379779
id                              : 0542c82cada09040be79bfb6e54fd119
spyware_detected                : 0
arch                            : x64
sched_start_time                : 1562922019
virus_detected                  : 0
platform                        : Win Server 2012 R2
version                         : 6.6.2457/14.1.1516
manual_aggressive_complete_time : 0
online                          : True
type                            : 1
status                          : 1
spam_detected                   : 0
sched_complete_time             : 1562926028
sched_scan_time                 : 1559294629
pop3_scan                       : False
manual_aggressive_start_time    : 0
last_logon_user_account         : jdoe
name                            : server01
ss_service                      : True
scan_mode                       : 0
manual_complete_time            : 0
mac_addr                        : 2B:41:38:B4:34:46
components                      : @{version=0; type=1} @{version=11.000.1006; type=3} @{version=15.241.00; type=14}





manual_start_time               : 0
last_logon_user_domain          : acme
created_at                      : 1510519578
last_connect_time_human         : 7/17/2019 4:09:39 PM
CustomerName                    : Acme Corp

QueryDate                       : 7/18/2019 12:00:00 AM

0 个答案:

没有答案