Home > database >  IF - Missing '=' operator after key in hash literal
IF - Missing '=' operator after key in hash literal

Time:09-23

Hello I am getting error "Missing '=' operator after key in hash literal." on the IF statement, where am I wrong

$Nests | ForEach-Object {
      $RecordMetrics = [pscustomobject]@{
            key = $_.key
            count = $_.doc_count
            if (key -like 'CBT99*') 
            {
              bo = 'CBT'
              SiteID = 1972
            }
           elseif
           {
            boperator = $map[$_.key.TrimEnd("1","2","3","4","5","6","7","8","9","0","-") ].boperator
            SiteID = $map[$_.key.TrimEnd("1","2","3","4","5","6","7","8","9","0","-") ].SiteID
           }
        }}

CodePudding user response:

You can't put a conditional statement in the middle of a hashtable literal like that.

You'll have to create the hashtable/dictionary first, then populate the relevant keys based on your conditional logic:

$Nests | ForEach-Object {
    # create dictionary with initial properties
    $properties = [ordered]@{
        key = $_.key
        count = $_.doc_count
        Host = ''
        OperatorName = ''
    }

    # conditionally add remaining properties
    if ($_.key -like 'CBT99*') {
        $properties['bo'] = 'CBT'
        $properties['SiteID'] = 1972
    }
    else {
        $properties['boperator'] = $map[$_.key.TrimEnd("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-") ].boperator
        $properties['SiteID'] = $map[$_.key.TrimEnd("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-") ].SiteID
    }

    # convert to custom object
    $RecordMetrics = [pscustomobject]$properties
}
  • Related