Use PowerShell to add Holidays to Outlook Calendar

Geeking around with PowerShell today trying to add all company holidays to my Outlook calendar. In the script, I’m creating all day appointments and setting the Show As to out of office. Simple enough!

function get-mailfolders { 
	$outlookfolders = @() 
	$outlook = New-Object -ComObject Outlook.Application 
	foreach ($folder in $outlook.Session.Folders){ 

		foreach($mailfolder in $folder.Folders ) { 
			$olkf = New-Object PSObject -Property @{ 
				Path = $($mailfolder.FullFolderPath) 
				EntryID = $($mailfolder.EntryID) 
				StoreID = $($mailfolder.StoreID) 
			} 

			$outlookfolders += $olkf 
		} 
	} 
	$outlookfolders 
}

$outlook = new-object -com Outlook.Application
$folder = get-mailfolders | where {$_.Path -like "*calendar*" -and $_.Path -and $_.Path -like "*$mailbox*"} 
$calendar = $outlook.Session.GetFolderFromID($folder.EntryID, $folder.StoreID) 

$holidays = @{"01/01/2017"="New Year’s Day"; "01/16/2017"="Martin Luther King Day"; "02/02/2017"="Presidents Day"; "05/29/2017"="Memorial Day"; `
	"07/04/2017"="Independence Day"; "11/4/2017"="Labor Day"; "11/23/2017"="Thanksgiving Break"; "11/24/2017"="Thanksgiving Break"; "12/25/2017"="Christmas Day"}

foreach($holiday in $holidays.GetEnumerator() | Sort Key)
{
	[string]$hName = $holiday.Value
	$hDate = Get-Date $holiday.Key
	
	$appt = $calendar.Items.Add(1) 
	$appt.Start = $holiday.key.ToString()
	$appt.AllDayEvent = $true
	$appt.Subject = $hName
	$appt.Body = $hName

	<#
	Show As / Status
	0 = Free 
	1 = Tentative 
	2 = Busy 
	3 = Out of Office 
	#>
	$appt.BusyStatus = 3

	$appt.Save()
}

This post helped guide me in the right direction:
Create a calendar item

Task ‘SharePoint’ reported error (0x80004005): an error occurred in this SharePoint list

From what I have read in other blogs and forums, this might not be the fix for everyone.

Error in Outlook:
0x80004005

Using Fiddler, I was seeing the same reference to http://randomSite.SP.com.

In my case, it turned that I had an outdated / not used URL in my Alternate Access Mappings.
Default Zone was set to http://goodSite.SP.com
Intranet Zone was set to http//randomSite.SP.com

Once http://randSIte.SP.com was removed, the Outlook error went away.
(removing the mapping did NOT induce an IISreset.)

Alternate Access Mappings can be found here:
http://YourCentralAdminSite/_admin/AlternateUrlCollections.aspx

Full error:
Task ‘SharePoint’ reported error (0x80004005) : ‘An error occurred in this SharePoint List (site name – list name). Try updating the folder again. If the problem continues, contact the SharePoint site administrator. HTTP 500.
The server returned the following error message: Exception of type
‘Microsoft.SharePoint.SoapServer.SoapServerException’ was thrown.’

Fiddler referenced this web service:
GetListItemChangesSinceToken