Hello,
The following version of the script determines whether you are out of office basing on OOF configuration. Use it instead of the script that you currently have.
[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")
$ruleName = "My Rule" # TODO: Modify me
$defaultApprover = "John Doe,CN=Users,DC=example,DC=com" # TODO: Modify me
$reasonMessage = "I'm at the office now and can approve Requests myself!" # TODO: Modify me
$scriptDescription = "Check whether Request procession is allowed" # TODO: Modify me
$scriptBlock = @"
`$defaultApproverDN = `"$defaultApprover`" `# TODO: Modify me
`$Context.ConditionIsMet = `$False
`# Check whether the Request is Approved, Denied or Cancelled
`$requestBeingProcessed = `$Context.IsPropertyModified(`"adm-ApprovalState`")
if (`!`$requestBeingProcessed)
{
return
}
`# Allow Request Initiators to Cancel their own Requests
`$requestStatus = `$Context.GetModifiedPropertyValue(`"adm-ApprovalState`")
if (`$requestStatus -eq 3)
{
return
}
`# Allow processing to the default approver
if (`"`%adm-InitiatorDN`%`" -ieq `$defaultApproverDN)
{
return
}
`# Check whether the default approver is in office
`$defaultApproverObj = `$Context.BindToObjectByDN(`$defaultApproverDN)
# Get Auto-Reply configuration
`$mailboxParams = `$defaultApproverObj.GetMailParameters()
`$automaticReplies = `$mailboxParams.AutoReplyConfiguration
# Get OOF status
`$automaticRepliesStatus = `$automaticReplies.AutoReplyState
switch (`$automaticRepliesStatus)
{
"ADM_EXCHANGE_OOFSTATETYPE_DISABLED"
{
# OOF disabled: the default approver is in office
`$Context.ConditionIsMet = `$True
return
}
"ADM_EXCHANGE_OOFSTATETYPE_ENABLED"
{
# OOF enabled: the default approver is out of office
return
}
"ADM_EXCHANGE_OOFSTATETYPE_SCHEDULED"
{
# OOF scheduled: check whether OOF is enabled right now
# Get current date
`$currentDate = Get-Date
# Check when OOF is scheduled to be enabled
`$startDate = `$automaticReplies.StartTime
`$endDate = `$automaticReplies.EndTime
if ((`$startDate -lt `$currentDate) -and (`$endDate -gt `$currentDate))
{
return
}
`$Context.ConditionIsMet = `$True
}
}
"@
# Connect to Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")
# Bind to the 'Business Rules' container
$businessRulesPath = $admService.Backend.GetConfigurationContainerPath(
"BusinessRules")
$businessRulesContainer = $admService.OpenObject($businessRulesPath,
$NULL, $NULL, 0)
# Create a new Business Rule
$rule = $businessRulesContainer.Create("adm-BusinessRule", "CN=$ruleName")
# Triggering Operation: Before updating an Approval Request
$rule.ExecutionMoment = "ADM_BUSINESSRULEEXECMOMENT_BEFORE"
$rule.ObjectType = "adm-ApprovalRequest"
$rule.OperationType = "set properties"
$rule.Disabled = $False
$rule.SetInfo()
# Include All Objects in the Activity Scope of the Business Rule
$scopeItem = $rule.ActivityScopeItems.Create()
$scopeItem.BaseObject = $NULL
$scopeItem.Type = "ADM_SCOPEBASEOBJECTTYPE_ALL_DIRECTORY"
$scopeItem.Inheritance = "ADS_SCOPE_SUBTREE"
$scopeItem.Exclude = $False
$scopeItem.SetInfo()
$rule.ActivityScopeItems.Add($scopeItem)
$rule.SetInfo()
# Create a new set of actions and conditions
$actionsAndConditions = $rule.ConditionedActions.Create()
$actionsAndConditions.ConditionsLogicalOperation = "ADM_LOGICALOPERATION_AND"
$actionsAndConditions.SetInfo()
# Create the 'If PowerShell returns True' condition that performs all the checks
$condition = $actionsAndConditions.Conditions.CreateEx("adm-ScriptCondition")
$scriptCondition = $condition.GetCondition()
$scriptCondition.ScriptDescription = $scriptDescription
$scriptCondition.Script = $scriptBlock
$condition.SetCondition($scriptCondition)
$condition.SetInfo()
$actionsAndConditions.Conditions.Add($condition)
# Add 'Cancel this operation' action
$action = $actionsAndConditions.Actions.CreateEx("adm-CancelOperationAction")
$action.ExecutionOptions = "ADM_ACTIONEXECUTIONOPTIONS_SYNC"
$actionObj = $action.GetAction()
$actionObj.ReasonMessage = $reasonMessage
$action.SetAction($actionObj)
$action.SetInfo()
$actionsAndConditions.Actions.Add($action)
# Add the set to the Business Rule
$rule.ConditionedActions.Add($actionsAndConditions)
# Save the Business Rule
$rule.SetInfo()