Home > Blockchain >  Add column with values to object
Add column with values to object

Time:11-18

I need to compare variables for my apps and add a column called "Match" with a value Pass or Fail. I already got the part of matching the values and print Pass/Fail in the screen, now i just need to add the values Pass or Fail to each of my variables to a new column in the object... this is where i don't know how to do.

This is where i have my current environment variables. Run $hash["deployment1"].spec.template.spec.containers.env And it lists:

name                                            value
----                                            -----
Build                                           001
Environment                                     DEV
OS                                              Windows
Log_Level                                       Information 

This is where i have the expected values of variables. Run $variables.deployment1.image.env and it lists:

Name                           Value
----                           -----
Build                          001
Environment                    DEV
OS                             Windows
Log_Level                      Error 

This is how i'm comparing the values

foreach($v in $hash["deployment1"].spec.template.spec.containers.env.name){
        Write-Host $v
        if ($hash["deployment1"].spec.template.spec.containers.env.where{$_.name -eq $v}.value -eq $variables.deployment1.image.env[$v]) {
            Write-Host "pass"
        }else{
            Write-Host "fail"
        }
    }

But instead of Write-Host pass/fail i'd like to add a column to $hash["deployment1"].spec.template.spec.containers.env with the expected value, like:

Name                           Value                           Match    
----                           -----                           ----
Build                          001                             Pass
Environment                    DEV                             Pass
OS                             Windows                         Pass
Log_Level                      Error                           Fail

CodePudding user response:

This can be done only through PSobject and not by Hashtable. Hashtable is only meant for key value pairs. Here is a sample for you. This can also be exported as a CSV.

$result = foreach($v in $hash["deployment1"].spec.template.spec.containers.env.name){
        Write-Host $v
        if ($hash["deployment1"].spec.template.spec.containers.env.where{$_.name -eq $v}.value -eq $variables.deployment1.image.env[$v]) {
            $output = "pass"
        }else{
            $output = "fail"
        }
        [pscustomobject]@{
        Name  = "Your Name value"
        Value = "Equivalent value"
        Match  = $output
    }
}
Write-Host $result
$result | Export-csv C:\temp\testresult.csv -NoTypeInformation
$result | ConvertTo-Html -As Table -Property name,value,match -Fragment -PreContent "<h3>Environment Variables</h3>"
  • Related