导入 csv 文件时找不到接受参数的位置参数

A positional parameter cannot be found that accepts argument while importing csv file

有两个脚本。脚本 1 和 2.

对于脚本 1

我在尝试将数据从 csv 导入 SQL table 时出现以下错误,同时我在 PowerShell 命令提示符下执行以及 SQL 作业计划。

Executed as user: NT Service\SQLSERVERAGENT. ...e corresponding line is 
'Import-CSV "E:\dump\moduleabc\*.csv" | ForEach-Object {Invoke-Sqlcmd `  '.
 Correct the script and reschedule the job. 
 The error information returned by PowerShell is: 
 'A positional parameter cannot be found that accepts argument 
 '+@{User=aser7@company.com; Device type=IOS; Device model=Apple iPhone11,2; UDID=WWWWWW-2B6B-4B69-8E21-795F454123EA; Mac Address=00:00:00:00:00:00; Company Name=company; OS Version=Version 14.4.2 (Build 18D70); App Client Connector Version=1.8.3 (3) ; Latest Config=Yes; Policy Name=Default; Last Seen=2021-05-08 03:48:29 GMT; VPN State=On; Registration State=Updated; Owner=; Hostname=iPhone; Manufacturer=Apple; Config Download Count=2; Registration TimeStamp=2021-05-08 03:33:50 GMT; Config Download TimeStamp=2021-05-08 03:48:29 GMT; Keep Alive Timestamp=2021-05-08 08:46:10 GMT; Device Hardware Fingerprint=WWWWWW-2B6B-4B69-8E21-795F454123EA; Tunnel Version=; Log TS=; Log Ack TS=; Log Url=}
 .Hostname+'  ,'+@{User=aser7@company.com; Device type=IOS; Device model=Apple iPhone11,2; UDID=WWWWWW-2B6B-4B69-8E21-795F454123EA; Mac Address=00:00:00:00:00:00; Company Name=company; OS Version=Version 14.4.2 (Build 18D70); App Client Connector Version=1.8.3 (3) ; Latest Config=Yes; Policy Name=Default; Last Seen=2021-05-08 03:48:29 GMT; VPN State=On; Registration State=Updated; Owner=; Hostname=iPhone; Manufacturer=Apple; Config Download Count=2; Registration TimeStamp=2021-05-08 03:33:50 GMT; Config Download TimeStamp=2021-05-08 03:48:29 GMT; Keep Alive Timestamp=2021-05-08 08:46:10 GMT; Device Hardware Fingerprint=WWWWWW-2B6B-4B69-8E21-795F454123EA; Tunnel Version=; Log TS=; Log Ack TS=; Log Url=}.App Client Connector Version+'  
 ,'+@{User=aser@company.com; Device type=IOS; Device model=Apple iPhone11,2; UDID=WWWWWW-2B6B-4B69-8E21-795F454123EA; Mac Address=00:00:00:00:00:00; Company Name=company; OS Version=Version 14.4.2 (Build 18D70); App Client Connector Version=1.8.3 (3) ; Latest Config=Yes; Policy Name=Default; Last Seen=2021-05-08 03:48:29 GMT; VPN State=On; Registration State=Updated; Owner=; Hostname=iPhone; Manufacturer=Apple; Config Download Count=2; Registration TimeStamp=2021-05-08 03:33:50 GMT; Config Download TimeStamp=2021-05-08 03:48:29 GMT; Keep Alive Timestamp=2021-05-08 08:46:10 GMT; Device Hardware Fingerprint=WWWWWW-2B6B-4B69-8E21-795F454123EA; Tunnel Version=; Log TS=; Log Ack TS=; Log Url=}.OS Version+' 
 ,'+@{User=asrerer@company.com; Device type=IOS; Device model=Apple iPhone11,2; UDID=WWWWWW-2B6B-4B69-8E21-795F454123EA; Mac Address=00:00:00:00:00:00; Company Name=company; OS Version=Version 14.4.2 (Build 18D70); App Client Connector Version=1.8.3 (3) ; Latest Config=Yes; Policy Name=Default; Last Seen=2021-05-08 03:48:29 GMT; VPN State=On; Registration State=Updated; Owner=; Hostname=iPhone; Manufacturer=Apple; Config Download Count=2; Registration TimeStamp=2021-05-08 03:33:50 GMT; Config Download TimeStamp=2021-05-08 03:48:29 GMT; Keep Alive Timestamp=2021-05-08 08:46:10 GMT; Device Hardware Fingerprint=WWWWWW-2B6B-4B69-8E21-795F454123EA; Tunnel Version=; Log TS=; Log Ack TS=; Log Url=}.Device type+'  
 ,'+@{User=aser7@company.com; Device type=IOS; Device model=Apple iPhone11,2; UDID=WWWWWW-2B6B-4B69-8E21-795F454123EA; Mac Address=00:00:00:00:00:00; Company Name=company; OS Version=Version 14.4.2 (Build 18D70); App Client Connector Version=1.8.3 (3) ; Latest Config=Yes; Policy Name=Default; Last Seen=2021-05-08 03:48:29 GMT; VPN State=On; Registration State=Updated; Owner=; Hostname=iPhone; Manufacturer=Apple; Config Download Count=2; Registration TimeStamp=2021-05-08 03:33:50 GMT; Config Download TimeStamp=2021-05-08 03:48:29 GMT; Keep Alive Timestamp=2021-05-08 08:46:10 GMT; Device Hardware Fingerprint=WWWWWW-2B6B-4B69-8E21-795F454123EA; Tunnel Version=; Log TS=; Log Ack TS=; Log Url=}.User+'  
 ,CONVERT( datetime, REPLACE(LEFT('+@{User=as3...  Process Exit Code -1.  The step failed.
 

脚本 1

Import-CSV "E:\dump\moduleabc\*.csv" | ForEach-Object {Invoke-Sqlcmd `
-ServerInstance "servername.company.com" -username 'usernamesomething' -password 'somepassword'`
-Query "SET QUOTED_IDENTIFIER ON delete from [DBname].[dbo].[tbl_destinationTable]; INSERT INTO [DBname].[dbo].[tbl_destinationTable]([Hostname]
,[Agent_Version]
,[OS_Version]
,[Device_Type]
,[User]
,[Last_Seen]) VALUES
 ('"+$_.'Hostname'+"'
,'"+$_.'App Client Connector Version'+"'
,'"+$_.'OS Version'+"'
,'"+$_.'Device type'+"'
,'"+$_.User+"'
,CONVERT( datetime, REPLACE(LEFT('"+$_.'Last Seen'+"', 19), '"+ +"', '"+T+"')))"
}

这是我要导入的 CSV 数据

脚本 2

但是当我执行以下查询脚本 2 时,我能够通过 PowerShell 命令提示符执行,但是由于 SQL 作业代理使用 $() 作为保留关键字。它抛出同样的错误。

Import-CSV "E:\dump\moduleabc\*.csv" | ForEach-Object {Invoke-Sqlcmd `
-ServerInstance "servername.company.com" -username 'usernamesomething' -password 'somepassword'`
-Query "SET QUOTED_IDENTIFIER ON delete from [DBname].[dbo].[tbl_destinationTable]; INSERT INTO [DBname].[dbo].[tbl_destinationTable]([Hostname]
,[Agent_Version]
,[OS_Version]
,[Device_Type]
,[User]
,[Last_Seen]) VALUES
 ('$($_.'Hostname')'
,'$($_.'App Client Connector Version')'
,'$($_.'OS Version')'
,'$($_.'Device type')'
,'$($_.User)'
,CONVERT( datetime, REPLACE(LEFT('$($_.'Last Seen')', 19), ' ', 'T')))"
}

您可以使用模板 Here-String 进行查询,并使用 -f Format operator 从 csv 中插入值。
这样你就不需要 $() 子表达式。

$query = @'
SET QUOTED_IDENTIFIER ON

DELETE
FROM [DBname].[dbo].[tbl_destinationTable];

INSERT INTO [DBname].[dbo].[tbl_destinationTable] (
     [Hostname]
    ,[Agent_Version]
    ,[OS_Version]
    ,[Device_Type]
    ,[User]
    ,[Last_Seen]
    )
VALUES (
     '{0}'
    ,'{1}'
    ,'{2}'
    ,'{3}'
    ,'{4}'
    ,CONVERT ( DATETIME, REPLACE(LEFT('{5}'), 19),' ','T' )
    )
'@

Import-CSV "E:\dump\moduleabc\*.csv" | ForEach-Object {
    # use a splatting Hashtable for better readability (no more ugly backticks)
    $sqlParams = @{
        ServerInstance = 'servername.company.com'
        UserName       = 'usernamesomething'
        Password       = 'somepassword'
        Query          = $query -f $_.'Hostname', 
                                   $_.'App Client Connector Version', 
                                   $_.'OS Version', 
                                   $_.'Device type', 
                                   $_.User,
                                   $_.'Last Seen'
    }
    Invoke-Sqlcmd @sqlParams
}