【问题标题】:Comparing two CSV files and create a new file with the unmatched values比较两个 CSV 文件并使用不匹配的值创建一个新文件
【发布时间】:2021-08-07 09:05:00
【问题描述】:

我在 CSV 中有两个文件,ServiceAccount 文件,ServiceAccount - 排除

ServiceAccount File Details

FirstName Surname   LoginName
Andrew    Jones     AJones
Ajay      Singh     ASingh
Bobby     Deol      BDeol
Rony      Choudhury RChoudhury
Tim       Perera    TPerera

ServiceAccount - 排除

FirstName Surname   LoginName
Andrew    Jones     AJones
Bobby     Deol      BDeol

新的 CSV 文件应如下所示

FirstName Surname   LoginName
Ajay      Singh     ASingh
Rony      Choudhury RChoudhury
Tim       Perera    TPerera

我已经写了这段代码,但是我看不到上面的结果,谁能帮忙,下面是我的代码。

$Path = "C:\PowerShell"


$ServiceAccountExclusion = "C:\temp\Test\ServiceAccount - Exclusion.csv"
$ServcieAccount = "C:\temp\Test\ServiceAccount.csv"
$UserList = Import-Csv $ServiceAccountExclusion -Delimiter ","
$UserData = Import-Csv $ServcieAccount -Delimiter ","

$UserOutput = @()

    ForEach ($name in $UserData)
    {

        $userMatch = $UserList | where {$_.FirstName -ne $name.FirstName}
        If($userMatch)
        {
            # Process the data

            $UserOutput += New-Object PsObject -Property @{UserName =$name.FirstName}

        }
        
        <# {
            $UserOutput += New-Object PsObject -Property @{UserName =$name.FirstName}
        }  #>
    }
$UserOutput | ft

【问题讨论】:

  • $Exclude = (import-csv .\exclusion.csv).FirstName; import-csv .\ServiceAccount |Where-Object FirstName -NotIn $Exclude

标签: powershell powershell-4.0


【解决方案1】:

这完全取决于您是否正在寻找解决问题的结果,或者您是否正在寻找在 PowerShell 中解决此问题的绝对最佳答案。

就我而言,我可以为您提供一个快速而肮脏的解决方案,从对象到字符串,然后在需要时再返回

(Get-Content 'C:\PS\ServiceAccounts.csv') + (Get-Content 'C:\PS\ServiceAccountsExcluded.csv') | select -Unique | Out-File -FilePath "C:\PS\ServiceAccountsCombined.csv"

$ServiceAccountsCombined = Import-CSV -Path "C:\PS\ServiceAccountsCombined.csv"

$ServiceAccountsCombined

【讨论】:

    猜你喜欢
    • 2014-02-21
    • 1970-01-01
    • 2023-03-15
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多