简体   繁体   English

将差异导出到CSV并与来自单独CSV PowerShell的列合并

[英]Export Differences to CSV and Combine with Columns from a Separate CSV PowerShell

Basically, I have a CSV file that lists the content of a directory to analyze for any missing files. 基本上,我有一个CSV文件,该文件列出了目录的内容以分析是否缺少任何文件。

CSV文件

目录内容

My script does this, however I am trying to export the results of the missing files to a new CSV file. 我的脚本可以执行此操作,但是我正在尝试将丢失文件的结果导出到新的CSV文件中。

Current script: 当前脚本:

#get working directory
$documents = 'C:\Users\Me\Documents\ScriptTest'
#path to files located in the directory
$myFolder = Get-ChildItem 'C:\Users\Me\Documents\Test\1' -Recurse -Include *.txt | 
    Select-Object -ExpandProperty BaseName | 
        Where-Object{$_ -notmatch "\\FolderName\\"}
#get items located in CSV file list column
$myCSV = Import-CSV -Path 'C:\Users\Me\Documents\Test.csv' | 
    Select-Object -ExpandProperty 'File Path'

$CSVexport = '\Users\Me\Documents\Test.csv'

$results=@()
$desiredColumns = @{
        ID = $CSVexport | Select-Object ID 
        'File Path' = $CSVexport | Select-Object 'File Path'
        }

#split the CSV item names
$splits=@()
foreach($file in $myCSV){
  $split = $file.split("\\")[2]
  $split2 = $split.split(".")[0]
  $splits+=$split2
}

#compare the CSV items to the folder items
$compare = Compare-Object -ReferenceObject $splits -DifferenceObject $myFolder -includeequal

Write-Output "`n_____Results____`n"
#Write the results and export to a CSV file
$compare | ForEach-Object {
    if($_.SideIndicator -eq "<="){
        write-host "`n$($_.InputObject) is missing from the Test folder.`n"
        $results = New-Object PSObject -Property $desiredColumns
        $results.'File Path' = $_.InputObject
        $results | Export-Csv -Path C:\Users\Me\Documents\Results.csv –NoTypeInformation
    }
}

All I get back is: 我得到的是:

结果

It only gets one of the missing files and nothing for the "ID" column. 它只会得到丢失的文件之一,而对于“ ID”列则什么也没有。 I want to be able to get the result of the comparison and export it to a new CSV with the related columns from the first CSV file. 我希望能够获得比较结果并将其导出到具有第一个CSV文件中相关列的新CSV文件中。 For example: 例如:

我在寻找什么

Can I get some help as to what I might be doing wrong? 关于我可能做错的事情,我可以得到一些帮助吗?


Edit (In response to LotPings answer) 编辑(针对LotPings的回答)

New Code: 新代码:

$documents = 'C:\Users\Me\Documents'

$NewCsv = Import-CSV -Path 'C:\Users\Me\Documents\Test.csv' |
    Select-Object ID,'File Path' |  
      ForEach-Object {
        if (!(Test-Path (Join-Path $documents $_.'File Path'))){
          [PSCustomObject]@{
             ID = $_.ID
             Missing = $_.'File Path'
          }
       }
    }
    $NewCsv

Output: 输出:

Name                           Value                                                                           
----                           -----                                                                           
ID                             4                                                                               
Missing                        Test\1\File4.txt                                                                
ID                             6                                                                               
Missing                        Test\1\File6.txt   

Extremely over complicated. 极其复杂。

If you iterate your csv, check the existence of every file and generate a new csv on the fly, it can be as simple as: 如果您要迭代csv,请检查每个文件的存在并即时生成一个新的csv,它可以很简单:

## Q:\Test\2018\05\24\SO_50517360.ps1
$BasePath = "C:\Users\Me\Documents"

$NewCsv = Import-CSV -Path '.\Test.csv'|Select-Object ID,'File Path' |  
  ForEach-Object {
    if (!(Test-Path (Join-Path $BasePath $_.'File Path'))){
      [PSCustomObject]@{
         ID = $_.ID
         Missing = $_.'File Path'
      }
   }
}
$NewCsv

Sample output (which can be put in a new file by 样本输出(可以通过以下方式将其放入新文件中
$NewCsv|Export-Csv .\\Missing.csv -NoTypeInformation

> .\SO_50517360.ps1

ID Missing
-- -------
4  Test\1\File4.txt
6  Test\1\File6.txt

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

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