Hello!
I'm trying to create a script that will create alarms in our vCenter environment. We are on VC 6.5 and I am using the latest version of powercli. I grabbed a script posted by LucD (thanks for that!), but I am continuing to get an error the error shown below. I'm not too familiar with creating alarms with powercli, but it seems like there is something simple that I'm missing here. The alarm does get created, but I'd like to resolve whatever is triggering the error. Any help would be much appreciated -- thanks!
----------------------------------- Error message that displays ----------------------------------------
Get-View : Cannot validate argument on parameter 'Id'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again.
At line:7 char:24
+ $alarms = Get-View -Id ($alarmMgr.GetAlarm($si.Content.RootFolder))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-View], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------- Script ---------------------------------------------------------
$si = Get-View serviceInstance
$alarmMgr = Get-View -Id $si.Content.AlarmManager
$alarms = Get-View -Id ($alarmMgr.GetAlarm($si.Content.RootFolder))
$ha = $alarms | where{$_.Info.Name -match "Host hardware"}
$spec = New-Object VMware.Vim.AlarmSpec
$spec.Name = 'TestHW2'
$spec.Description = 'Test HW alarm'
$expr1 = New-Object VMware.Vim.EventAlarmExpression
$expr1.EventType = 'EventEx'
$expr1.EventTypeId = 'com.vmware.vc.cim.CIMGroupHealthStateChanged'
$expr1.ObjectType = 'HostSystem'
$expr1.Status = 'red'
$exprComp1 = New-Object VMware.Vim.EventAlarmExpressionComparison
$exprComp1.AttributeName = 'group'
$exprComp1.Operator = 'equals'
$exprComp1.Value = 'SystemBoard'
$exprComp2 = New-Object VMware.Vim.EventAlarmExpressionComparison
$exprComp2.AttributeName = 'newstate'
$exprComp2.Operator = 'equals'
$exprComp2.Value = 'red'
$expr1.Comparisons = $exprComp1,$exprComp2
$spec.Expression = $expr1
$alarmMgr.CreateAlarm($si.Content.RootFolder,$spec)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Could it be that you have no alarms defined on the root folder (folder Datacenters)?
In that case we better replace that by a check if that is $null
if ($alarmsMoRef) {
$alarms = Get-View -Id ($alarmMgr.GetAlarm($si.Content.RootFolder))
$ha = $alarms | where {$_.Info.Name -match "Host hardware"}
}
On the other hand, I do understand what this part of the code is doing.
It checks if the alarm already exists, but the scrip doesn't seem to remove it in that case.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Do you have perhaps multiple vSphere Server connections open?
Check what is in $global:defaultviservers.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
When I was testing I wasn't actually disconnecting from the VC that I was connected to, so the connection did stay open. I since tweaked what I was testing to what's shown below and unfortunately, I'm still seeing the same error. Any other thoughts?
write-host $global:defaultviservers
Function CreateAlarm
{
# Specify which vCenter to connect to
Connect-VIServer "<VC>"
$si = Get-View serviceInstance
$alarmMgr = Get-View -Id $si.Content.AlarmManager
$alarms = Get-View -Id ($alarmMgr.GetAlarm($si.Content.RootFolder))
$ha = $alarms | where{$_.Info.Name -match "Host hardware"}
$spec = New-Object VMware.Vim.AlarmSpec
$spec.Name = 'TestHW2'
$spec.Description = 'Test HW alarm'
$expr1 = New-Object VMware.Vim.EventAlarmExpression
$expr1.EventType = 'EventEx'
$expr1.EventTypeId = 'com.vmware.vc.cim.CIMGroupHealthStateChanged'
$expr1.ObjectType = 'HostSystem'
$expr1.Status = 'red'
$exprComp1 = New-Object VMware.Vim.EventAlarmExpressionComparison
$exprComp1.AttributeName = 'group'
$exprComp1.Operator = 'equals'
$exprComp1.Value = 'SystemBoard'
$exprComp2 = New-Object VMware.Vim.EventAlarmExpressionComparison
$exprComp2.AttributeName = 'newstate'
$exprComp2.Operator = 'equals'
$exprComp2.Value = 'red'
$expr1.Comparisons = $exprComp1,$exprComp2
$spec.Expression = $expr1
$alarmMgr.CreateAlarm($si.Content.RootFolder,$spec)
Disconnect-VIServer "<VC>" -Confirm:$false
}
CreateAlarm
But did you check the content of $global:defaultviservers?
Is there only 1 entry in there?
Did you stop/start your PowerShell/PowerCLI sessions between tests?
Btw, I took your code, and for me it seems to be working ok.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I did check -- there was nothing in there. Additionally, I just did a full reboot of the system I was running the script on and then opened PowerShell ISE and re-ran the script and I'm still seeing the Get-View error below
Get-View : Cannot validate argument on parameter 'Id'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again.
At line:13 char:24
+ $alarms = Get-View -Id ($alarmMgr.GetAlarm($si.Content.RootFolder))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-View], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
I just tried doing this block of code in ISE
Connect-VIServer "<VC>"
#Disconnect-VIServer "<VC>" -Confirm:$false
write-host $global:defaultviservers
When I do a connect, I see 2 entries. When I comment out the Connect and uncomment the Disconnect there are 0 entries shown
This seemed to work. I commented out the $alarms and $ha part as I'm not entirely sure how those are used in the script as they don't get called out anywhere else in it. Additionally for the specific alert I've been playing around with, we don't have any additional conditions on the trigger, so for a simple alert, we wouldn't need that piece. We do have a few alarms that we want additional conditions on that I'll use that code on.
Function CreateAlarm
{
# Specify which vCenter to connect to
Connect-VIServer "<VC>"
$si = Get-View serviceInstance
$alarmMgr = Get-View -Id $si.Content.AlarmManager
#$alarms = Get-View -Id ($alarmMgr.GetAlarm($si.Content.RootFolder))
#$ha = $alarms | where{$_.Info.Name -match "Host hardware"}
$spec = New-Object VMware.Vim.AlarmSpec
$spec.Name = 'cannot connect host'
$spec.Description = 'cannot connect host'
$spec.Enabled = $TRUE
$expr1 = New-Object VMware.Vim.EventAlarmExpression
$expr1.EventType = 'EventEx'
$expr1.EventTypeId = 'cannot connect host'
$expr1.ObjectType = 'HostSystem'
$expr1.Status = 'red'
#$exprComp1 = New-Object VMware.Vim.EventAlarmExpressionComparison
#$exprComp1.AttributeName = 'group'
#$exprComp1.Operator = 'equals'
#$exprComp1.Value = 'SystemBoard'
#$exprComp2 = New-Object VMware.Vim.EventAlarmExpressionComparison
#$exprComp2.AttributeName = 'newstate'
#$exprComp2.Operator = 'equals'
#$exprComp2.Value = 'red'
#$expr1.Comparisons = $exprComp1,$exprComp2
$spec.Expression = $expr1
$alarmMgr.CreateAlarm($si.Content.RootFolder,$spec)
# Configure email alert for newly created alarm
Get-AlarmDefinition -Name "Cannot connect host" | New-AlarmAction -Email -To ("<EMAIL ADDRESS>")
Disconnect-VIServer "<VC>" -Confirm:$false
}
CreateAlarm
Could it be that you have no alarms defined on the root folder (folder Datacenters)?
In that case we better replace that by a check if that is $null
if ($alarmsMoRef) {
$alarms = Get-View -Id ($alarmMgr.GetAlarm($si.Content.RootFolder))
$ha = $alarms | where {$_.Info.Name -match "Host hardware"}
}
On the other hand, I do understand what this part of the code is doing.
It checks if the alarm already exists, but the scrip doesn't seem to remove it in that case.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That worked! Yes, you were correct in that we didn't have an alarms at the root folder. We had deleted all of the canned alarms and are working on replacing them with 40 or 50 newly created alarms that are what we are looking for specifically. Thanks so much for your help!