【问题标题】:Powershell - Compare two csv rows and write the result in the thirdPowershell - 比较两个 csv 行并将结果写入第三行
【发布时间】:2014-12-10 16:18:41
【问题描述】:

我有两个 csv 列,我需要比较这两列并将结果写入第三列(Result)。

"行","路径","结果"

"RETENTION_LEVEL 3 1 1 1 1 1 1 1 1 1","C:\Veritas\NetBackup\db\class\policy-v-\schedule\cum\info",

如何使第三列的变量可设置?

#Write retention and policy in a csv file. 
$file="C:\Users\toto\schedules.csv"
#Write analysis results in a final csv.
$finalFile="C:\Users\toto\bad_retention.csv"
#query to find information based on the retention level and policy + export in the csv
$find=Get-ChildItem "C:\Veritas\NetBackup\db\class\" -recurse | Select-String -pattern "RETENTION_LEVEL" | Select line,path,@{Expression={$_.result};Label="Result";} | Export-CSV $file –NoTypeInformation

        (Import-Csv $file -Delimiter ',')

        ForEach-Object{
            if(($_.line -match "RETENTION_LEVEL 1 1 1 1 1 1 1 1 1 1") -and ($_.path -match "-v-"))
            {
             $_.result="BAD RETENTION"
             echo $_result;
            }
            else
            {
             $_.result="OK";
             echo $_result;
            }
        } | Export-CSV $finalFile –NoTypeInformation
Property 'result' cannot be found on this object; make sure it exists and is settable.
At C:\Users\toto\Documents\bad_retention.ps1:28 char:17
+              $_. <<<< result="OK";
    + CategoryInfo          : InvalidOperation: (result:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

【问题讨论】:

    标签: powershell csv compare


    【解决方案1】:

    未测试:

    Get-ChildItem "C:\Veritas\NetBackup\db\class\" -recurse |
     Select-String -pattern "RETENTION_LEVEL" |
     Select line,path,@{
      Label = 'result'
      Expression = {('OK','BAD RETENTION')[($_.line -match "RETENTION_LEVEL 1 1 1 1 1 1 1 1 1 1") -and ($_.path -match "-v-")]}
      } |
      Export-CSV $file –NoTypeInformation
    

    【讨论】:

      【解决方案2】:

      使用下面的语法它可以工作“:

      Expression = {('OK','BAD RETENTION')[($_.line -eq "RETENTION_LEVEL 1 1 1 1 1 1 1 1 1 1")]}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多