Hello again, VicMware-
For the function, there are (as always), plenty of ways to do so. The script file that markdjones82 made should do just fine. Or, if you want an inventory type of path to the entity where the alarm is defined, you could use LucD's Get-AlarmLocation function.
Or, to get some of both (the info that I was returning plus the inventory path info that LucD got for you), you could do something like:
## function to build a path string for an inventory entity -- basically LucD's, but including the "hidden" folder info for clarity
functionGet-EntityPath {
param ($oEntity)
## start off with the entity itself whose parent path to generate
$oPathPiece=$oEntity
## the string that will hold the path to the entity (starts as the entity name)
$strPath=$oPathPiece.Name
## while there is a .Parent property of the entity at hand, get the info about that Parent
while($oPathPiece.Parent) {
$oPathPiece=Get-View$oPathPiece.Parent -Property Name,Parent
$strPath=$oPathPiece.Name +"/"+$strPath
} ## end while
## return the path
"/$strPath"
} ## end function
## define an array of items to select from objects returned by Get-AlarmDefinition
$arrAlarmDefItemsToSelect="name","enabled",@{n="EntityName"; e={$_.Entity.Name}},@{n="EntityType"; e={$_.Entity.ExtensionData.GetType().Name}},@{n="EntityPath"; e={Get-EntityPath (Get-View-Property Name,Parent $_.ExtensionData.Info.Entity)}}
This approach uses a function to get the entity inventory path (basically the code that LucD provided) and an array of properties to select with Select-Object, then just lets you use the native Get-AlarmDefinition cmdlet to get the alarm(s) of you choosing and pipe the output to a select statement. Like:
## get alarm defs by name, then select some interesting stuff
Get-AlarmDefinition-Name d*Test0 | select$arrAlarmDefItemsToSelect | ft-AutoSize
## or, find alarms defined for given entities (including inherited definitions), and select things
Get-AlarmDefinition-Entity (Get-VMmyVM0),(Get-ClustermyCluster0) | select$arrAlarmDefItemsToSelect | ft-AutoSize
This allows you to still use other parameters on Get-AlarmDefinition (like -Name, or -Entity for particular entities if you already have them, or to filter with -Enabled, etc.), but to also get the custom properties desired in the output. Sample output from the first example:
Name Enabled EntityName EntityType EntityPath ---- ------- ---------- ---------- ---------- dDatastoreAlarmTest0 False dstore0 Datastore /Datacenters/dc0/datastore/dstore0 dClusterAlarmTest0 True myCluster1 ClusterComputeResource /Datacenters/dc0/host/myCluster1 dVMAlarmDefinitionTest0 False someVM09 VirtualMachine /Datacenters/dc0/vm/XDVMs/someVM09 dFolderAlarmDefTest0 False PowerCLILab Folder /Datacenters/dc0/vm/PowerCLILab
Or, you could change it up to fit you wants/needs, too. Enjoy.