robie2011
4/30/2014 - 2:15 PM

Scan Outlook Calendar Entries (Powershell)

Scan Outlook Calendar Entries (Powershell)

clear

$StartDatum = "01.01.13"
$EndDatum = "01.09.13"
$users = @{ "max müller" = @(); "peter pan" = @(); "mitz mausi" = @(); "fun farah" = @()}
$LookupCalender = "Calendar Name"


$usersKW = $users.Clone()
$outHash = $users.Clone()

Add-Type -AssemblyName Microsoft.Office.Interop.Outlook

$class = @"
using Microsoft.Office.Interop.Outlook;public class MyOL
{
    public MAPIFolder GetCalendar(string userName)
    {
        Application oOutlook = new Application();
        NameSpace oNs = oOutlook.GetNamespace("MAPI");
        Recipient oRep = oNs.CreateRecipient(userName);
        MAPIFolder calendar = oNs.GetSharedDefaultFolder(oRep, OlDefaultFolders.olFolderCalendar);
        return calendar;
    }
}
"@

Add-Type $class -ReferencedAssemblies Microsoft.Office.Interop.Outlook
$MyOL = New-Object MyOL
$olInbox = $MyOL.GetCalendar($LookupCalender)

#http://cjoprey.blog.com/2010/03/09/getting-another-users-outlook-folder/

# Get-CalendarWeek by Holger Adam
# Simple function to retrieve the calendar week to a given or the current date.

function Get-CalendarWeek 
{
 param($Date = (Get-Date))
 
 # get current culture object
 $Culture = [System.Globalization.CultureInfo]::CurrentCulture
 
 # retrieve calendar week
 $Culture.Calendar.GetWeekOfYear($Date, $Culture.DateTimeFormat.CalendarWeekRule, 
 $Culture.DateTimeFormat.FirstDayOfWeek)
} #end function Get-CalendarWeek



$olInbox.Items.Restrict("[Start] > '$StartDatum' AND [End] < '$EndDatum'") | ?{ $_.Subject -match "Appointment-Descriptions-(Pattern)-Comes-Here" } | %{	
	$tmp = $users.Clone();
	foreach($u in $users.Keys){
		if ($_.RequiredAttendees -match $u){
			$tmp[$u] += $_.Start
			$usersKW[$u] += Get-CalendarWeek( Get-Date($_.Start) )
		}
	}
	$users = $tmp
}



$users.Keys | %{
	$multipleTimesInWeek = $users[$_].count - ($usersKW[$_] | Sort-Object | Get-Unique ).Count
	if($multipleTimesInWeek -gt 1){	
		$o = ("{0} ({1} davon kommmen mehrfach in derselben Woche vor)" -f  $users[$_].count,  $multipleTimesInWeek)	
	}else{
		$o = $users[$_].count
	}
	$outHash[$_] = $o
}

# $users
$outHash.GetEnumerator() | Sort Value -Descending