简体   繁体   English

将输出存储在CSV文件中

[英]Storing output in CSV file

I have wriiten the following SQL script to execute using PowerShell. 我编写了以下SQL脚本以使用PowerShell执行。

cls
foreach ($svr in get-content "demo.txt")
{
  $con = "server=MC1-PQ10X.RF.LILLY.COM\SQL01;database=mylilly_WSS_Content_INCTSK0014840;Integrated Security=sspi" 

  $cmd = "SELECT
Docs.DirName + '/' + Docs.LeafName AS 'Item Name',
DocVersions.UIVersion, 
(DocVersions.UIVersion/512) as Version_Label, DocVersions.Level, DocVersions.TimeCreated
FROM DocVersions FULL OUTER JOIN Docs ON Docs.Id = DocVersions.Id
--   INNER JOIN Webs On Docs.WebId = Webs.Id
--INNER JOIN Sites ON Webs.SiteId = SItes.Id
WHERE (DirName LIKE '%globalcontentrepository%')
AND (IsCurrentVersion = '0')
AND (DocVersions.Id IN ('$svr'))"

  $da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)

  $dt = new-object System.Data.DataTable

  $da.fill($dt) |out-null

  $dt | Export-Csv music.csv -Encoding ascii -NoTypeInformation
}

The problem I'm facing with the above code is regarding the output. 我上面的代码面临的问题与输出有关。 For every $svr this code is creating a new CSV file. 对于每个$svr此代码将创建一个新的CSV文件。 The input file is containing around 1000 inputs. 输入文件包含大约1000个输入。 My requirement is that all the output should get stored in the same file rather than creating new file. 我的要求是所有输出都应该存储在同一文件中,而不是创建新文件。

There are several ways to achieve your goal. 有几种方法可以实现您的目标。 One is to append to the output file as @PeterMmm suggested in the comments to your question: 一种是按照问题的注释中的@PeterMmm的建议将其追加到输出文件中:

foreach ($svr in Get-Content "demo.txt") {
  ...
  $dt | Export-Csv music.csv -Encoding ascii -NoTypeInformation -Append
}

Note, however, that Export-Csv doesn't support appending prior to PowerShell v3. 但是请注意, Export-Csv不支持在PowerShell v3之前附加。 Also, appending usually has a negative impact on performance, because you have to repeatedly open the output file. 此外,附加通常会对性能产生负面影响,因为您必须反复打开输出文件。 It's better to put the export cmdlet outside the loop and write the output file in one go. 最好将export cmdlet放在循环之外,并一次性写入输出文件。 foreach loops don't play nicely with pipelines, though, so you'd have to assign the loop output to a variable first: foreach循环不能很好地与管道配合使用,因此您必须首先将循环输出分配给变量:

$music = foreach ($svr in Get-Content "demo.txt") {
  ...
  $dt
}
$music | Export-Csv music.csv -Encoding ascii -NoTypeInformation

or run it in a subexpression: 或在子表达式中运行它:

(foreach ($svr in Get-Content "demo.txt") {
  ...
  $dt
}) | Export-Csv music.csv -Encoding ascii -NoTypeInformation

Personally I'd prefer a ForEach-Object loop over a foreach loop, because the former does work well with pipelines: 就个人而言,与foreach循环相比,我更喜欢ForEach-Object循环,因为前者在管道中运行良好:

Get-Content "demo.txt" | ForEach-Object {
  ...
  $dt
} | Export-Csv music.csv -Encoding ascii -NoTypeInformation

Note that you must replace every occurrence of your loop variable $svr with the "current object" variable $_ for this to work, though. 请注意,尽管如此,必须将循环变量$svr每次出现都替换为“当前对象”变量$_

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

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