简体   繁体   English

在此Powershell脚本中进行过滤时,如何用export-csv替换write-host

[英]How to replace write-host with export-csv while filtering in this powershell script

I can't find the syntax to replace Write-Host with Export-Csv while filtering with Where-Object (a bit like Import-Csv in this example How to export to "non-standard" CSV with Powershell ) in this script : 我找不到在此脚本中用Where-Object过滤时用Export-Csv替换Write-Host的语法(在此示例中有点像Import-Csv, 如何使用Powershell导出到“非标准” CSV ):

$xml=[xml]@'
<?xml version="1.0" encoding="iso-8859-1"?>
<catalogue>
  <produits>
    <produit id="pdt1" libelle="produit 1" cat="PDT">
      <metas date="2015.07.24">
        <meta code="123456" value="123456"></meta>
        <meta code="789012" value="ghijkl"></meta>
        <meta code="345678" value="mnopqr"></meta>
      </metas>
    </produit>
    <produit id="pdt2" libelle="produit 2" cat="PDT">
      <metas date="2015.07.24">
        <meta code="111111" value="abcdef"></meta>
        <meta code="789012" value="ghijkl"></meta>
        <meta code="345678" value="mnopqr"></meta>
      </metas>
    </produit>
    <produit id="pdt3" libelle="produit 3" cat="PDT">
      <metas date="2015.07.24">
        <meta code="123456" value="123456"></meta>
        <meta code="789012" value="ghijkl"></meta>
        <meta code="345678" value="mnopqr"></meta>
      </metas>
    </produit>
  </produits>
</catalogue>
'@

$out_csv = "C:\out.csv"

$meta_code = "123456"

$produit_code = "pdt1"
$produit=$xml.catalogue.produits.produit | where {$_.id –like "$produit_code"}

$xml.catalogue.produits.produit | where {$_.cat.startsWith("PDT")} | ForEach-Object {
$meta = $xml.SelectSingleNode("//catalogue/produits/produit[@id='$($_.id)']/metas/meta[@code='$meta_code']")
if($meta) {Write-Host "$($_.id),$($_.libelle)"}
}

Update : I really want same output as with Write-Host ie without header and without quotes like this : 更新:我真的想要与Write-Host相同的输出,即没有标题并且没有这样的引号:

pdt1,produit 1 pdt1,产品1
pdt3,produit 3 pdt3,产品3

and not like this 而不是这样

"id","libelle" “ID”, “libelle”
"pdt1","produit 1" “ pdt1”,“产品1”
"pdt3","produit 3" “ pdt3”,“产品3”

The question is a bit unclear as written, but I'm going to summarize my interpretation of it: 这个问题从书面上来说还不清楚,但是我将总结一下我的解释:

  • You are iterating through XML looking for specific fields. 您正在遍历XML寻找特定字段。
  • You have a loop wherein you test for conditions to be met, and if so you want to take 2 specific fields and write them out as a row into a CSV. 您有一个循环,其中您测试要满足的条件,如果是,则要采用2个特定字段并将它们作为一行写到CSV中。

Export-Csv takes an object or collection of objects and uses the properties in them to create a CSV. Export-Csv接受一个对象或对象集合,并使用其中的属性创建CSV。

In my opinion, the best way to go about this is to replace Write-Host with the creation of a [PSObject] ; 在我看来,最好的方法是用[PSObject]的创建代替Write-Host that will get returned in each iteration of your ForEach-Object loop, and then the entire collection that's returned from that can be piped directly into Export-CSV : 它将在ForEach-Object循环的每次迭代中返回,然后可以将返回的整个集合直接通过管道传递到Export-CSV

$xml.catalogue.produits.produit | where {$_.cat.startsWith("PDT")} | ForEach-Object {
    $meta = $xml.SelectSingleNode("//catalogue/produits/produit[@id='$($_.id)']/metas/meta[@code='$meta_code']")
    if($meta) {
        New-Object PSObject -Property @{ id = $_.id ; libelle = $_.libelle }
    }
} | Export-Csv -Path C:\whatever\my.csv -NoTypeInformation

Based on your comment about needing no headers and no quotes, I would do this this way: 根据您对不需要标题和引号的评论,我可以这样做:

$csv = $xml.catalogue.produits.produit | where {$_.cat.startsWith("PDT")} | ForEach-Object {
    $meta = $xml.SelectSingleNode("//catalogue/produits/produit[@id='$($_.id)']/metas/meta[@code='$meta_code']")
    if($meta) {
        New-Object PSObject -Property @{ id = $_.id ; libelle = $_.libelle }
    }
} | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1
$csv = $csv -replace '"',''
$csv | Out-File -FilePath C:\whatever\my.csv

Out-File defaults to Unicode. Out-File默认为Unicode。 If you need ASCII add -Encoding Ascii . 如果需要ASCII,请添加-Encoding Ascii

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

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