FROM OpenExchangePSQuery(
POWERSHELL, '
param
(
[DateTime]$EndDate = (Get-date)
Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
#$psCred = Get-Credential
#$service.Credentials = $creds
#Credentials Option 2
$service.UseDefaultCredentials = $true
## Create a compilation environment
$Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Provider.CreateCompiler()
$Params=New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable=$False
$Params.GenerateInMemory=$True
$Params.IncludeDebugInformation=$False
$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
namespace Local.ToolkitExtensions.Net.CertificatePolicy{
public class TrustAll : System.Net.ICertificatePolicy {
public TrustAll() {
}
public bool CheckValidationResult(System.Net.ServicePoint sp,
System.Security.Cryptography.X509Certificates.X509Certificate cert,
System.Net.WebRequest req, int problem) {
return true;
}
}
}
'@
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
#$service.AutodiscoverUrl($MailboxName,{$true})
#"Using CAS Server : " + $Service.url
$service.Url = $uri
$start = Get-Date
$i = 0
foreach($Mailbox in $Mailboxes)
{
$i +=1
$Duration = (New-TimeSpan -Start ($start) -End (Get-Date)).totalseconds
$TimeLeft = ($Duration/$i)*($mailboxes.count - $i)
Write-Progress -Status "$($Mailbox.DisplayName)" -Activity "Mailbox $i of $($Mailboxes.Count)" -PercentComplete ($i/$($mailboxes.count)*100) -SecondsRemaining $timeleft -Id 100
$WorkingHoursStartTime = ($Mailbox | Get-mailboxCalendarConfiguration).WorkingHoursStartTime
$WorkingHoursEndTime = ($Mailbox | Get-mailboxCalendarConfiguration).WorkingHoursEndTime
if($WorkingDays -eq "AllDays"){$WorkingDays = "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday"}
if($WorkingDays -eq "WeekEndDays"){$WorkingDays = "Saturday,Sunday"}
$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $Mailbox.PrimarySMTPAddress)
$Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$folderid)
$cvCalendarview = new-object Microsoft.Exchange.WebServices.Data.CalendarView($StartDate,$EndDate,2000)
$cvCalendarview.PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$inPolicy = New-TimeSpan
$OutOfPolicy = New-TimeSpan
$TotalDuration = New-timespan
$BookableTime = New-TimeSpan
$c = 0
foreach ($apApointment in $frCalendarResult.Items){
$c +=1
Write-Progress -Status "$($Mailbox.DisplayName)" -Activity "Mailbox $i of $($Mailboxes.Count)" -PercentComplete ($i/$($mailboxes.count)*100) -SecondsRemaining $timeleft -Id 100 -CurrentOperation "Processing calendarItem $c or $($frCalendarResult.Items.count)"
$psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$apApointment.load($psPropset)
if($apApointment.IsAllDayEvent -eq $false)
{ if($apApointment.Duration)
{
if($WorkingDays.split(",") -contains ($apApointment.start).dayofweek)
{
$TotalDuration = $TotalDuration.add((new-timespan -End $apApointment.End.tolongTimeString() -start $apApointment.start.tolongTimeString())) #Only count to inPolicy if within the workinghours time
if($apApointment.start.tolongTimeString() -lt $WorkingHoursStartTime)
{
$tStart = $WorkingHoursStartTime.ToString()
}
else
{
$tStart = $apApointment.start.ToLongTimeString()
} if($apApointment.End.tolongTimeString() -gt $WorkingHoursEndTime)
{
$tEnd = $WorkingHoursEndTime.ToString()
}
else
{
$tEnd = $apApointment.End.ToLongTimeString()
$inPolicy = $inPolicy.add($Duration)
}
}
}
for ($d=$Startdate;$d -le $Enddate;$d=$d.AddDays(1)){
if ($WorkingDays.split(",") -contains $d.DayOfWeek)
{
$BookableTime += $WorkingHoursEndTime - $WorkingHoursStartTime
} #for
$rptobj = "" | Select samAccountName,DisplayName,inPolicy,OutOfPolicy,TotalDuration,BookableTime,BookedPercentage
$rptobj.samAccountName = $Mailbox.samAccountName
$rptobj.DisplayName = $Mailbox.DisplayName
$rptobj.inPolicy = '{0:f2}' -f ($inPolicy.TotalHours)
$rptobj.OutOfPolicy = '{0:f2}' -f (($TotalDuration - $inPolicy).TotalHours)
$rptobj.TotalDuration = '{0:f2}' -f ($TotalDuration.TotalHours)
$rptobj.BookableTime = '{0:f2}' -f ($BookableTime.TotalHours)
$rptobj.BookedPercentage = '{0:f2}' -f (($inPolicy.TotalHours / $BookableTime.TotalHours) * 100)
$rptcollection += $rptobj
}
)