0

我们有大约 90 个房间邮箱作为交换 2010,现在我想使用 powershell 提取交换服务器 2010 中房间的日历报告。报告应包含以下细节。

一个。每个特定房间一周的房间使用情况(使用情况与房间当天可用的小时数有关) b. 每个特定房间一个月的房间使用情况(使用情况与该房间在一周内可用的小时数有关) c.每个用户的预订数量 d. 取消的预订数量 e. 重新安排的预订数量

是否有可能使用exchage服务器中的powershell将具有上述要点的报告导入excel?

如果我从 Outlook 导出房间日历,我只能得到一个房间的报告。是否可以获得所有房间日历的报告?

4

3 回答 3

1

我做过这样的事情..

您需要安装 Exchange Managed API 才能正常工作。

New-ManagementRoleAssignment –Name:impersonationAssignmentName –Role:ApplicationImpersonation –User:serviceAccount

请参阅http://msdn.microsoft.com/en-us/library/exchange/bb204095%28v=exchg.140%29.aspx

运行脚本时,系统将提示您输入凭据。请务必指定域 (DomainName\UserId)

还更改脚本中的 cashub 服务器(或使用自动发现)

另存为 Get-RoomUsage.ps1 可选参数为 -StartDate 和 -Enddate

param 
(

[DateTime]$StartDate = (Get-Date).addDays(-30),
[DateTime]$EndDate = (Get-date)

)

$Mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails RoomMailbox #| select -First 10


$rptcollection = @()

## Load Managed API dll  
Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"  

## Set Exchange Version  
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2  

## Create Exchange Service Object  
$service = New-Object    Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)  

## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials  

#Credentials Option 1 using UPN for the windows Account  
$psCred = Get-Credential  

$creds = New-Object    System.Net.NetworkCredential($psCred.GetNetworkCredential().username.ToString(),$psCred.GetNetworkCredential().password.ToString(),$psCred.GetNetworkCredential().domain.ToString())  
$service.Credentials = $creds   


#Credentials Option 2  
#$service.UseDefaultCredentials = $true  

## Choose to ignore any SSL Warning issues caused by Self Signed Certificates  

## Code From http://poshcode.org/624
## 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

$TASource=@'
  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

## We now create an instance of the TrustAll and attach it to the ServicePointManager
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll

## end code from http://poshcode.org/624

## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use  

#CAS URL Option 1 Autodiscover  
#$service.AutodiscoverUrl($MailboxName,{$true})  
#"Using CAS Server : " + $Service.url   

#CAS URL Option 2 Hardcoded  

$uri=[system.URI] "https://cashubserver/ews/exchange.asmx"  
$service.Url = $uri    

$obj = @{}
$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


$WorkingDays = ($Mailbox | Get-mailboxCalendarConfiguration).WorkDays.ToString()
$WorkingHoursStartTime = ($Mailbox |    Get-mailboxCalendarConfiguration).WorkingHoursStartTime
$WorkingHoursEndTime = ($Mailbox | Get-mailboxCalendarConfiguration).WorkingHoursEndTime

if($WorkingDays -eq "Weekdays"){$WorkingDays = "Monday,Tuesday,Wednesday,Thursday,Friday"}
if($WorkingDays -eq "AllDays"){$WorkingDays = "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday"}
if($WorkingDays -eq "WeekEndDays"){$WorkingDays = "Saturday,Sunday"}

## Optional section for Exchange Impersonation  
$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $Mailbox.PrimarySMTPAddress) 

$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnown    FolderName]::Calendar,$Mailbox.PrimarySMTPAddress.tostring())   
$Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$folderid)

if($Calendar.TotalCount -gt 0){
    $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)

$frCalendarResult = $Calendar.FindAppointments($cvCalendarview)
$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()

                    }

                    $Duration = New-TimeSpan -Start $tStart -End $tEnd
                    $inPolicy = $inPolicy.add($Duration)
                }
            }
     }

}

#Calculate to total hours of bookable time between the 2 dates
for ($d=$Startdate;$d -le $Enddate;$d=$d.AddDays(1)){
  if ($WorkingDays.split(",") -contains $d.DayOfWeek) 
  {
    $BookableTime += $WorkingHoursEndTime - $WorkingHoursStartTime

  }
  } #for

#Save result....
$rptobj = "" | Select samAccountName,DisplayName,inPolicy,Out-Of-Policy,TotalDuration,BookableTime,BookedPersentage
$rptobj.samAccountName = $Mailbox.samAccountName
$rptobj.DisplayName = $Mailbox.DisplayName
$rptobj.inPolicy =  '{0:f2}' -f ($inPolicy.TotalHours)
$rptobj."Out-Of-Policy" =  '{0:f2}' -f (($TotalDuration - $inPolicy).TotalHours)
$rptobj.TotalDuration =  '{0:f2}' -f ($TotalDuration.TotalHours)
$rptobj.BookableTime =  '{0:f2}' -f ($BookableTime.TotalHours)
$rptobj.BookedPersentage =  '{0:f2}' -f (($inPolicy.TotalHours / $BookableTime.TotalHours) * 100)
$rptcollection += $rptobj

} #ForEach
}

$rptcollection

它会输出这样的东西。

samAccountName   : room1
DisplayName      : Room Mailbox 1
inPolicy         : 98,50
Out-Of-Policy    : 2,75
TotalDuration    : 101,25
BookableTime     : 207,00
BookedPersentage : 47,58

samAccountName   : room2
DisplayName      : Room Mailbox 2
inPolicy         : 35,92
Out-Of-Policy    : 0,00
TotalDuration    : 35,92
BookableTime     : 207,00
BookedPersentage : 17,35
于 2012-09-27T09:46:27.277 回答
0

您是否为您的用户添加了ManagementRoleAssignment

New-ManagementRoleAssignment –Name:impersonationAssignmentName –Role:ApplicationImpersonation –User:serviceAccount
于 2014-01-26T13:05:32.493 回答
-1

不确定这是否会绊倒其他任何人,但在一个不应该有它们的地方有空间。我的以下行的复制粘贴包含 WellKnow 和 FolderName 之间的空格

$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnown FolderName]::Calendar,$Mailbox.PrimarySMTPAddress.tostring())

于 2013-08-09T17:38:31.627 回答