简体   繁体   English

我可以在 PowerShell 中的对象上使用 SQL 命令(例如 join),而不涉及任何 SQL 服务器/数据库吗?

[英]Can I use SQL commands (such as join) on objects in powershell, without any SQL server/database involved?

As a disclaimer, I'm no professional powershell scripter, but more of a 'script-kiddie' level scripter who was assigned something far outside the scope of his job, tech support.作为免责声明,我不是专业的 powershell 脚本编写者,而更像是一个“脚本小子”级别的脚本编写者,他被分配的工作远远超出了他的工作范围,即技术支持。 :P :P

I've been working on a migration script to bring some really old data into a new software app.我一直在研究迁移脚本,以将一些非常旧的数据带入新的软件应用程序中。

As a part of this, I'm using the following code to 'Join' two objects (which were imported from csv files).作为其中的一部分,我使用以下代码“连接”两个对象(从 csv 文件导入)。 Because of the way the data is presented to me, I'm having to run this join script several times, against rather large data sets, and it's taking a very long time to complete.由于数据呈现给我的方式,我不得不针对相当大的数据集多次运行此连接脚本,并且需要很长时间才能完成。

The code I'm using is:我正在使用的代码是:

Import-Csv extract-accounts.csv | ForEach-Object -Begin {
    $Jobs = @{}
} -Process {
    $Jobs.Add($_.NAME,$_.ID)
}

Import-Csv Job.csv | ForEach-Object {
    $_ | Add-Member -MemberType NoteProperty -Name ContactID -Value $Jobs."$($_.DisplayName)" -PassThru
} | Export-Csv -NoTypeInformation Joined-AccountJob.csv

What I'm wondering is, can I use SQL-like commands on objects in powershell to simplify and speed-up this process?我想知道的是,我可以在 powershell 中的对象上使用类似 SQL 的命令来简化和加速这个过程吗? Or, would I be better to take the CSV files, push them into an SQL database (I have no knowledge of this process), perform my joins, then export them back out to csv files?或者,我是否会更好地获取 CSV 文件,将它们推送到 SQL 数据库(我不知道这个过程),执行我的连接,然后将它们导出回 csv 文件?

Thanks!谢谢!

SQL Commands, no not exactly. SQL 命令,不完全是。 Although I did find a site that described a way to do it here.虽然我确实找到了一个网站,描述了一种方法。

http://social.technet.microsoft.com/Forums/windowsserver/en-US/e242e1e9-6a8f-4a46-a884-8bd6811b5e35/combining-multiple-csv-files-with-powershell http://social.technet.microsoft.com/Forums/windowsserver/en-US/e242e1e9-6a8f-4a46-a884-8bd6811b5e35/combining-multiple-csv-files-with-powershell

Basically what they describe, and what sounds like the right way to do it to me, is to grab each column out of the CSVs you want into a variable then dump them into a text file with each variable separated by a comma.基本上他们所描述的,以及听起来对我来说正确的方法是将每一列从你想要的 CSV 中提取到一个变量中,然后将它们转储到一个文本文件中,每个变量用逗号分隔。

If that doesn't work for you and you want to get them into SQL anyway this article describes the process of getting CSVs into SQL through several methods.如果这对您不起作用并且您无论如何都想将它们导入 SQL,本文将介绍通过多种方法将 CSV 导入 SQL 的过程。

http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/28/four-easy-ways-to-import-csv-files-to-sql-server-with-powershell.aspx http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/28/four-easy-ways-to-import-csv-files-to-sql-server-with-powershell.aspx

After looking at the options, I didn't see a way to simplify exactly what I was doing, however I did discover where-object, which helped in other areas of my project.在查看了这些选项后,我没有看到可以完全简化我正在做的事情的方法,但是我确实发现了 where-object,这对我项目的其他领域有所帮助。 Here's an example:下面是一个例子:

Import-Csv JobContact.csv |
Select-Object JobContactRole,JobExternalReference,ContactExternalReference |
Where-Object {$_.JobContactRole -ne "Policyholder" -and $_.JobContactrole -ne "Broker"} |
Export-Csv Cleaned-JobContact.csv -NoTypeInformation

I hope this helps someone!我希望这可以帮助别人!

Import-Csv extract-accounts.csv | Join (Import-Csv Job.csv) -On ContactId Join (Import-Csv Job.csv) -On ContactId

For details see: In Powershell, what's the best way to join two tables into one?有关详细信息,请参阅:在 Powershell 中,将两个表合并为一个的最佳方法是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM