1 2 3 Previous Next 43 Replies Latest reply on Sep 16, 2020 9:18 AM by mk5213 Go to original post
      • 30. Re: Create a custom CPU alarm for VM using PowerCli
        mk5213 Novice

        Hey LucD, I am trying to get the input for alarms working with json files. I have a json file that gets taken in and successfully have everything stored in a hashtable of hashtables, but I cant get the cmdlet to work with it for some reason:

         

        $actionTrigger = New-AlarmActionTrigger $json.sActionTrigger

         

        This is the error message for this line, as well as the contents of $json.sActionTrigger:

        • 31. Re: Create a custom CPU alarm for VM using PowerCli
          LucD Guru
          Community WarriorsvExpertUser Moderators

          Did you already try casting your value from the JSON file to ManagedEntityStatus type

           

          [VMware.Vim.ManagedEntityStatus]$json.sActionTrigger.Value
          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 32. Re: Create a custom CPU alarm for VM using PowerCli
            mk5213 Novice

            Just tried the following:

             

            $actionTrigger = New-AlarmActionTrigger [VMware.Vim.ManagedEntityStatus]$json.sActionTrigger.Value

             

            and got the following output:

             

            • 33. Re: Create a custom CPU alarm for VM using PowerCli
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              What exactly do you have in $json.sActionTrigger.Value?

              Or else share part of the JSON file.

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 34. Re: Create a custom CPU alarm for VM using PowerCli
                mk5213 Novice

                Now that you say that just checked what is stored in $json.sActionTrigger.Value and nothing comes up. But if I do run $json.sActionTrigger it shows the correct name value pairs like in my above picture, I am confused why this is.

                • 35. Re: Create a custom CPU alarm for VM using PowerCli
                  mk5213 Novice

                  The only thing I have stored in the JSON is the hashtable sActionTrigger:

                  • 36. Re: Create a custom CPU alarm for VM using PowerCli
                    LucD Guru
                    Community WarriorsvExpertUser Moderators

                    How did you create that JSON file?
                    That field should contain green,yellow or red

                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                    • 37. Re: Create a custom CPU alarm for VM using PowerCli
                      mk5213 Novice

                       

                      Yeah so I actually got this to work without nested objects in the json file. So before I just had the json only contain the startstatus, endstatus and repeat and it worked perfectly. But I am trying to contain all the hastables in one json file, so i am testing right now with just doing one hastable in the hashtable but I am hoping to add them for all the other hastables in the script. I just don't understand why the cmdlet works with a hastable, but I cant get it to work with a hashtable within a hastable. It may not be possible but I was just trying to see if you would possibly know what is wrong

                      • 38. Re: Create a custom CPU alarm for VM using PowerCli
                        LucD Guru
                        User ModeratorsCommunity WarriorsvExpert

                        I would need to see the relevant part of your code and a sample JSON extract to further analyse.

                        Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                        • 39. Re: Create a custom CPU alarm for VM using PowerCli
                          mk5213 Novice

                          Ok if need be I can do that but I think I have an idea of what the issue is. I basically have a hash table variable I am trying to splat with. I didn't assign it  in the script like you created your splatting variable in your example, I have a function create a hashtable from a json file. I read somewhere that you cannot splat with a created hashtable, do you think this is the issue? It is treating my hashtable as the first parameter instead of using the name-value pairs in the hashtable

                          • 40. Re: Create a custom CPU alarm for VM using PowerCli
                            mk5213 Novice

                            From my understanding this means that it is impossible to use a hashtable object to splat currently. Basically you need a created splatting variable?

                            • 41. Re: Create a custom CPU alarm for VM using PowerCli
                              mk5213 Novice

                              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"

                                  }]

                              }

                              • 42. Re: Create a custom CPU alarm for VM using PowerCli
                                LucD Guru
                                Community WarriorsUser ModeratorsvExpert

                                Your ConvertTo-HashTable function does not seem to be recursive.

                                 

                                Starting from your JSON file, the following does seem to work for me.

                                 

                                $json = Get-Content -Raw -Path $jsonFile | ConvertFrom-Json

                                $sTrigger = $json.sActionTrigger | ConvertTo-Hashtable

                                New-AlarmActionTrigger @sTrigger

                                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                • 43. Re: Create a custom CPU alarm for VM using PowerCli
                                  mk5213 Novice

                                  Thanks LucD! Things seem to be working now. Your help is greatly appreciated

                                  1 2 3 Previous Next