# -----------------------------------------------
# Объединенные календари
# -----------------------------------------------
function New-CalendarOverlayInfo()
{
  param ($CalendarOverlayXml)

  $calendarOverlayInfo = new-object PSObject

  # Наименование объединенного календаря
  $calendarOverlayInfo | add-member -type NoteProperty -Name Title -Value $CalendarOverlayXml.Name

  # Календари
  $calendars = @()  
  foreach($calendar in  $CalendarOverlayXml.Calendar) { $calendars += $calendar.Name }   
  $calendarOverlayInfo | add-member -type NoteProperty -Name CalendarNames -Value $calendars 

  return $calendarOverlayInfo

}

# -----------------------------------------------
# -----------------------------------------------
# -----------------------------------------------
function CreateCalendarOverlayByCalendarOverlayInfo()
{
    param ($SPWeb, $CalendarOverlayInfo)    
    
    Write-Host $CalendarOverlayInfo.Title -ForegroundColor: Green
    
    $targetList = $null
    [int]$color = 9
    foreach($calendar in $CalendarOverlayInfo.CalendarNames)        
    {           
        Write-Host "Создаем календарь " $calendar           
        [GUID]$listId = $SPWeb.Lists.Add($calendar, $CalendarOverlayInfo.Title, [Microsoft.SharePoint.SPListTemplateType]::Events)                             
        $list = $SPWeb.Lists[$listId]
     
        if($targetList)
        {
            # Добавляем объединенный календарь
            Add-SPCalendarOverlay -TargetList: $targetList -OverLayList: $list -Color: $color -ErrorAction: Stop
            $color--
            if($color -eq 0){ $color= 9 }
        }
        else
        {
            # Основной календарь
            $targetList = $list
        }
    }

    # Делаем заголовок на QuickLaunch
    AddHeading  $CalendarOverlayInfo.Title $targetList.DefaultViewUrl $SPWeb
}