Tested on vCenter 7.0.3 22837322
# save as .ps1 and run in PowerShell I have put in bold what you need to change
# import script
function Replace-SpecialChars {
param($InputString)
$SpecialChars = '[#?\/{:}]'
$Replacement = '-'
$InputString -replace $SpecialChars,$Replacement
}
function ExportAlarm() {
if( -not (Test-Path -Path $exportFolder -PathType Container) ) {
mkdir -Path $exportFolder}
Connect-VIServer $serverToExportFrom -User 'administrator@vsphere.local' -Password 'VMware1!'
# Exclude vCenter specific Alarms containing "Exhaustion"in the name AND matching the filter
$alarmToExport = Get-AlarmDefinition $alarmToExportName | ? {$_.name -notmatch "Exhaustion"}
$alarmToExport | % {
$a = Get-View -Id $_.Id
$CustomAlarmName = $alarmprefix + ' ' + (Replace-SpecialChars -InputString $a.info.Name)
$a.info.Name = $CustomAlarmName
$ShortCustomAlarmName=$CustomAlarmName[1..84] -join ''
$a.Info | Export-Clixml -Path ($exportFolder + $ShortCustomAlarmName + ".xml") -Depth ( [System.Int32]::MaxValue )}
}
$serverToExportFrom = "172.168.1.5"
$alarmToExportName = "172-168-1-5alarms"
$exportFolder = "C:\temp\"
$alarmToExportName = "*"
ExportAlarm
Disconnect-VIServer * -Confirm:$False -Force:$True
——————————————————————————————————————–
# save as .ps1 and run in PowerShell I have put in bold what you need to change
#export script
function ImportAlarm {
Connect-VIServer $serverToImportTo -User 'administrator@vsphere.local'-Password 'VMware1!'
# Remove all but vCenter-specific Alarms from the target vCenter and wait for 15 seconds before we bulk-import the new Alarms
Get-AlarmDefinition | ? {$_.name -notmatch "Exhaustion"} |Remove-AlarmDefinition -Confirm:$false
sleep 15
Get-ChildItem -Path ($exportFolder + "*.xml") | % {
$deserializedAlarmInfo = Import-Clixml -Path $_.FullName
# We need to limit the Alarm-name to 80 characters for it to import correctly
$deserializedAlarmInfo.Name = ($deserializedAlarmInfo.Name[0..76] -join "")
$deserializedAlarmInfo.Description = ($deserializedAlarmInfo.Description[0..254] -join "")
$importedAlarmInfo = ConvertFromDeserialized( $deserializedAlarmInfo )
$entity = Get-Folder -NoRecursion
$alarmManager = Get-View -Id 'AlarmManager-AlarmManager'
$alarmManager.CreateAlarm($entity.Id, $importedAlarmInfo)}
}
# This function converts a Powershell object deserialized from xml (with Import-Clixml) to its original type (that has been previously serialized with Export-Clixml)
function ConvertFromDeserialized {
param($deserializedObject)
if($deserializedObject -eq $null){return $null}
$deserializedTypeName = ($deserializedObject | Get-Member -Force | where { $_.Name -eq "psbase" } ).TypeName;
if($deserializedTypeName.StartsWith("Deserialized.")) {
$originalTypeName = $deserializedTypeName.Replace("Deserialized.", "")
$result = New-Object -TypeName $originalTypeName
$resultType = $result.GetType()
if($resultType.IsEnum){
$result = [Enum]::Parse($resultType, $deserializedObject, $true)
return $result}
$deserializedObject | Get-Member | % {
# exclude "SystemName" entries from the XML-import as these will fail during the CreateAlarm call
if($_.MemberType -eq "Property" -and $_.Name -ne "SystemName") {
$resultProperty = $resultType.GetProperty($_.Name)
if($resultProperty.CanWrite ){
$propertyValue = ( Invoke-Expression ('$deserializedObject.' + $_.Name) | % { ConvertFromDeserialized( $_ ) } )
if($propertyValue -and $resultProperty.PropertyType.IsArray ) {
if($propertyValue.GetType().IsArray){
$elementTypeName = $resultProperty.PropertyType.AssemblyQualifiedName.Replace("[]", "")
$elementType = [System.Type]::GetType($elementTypeName)
$array = [System.Array]::CreateInstance($elementType, $propertyValue.Count)
for($i = 0; $i -lt $array.Length; $i++){
$array[$i] = $propertyValue[$i]}
$propertyValue = $array
} else {
$elementTypeName = $resultProperty.PropertyType.AssemblyQualifiedName.Replace("[]", "")
$elementType = [System.Type]::GetType($elementTypeName)
$array = [System.Array]::CreateInstance($elementType, 1)
$array[0] = $propertyValue
$propertyValue = $array}}
$resultProperty.SetValue($result, $propertyValue, $null)}}}
} else {
$result = $deserializedObject}
return $result
}
$serverToImportTo = "172.168.1.5"
$exportFolder = "C:\temp\"
ImportAlarm
Disconnect-VIServer * -Confirm:$false -Force:$true