簡體   English   中英

如何檢查“外出”是否已啟用,如果已禁用,請啟用它

[英]How to check if Out of office is enabled, and if disabled, enable it

我正在嘗試與我們的帳戶終止流程建立集成。 終止時,尚未離開辦公室。

我要放在公共main()中運行的內容-如果未設置IsOutOfOfficeSet,則什么也不做,先設置它,然后運行SetOutofOffice。
否則,如果有人有更好的代碼來檢查是否設置了不在辦公室,如果設置了則忽略,否則進行設置?

class Script : ScriptBase
{

    const bool bTest = false;   //set to true to not set OOF
    const bool bDebug = false;  //set to true for more logging
    ExchangeResource2010 exchange = ProvisioningSystem.Organisation.Resources["Exchange Servers"] as ExchangeResource2010;

    public void main()
    {
    }

    public bool IsOutOfOfficeSet(UserDirectoryEntry user)
    {
        StringBuilder sb = new StringBuilder();

        try
        {
            foreach (PSObject o in exchange.Exec("Get-MailboxAutoReplyConfiguration", "Identity", user.DomainName, "DomainController", user.ServerName))
            {

                string autoReplyState = o.Properties["AutoReplyState"].Value.ToString();
                DateTime startTime = Convert.ToDateTime(o.Properties["StartTime"].Value);
                DateTime endTime = Convert.ToDateTime(o.Properties["EndTime"].Value);


                if (bDebug)
                {
                    sb.AppendLine(String.Format("AutoReplyState={0}", autoReplyState));
                    sb.AppendLine(String.Format("StartTime={0}", startTime));
                    sb.AppendLine(String.Format("EndTime={0}", endTime));

                    if (Job != null)
                        Job.LogAudit("AutoReplyConfiguration", sb.ToString());
                    else
                        Trace.WriteLine("AutoReplyConfiguration\r\n" + sb.ToString());
                }


                if (String.Compare(autoReplyState, "Disabled") == 0)
                    return false;

                if (String.Compare(autoReplyState, "Enabled") == 0)
                    return true;

                if (String.Compare(autoReplyState, "Scheduled") == 0)
                {

                    //If scheduled OOO has not ended then return true
                    if (endTime > DateTime.Now)
                        return true;
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error retrieving auto reply configuration for " + user.DomainName, ex);

        }

        return false;
    }

    public void Setoutofoffice(UserDirectoryEntry user)
    {

        //Out of office should be termination date plus 30 days
        DateTime fromDate = DateTime.Now;
        DateTime toDate = DateTime.Now.AddDays(30); //end out of office within 30 days from today

        string message = Evaluator.GetString(Job, "=//Job/Task/OOOMessage");
        LogAudit("Scheduling out of office for {0} from {1} to {2}", user, fromDate, toDate);

        ExchangeResource exchange = ProvisioningSystem.Organisation.Resources["Exchange Servers"] as ExchangeResource;
        if (exchange == null) throw new Exception("Exchange resource is NULL");
        ((ExchangeResource2010)exchange).ExecNoResults("Set-MailboxAutoReplyConfiguration", "Identity", user.EmailAddress, "AutoReplyState", "Scheduled", "StartTime", fromDate.ToString("MM/dd/yyyy"), "EndTime", toDate.ToString("MM/dd/yyyy"), "Confirm", false, "-InternalMessage", message, "ExternalMessage", message, "ExternalAudience", "All", "DomainController", user.ServerName);


    }    
}

你只想寫嗎?

public void main()
{
    if(!IsOutOfOfficeSet(user)){
        Setoutofoffice(user);
    }
}

其中user是有效的UserDirectoryEntry對象

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM