powershell - 标题名称和跳过第一行

标签 powershell

Excel源:具有2列和2行(第一行具有标题)

下面提到的代码导入数据库中的空白行...

但是,如果删除-Skip 1-则同时导入两行(一个具有标题,另一个具有值)

Import-CSV $DestinationFile -Header Col1, Col2 | Select-Object -Skip 1
ForEach-Object {Invoke-Sqlcmd -Database $database -ServerInstance $server -Query "insert into $table VALUES ('$($_.Col1)','$($_.Col2)')"}

有人可以建议上述代码有什么问题吗,我们希望跳过标题并为标题提供自定义名称(因为文件中的标题名称很大)

最佳答案

Import-CSV假定给定的csv文件的第一行是标题。因此,如果使用-header开关并指定标题,则Import-CSV也会将csv文件中的第一行用作数据行。

此外,您忘记了将导入的csv对象通过管道传递到foreach-object循环中。

Import-CSV $DestinationFile | ForEach-Object {Invoke-Sqlcmd -Database $database -ServerInstance $server -Query "insert into $table VALUES ('$($_.Col1)','$($_.Col2)')"}

关于powershell - 标题名称和跳过第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51422708/

相关文章:

powershell - 运行可执行文件的语法?

powershell - 更新 Office 365 用户的某些 "My Contacts"的电子邮件地址

powershell - 如何使用Microsoft帐户添加-AzureRmAccount?

powershell - Select-String 因搜索词包含特殊字符而失败

powershell - 使用部分服务名称(版本)

.net - 在Powershell中,如何使用rencissh dll中的scpclient下载方法?

powershell - 发送-替换HTML电子邮件

powershell - 用不同的名称重命名多个文件

windows - 类似于 Windows 中 linux shell 中的 `...` 的功能

powershell - cd 程序文件错误 : positional parameter cannot be found