I am looking to build a basic config file using JSON. To begin with, I query a SQL database to get 3 values. These 3 values are stored in CSV, I would like to iterate through my JSON config file looking for a match. Should there be a match, I would like to pass the header and all the key pairs to a Powershell variable.
I have managed to get this working using a CSV as the config file but it seems JSON doesn't support the ".where" and I am completely stuck.
This is my JSON File. The 3 values I am comparing my SQL file are DFS, Message queue, errors. Should they match, I would like to eventually email the team assigned the 3 values.
{
"Messagequeue1": [
{
"dfs": "OAKHILL",
"Messagequeue": "IMPORTSEQ",
"errormsg": "Input in non-input field",
"Ignore" : "false",
"team": "[email protected]",
"teamcc": "[email protected]",
"escalate": "60"
}
]
,"Messagequeue2": [
{
"dfs": "MQ",
"Messagequeue": "MX",
"errormsg": "ERRORMESS",
"Ignore" : "false",
"team": "[email protected]",
"teamcc": "",
"escalate": "60"
}
]
}
Powershell Below.
$json = "C:\Temp\Example.json"
$MessageQueue = Get-Content -Path $json | ConvertFrom-Json
$Param = @{
Header = 'DFS','MessageQueue','ErrorMessage','CreatedDate','StartTime'
Path = 'C:\temp\file.csv'
}
$SqlFile = Import-Csv @Param | Select-Object -Skip 2 | Select-Object -SkipLast 2
$teamemails = @{}
$itsemails = @()
$MessageQueue | gm -MemberType Methods
ForEach ($sqlerror In $SqlFile){
if ($Config = $MessageQueue.where({ $_.DFS -Match $sqlerror.DFS -And $_.ErrorMsg -Match $sqlerror.ErrorMessage -And $_.MessageQueue -Match $sqlerror.MessageQueue}))
{
Write-Host "Match sending to " -NoNewline
Write-Output -InputObject $Config.Team
foreach ($c in $config)
{
$c.Resend
if ($teamemails[$c.Team] -eq $null) {
$teamemails[$c.Team] = @()
}
$teamemails[$c.Team] += $sqlerror
}
}
else {
Write-host 'No Match sending to default'
$itsemails += $sqlerror
}
}
$MessageQueue.where({...})statement is incorrect, you're trying to refer to nested properties ofMessagequeue1andMessagequeue2. Try this for example to see the difference:$MessageQueue = Get-Content -Path $json | ConvertFrom-Json=>$MessageQueue.PSObject.Properties.ForEach({$_.where({$_.Value.DFS -eq 'OAKHILL'})}).Value