Hi,
I'm writting a small code to export alarms from one vcenter to another. I know there is existing scripts that already do that, but I couldn't find one that works in multiple locations.
Here is what I wrote :
Functions :
function getAlarms{
param($location)
$origin = $location | Get-View | Get-VIObjectByVIView
$alarms = Get-AlarmDefinition -entity $origin
return $alarms
}
function createAlarms{
param($location,$alarms)
$destination = $location | get-view
$alarmMgr = Get-View AlarmManager
ForEach($alarm in $alarms)
{
$newalarm=$null
$newAlarm = New-Object VMware.Vim.AlarmSpec
$newAlarm = (Get-View $alarm).Info
try {
$alarmMgr.CreateAlarm($destination.MoRef,$newAlarm)
}
catch {
#alarms already exist because vcenter returns alarms created in the parent folders as if they were created here,
#so can't avoid duplicates when browsing containers
}
}
}
Export the alarms :
$alarmsSRC = @{}
#save alarms in vcenter's root :
$alarmsSRC["root"] = getAlarms -location (get-folder -norecursion)
$alarmsSRC["datacenters"] = @{}
$datacenters = Get-Datacenter
ForEach ($datacenter in $datacenters) {
$alarmsSRC["datacenters"][$datacenter.Name] = @{}
$alarmsSRC["datacenters"][$datacenter.Name]["root"] = getAlarms -location $datacenter
$folders = Get-Folder -location $datacenter -Type vm | where-object {$_.Parent -like "vm"} #only first sublevel of folders of a specific datacenter
ForEach ($folder in $folders) {
$alarmsSRC["datacenters"][$datacenter.Name][$folder.Name] = getAlarms -location $folder
}
}
From here all is fine, but now I need to recreate em and I don't know how to browse my array...
For exemple, I get an array like this :
$alarmsSRC["root"] = alarms_in_root
$alarmsSRC["datacenters"]["Datacenter 1"]["root"] = alarms_in_root_of_datacenter1
$alarmsSRC["datacenters"]["Datacenter 1"]["Folder A"] = alarms_in_folderA_of_datacenter1
$alarmsSRC["datacenters"]["Datacenter 1"]["Folder B"] = alarms_in_folderB_of_datacenter1
$alarmsSRC["datacenters"]["Datacenter 2"]["root"] = alarms_in_root_of_datacenter2
$alarmsSRC["datacenters"]["Datacenter 2"]["Folder A"] = alarms_in_folderA_of_datacenter2
$alarmsSRC["datacenters"]["Datacenter 2"]["Folder B"] = alarms_in_folderB_of_datacenter2
How to use my createAlarms function with this array ?
I'm a little stuck on how to extract the name of the datacenter and folders.
Here is what I've done to recreate the alarms :
ForEach ($datacenter in $alarmsSRC["datacenters"]) {
ForEach ($folder in $datacenter) {
if (//$folder.Name// -eq "root")
{
#create alarms at the root of this datacenter
createAlarms -location (get-datacenter -name //$datacenter.Name//) -alarms $folder
} else {
createAlarms -location (get-folder -name //$folder.Name// -Type vm -location (get-datacenter -name //$datacenter.Name//) | where-object {$_.Parent -like "vm"} ) -alarms $folder
}
}
}
As you can see I'm looking to extract "key" names of the array like "Datacenter 1", "Datacenter 2", "Folder A", etc.
Thanks for any help
The $alarmsSRC variable contains a collection of hash tables. You can use the GetEnumerator() method to enumerate the entries in a hash table. The following PowerCLI script shows you how to do that in your case. I leave it as an exercise to you to modify the script to create the alarms.
$alarmsSRC = @{}
$alarmsSRC["datacenters"] = @{}
$alarmsSRC["datacenters"]["Datacenter 1"] = @{}
$alarmsSRC["datacenters"]["Datacenter 2"] = @{}
$alarmsSRC["root"] = 'alarms_in_root'
$alarmsSRC["datacenters"]["Datacenter 1"]["root"] = 'alarms_in_root_of_datacenter1'
$alarmsSRC["datacenters"]["Datacenter 1"]["Folder A"] = 'alarms_in_folderA_of_datacenter1'
$alarmsSRC["datacenters"]["Datacenter 1"]["Folder B"] = 'alarms_in_folderB_of_datacenter1'
$alarmsSRC["datacenters"]["Datacenter 2"]["root"] = 'alarms_in_root_of_datacenter2'
$alarmsSRC["datacenters"]["Datacenter 2"]["Folder A"] = 'alarms_in_folderA_of_datacenter2'
$alarmsSRC["datacenters"]["Datacenter 2"]["Folder B"] = 'alarms_in_folderB_of_datacenter2'
$alarmsSRC["root"]
ForEach ($datacenter in $alarmsSRC["datacenters"].GetEnumerator()) {
ForEach ($folder in $alarmsSRC["datacenters"][$datacenter.Key].GetEnumerator()) {
$Folder.Key + " is " + $Folder.value
}
}
The $alarmsSRC variable contains a collection of hash tables. You can use the GetEnumerator() method to enumerate the entries in a hash table. The following PowerCLI script shows you how to do that in your case. I leave it as an exercise to you to modify the script to create the alarms.
$alarmsSRC = @{}
$alarmsSRC["datacenters"] = @{}
$alarmsSRC["datacenters"]["Datacenter 1"] = @{}
$alarmsSRC["datacenters"]["Datacenter 2"] = @{}
$alarmsSRC["root"] = 'alarms_in_root'
$alarmsSRC["datacenters"]["Datacenter 1"]["root"] = 'alarms_in_root_of_datacenter1'
$alarmsSRC["datacenters"]["Datacenter 1"]["Folder A"] = 'alarms_in_folderA_of_datacenter1'
$alarmsSRC["datacenters"]["Datacenter 1"]["Folder B"] = 'alarms_in_folderB_of_datacenter1'
$alarmsSRC["datacenters"]["Datacenter 2"]["root"] = 'alarms_in_root_of_datacenter2'
$alarmsSRC["datacenters"]["Datacenter 2"]["Folder A"] = 'alarms_in_folderA_of_datacenter2'
$alarmsSRC["datacenters"]["Datacenter 2"]["Folder B"] = 'alarms_in_folderB_of_datacenter2'
$alarmsSRC["root"]
ForEach ($datacenter in $alarmsSRC["datacenters"].GetEnumerator()) {
ForEach ($folder in $alarmsSRC["datacenters"][$datacenter.Key].GetEnumerator()) {
$Folder.Key + " is " + $Folder.value
}
}
Thanks, I didnt find anywhere the getenumerator tip that make the .key value accessible.
I was going to make a workarround and store the key name in the first row ...