如何将Microsoft.Exchange.Common.ScheduleInterval与日期时间对象进行比较?

时间:2012-09-21 04:00:41

标签: powershell exchange-server

我想知道是否有任何Exchange数据库计划正在运行,或者将在接下来的几个小时内运行。但是,我无法弄清楚如何将scheduleinterval对象与我用get-date或(get-date)创建的变量进行比较.addhours(20)

我正在查看的scheduleInterval是

C:\>(get-mailboxdatabase | where {$_.name -like 'database'}).maintenanceschedule  | Get-Member

   TypeName: Microsoft.Exchange.Common.ScheduleInterval

Name         MemberType Definition
----         ---------- ----------
CompareTo    Method     int CompareTo(System.Object value), int CompareTo(Microsoft.Exchange.Common.ScheduleInterval...
ConjointWith Method     bool ConjointWith(Microsoft.Exchange.Common.ScheduleInterval other)
Contains     Method     bool Contains(Microsoft.Exchange.Common.WeekDayAndTime dt), bool Contains(System.DateTime dt...
Equals       Method     bool Equals(System.Object obj)
GetHashCode  Method     int GetHashCode()
GetType      Method     type GetType()
Overlaps     Method     bool Overlaps(Microsoft.Exchange.Common.ScheduleInterval other)
ToString     Method     string ToString()
EndDay       Property   System.DayOfWeek EndDay {get;}
EndHour      Property   System.Int32 EndHour {get;}
EndMinute    Property   System.Int32 EndMinute {get;}
EndTime      Property   Microsoft.Exchange.Common.WeekDayAndTime EndTime {get;}
Length       Property   System.TimeSpan Length {get;}
StartDay     Property   System.DayOfWeek StartDay {get;}
StartHour    Property   System.Int32 StartHour {get;}
StartMinute  Property   System.Int32 StartMinute {get;}
StartTime    Property   Microsoft.Exchange.Common.WeekDayAndTime StartTime {get;}

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:1)

这是一种测试作业是否以Get-Date值运行的方法。 也许可以将starttime/endtime properties投射到[datetime]进行比较,但我无法测试,我只是假设..

$a = (get-mailboxdatabase | where {$_.name -like 'database'}).maintenanceschedule
$b = Get-date # you can add days to check events in the future
if ( $a.startday, $a.endday -contains $b.dayofweek)
{
  if ( $a.starthour -le $b.hour -and $a.endhour -ge $b.hour)
  {
    if (  $a.startminute -le $b.minute -and $a.endminute -ge $b.minute)
    {
       Write-Host "Jod scheduled is running at this time!"
    }
  }
}

答案 1 :(得分:0)

我编写了以下代码,将Microsoft.Exchange.Common.ScheduleInterval转换为datetime对象数组。希望有人可以改进我的代码...

$schedules=(get-mailboxdatabase|where {$_.name -like "dbname"}).maintenanceschedule 

$curdatetime = get-date
$dowhash = @{"Monday"=1;"Tuesday"=2;"Wednesday"=3;"Thursday"=4; `
             "Friday"=5;"Saturday"=6;"Sunday"=7}
$todaydow = $dowhash.get_item([string]$curdatetime.dayofweek)
$coming = @()

foreach ($schedule in $schedules) {
  $dow = $dowhash.get_item([string]$schedule.startday)
  $start = $curdatetime.adddays((($dow+7-$todaydow) % 7))
  $startstr = "{0:yyyyMMdd}{1:00}{2:00}" `
              -f $start,$schedule.starthour,$schedule.startminute

  $dow = $dowhash.get_item([string]$schedule.endday)
  $end = $curdatetime.adddays((($dow+7-$todaydow) % 7))
  $endstr   = "{0:yyyyMMdd}{1:00}{2:00}" `
              -f $end,$schedule.endhour,$schedule.endminute

  $coming+=,([datetime]::ParseExact($startstr,"yyyyMMddHHmm",$null), `
             [datetime]::ParseExact($endstr,"yyyyMMddHHmm",$null) )
}

由于