Here are the important parts of the script, the commented lines work but the uncommented cmdlets next to them do not work and am trying to get them working.
#Converts json file to hashtable variable
function ConvertTo-Hashtable {
[CmdletBinding()]
[OutputType('hashtable')]
param (
[Parameter(ValueFromPipeline)]
$InputObject
)
process {
if ($null -eq $InputObject) {
return $null
}
if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) {
$collection = @(
foreach ($object in $InputObject) {
ConvertTo-Hashtable -InputObject $object
}
)
Write-Output -NoEnumerate $collection
} elseif ($InputObject -is [psobject]) {
$hash = @{}
foreach ($property in $InputObject.PSObject.Properties) {
$hash[$property.Name] = ConvertTo-Hashtable -InputObject $property.Value
}
$hash
} else {
$InputObject
}
}
}
$json = Get-Content -Raw -Path $jsonFile | ConvertFrom-Json | ConvertTo-Hashtable
$mGroup,$mName,$mRollup = $json.metricName.Split('.')
$si = Get-View ServiceInstance
$perfMgr = Get-View -Id $si.Content.PerfManager
$metric = $perfMgr.PerfCounter |
where{$_.GroupInfo.Key -eq $mGroup -and
$_.NameInfo.Key -eq $mName -and
$_.RollupType -eq $mRollup}
$metricParam = Get-Metric -MetricGroup $mGroup -Name $mName | where{$_.Key -eq $metric.Key}
#$trigger = New-AlarmTrigger -metric $metricParam -MetricAlarmOperator $json.sAlarmTrigger.MetricAlarmOperator -Red $json.sAlarmTrigger.Red -RedIntervalSeconds $json.sAlarmTrigger.RedIntervalSeconds -EntityType $json.sAlarmTrigger.Entitytype
$trigger = New-AlarmTrigger $json.sAlarmTrigger
#$actionTrigger = New-AlarmActionTrigger -StartStatus $json.sActionTrigger.StartStatus -EndStatus $json.sActionTrigger.EndStatus -Repeat:$json.sActionTrigger.Repeat
$actionTrigger = New-AlarmActionTrigger $json.sActionTrigger
#$action = New-AlarmAction -Email:$json.sAlarmAction.Email -Subject $json.sAlarmAction.Subject -To $json.sAlarmAction.To -AlarmActionTrigger $actionTrigger
$action = New-AlarmAction $json.sAlarmAction
New-AlarmDefinition -Name $json.Name -Description $json.Description -Entity $object -AlarmTrigger $trigger -AlarmAction $action -ActionRepeatMinutes $json.ActionRepeatMinutes
_____________________________-
Here is the JSON
{
"Name": "JSON TEST ALARM",
"Description": "CPU alarm created from json file",
"ActionRepeatMinutes": 5,
"metricName": "CPU.usage.average",
"sAlarmTrigger": [{
"MetricAlarmOperator": "Above",
"Red": 7500,
"RedIntervalSeconds": 300,
"EntityType": "VirtualMachine"
}],
"sActionTrigger": [{
"StartStatus": "yellow",
"EndStatus": "red",
"Repeat": true
}],
"sAlarmAction": [{
"Email": true,
"Subject": "CPU Usage high",
"To": "x@gmail.com"
}]
}