当前位置: 首页>>代码示例>>C#>>正文


C# PSCommand.AddStatement方法代码示例

本文整理汇总了C#中System.Management.Automation.PSCommand.AddStatement方法的典型用法代码示例。如果您正苦于以下问题:C# PSCommand.AddStatement方法的具体用法?C# PSCommand.AddStatement怎么用?C# PSCommand.AddStatement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Management.Automation.PSCommand的用法示例。


在下文中一共展示了PSCommand.AddStatement方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Set_MailboxCalendarPermission

        /// <summary>
        /// Sets the mailbox default calendar permissions by removing the default and adding their company's ExchangeSecurity group to the AvailabilityOnly
        /// </summary>
        /// <param name="userPrincipalName"></param>
        /// <param name="companyCode"></param>
        public void Set_MailboxCalendarPermission(string userPrincipalName, string companyCode)
        {
            PowerShell powershell = null;

            try
            {
                // Strip whitespace from company code
                companyCode = companyCode.Replace(" ", string.Empty);

                // DEBUG
                logger.Debug("Removing default permissions for mailbox calendar and adding correct permissions for user " + userPrincipalName);

                // Start clock
                Stopwatch stopwatch = Stopwatch.StartNew();
                
                // Run commands
                powershell = PowerShell.Create();
                powershell.Runspace = runspace;

                // Enable the mailbox
                PSCommand cmd = new PSCommand();

                // Get the calendar name
                string calendarName = Get_CalendarName(userPrincipalName);

                // Remove default calendar permissions
                cmd.AddCommand("Set-MailboxFolderPermission");
                cmd.AddParameter("Identity", string.Format(@"{0}:\{1}", userPrincipalName, calendarName));
                cmd.AddParameter("User", "Default");
                cmd.AddParameter("AccessRights", "None");
                cmd.AddParameter("DomainController", this.domainController);

                // Add calendar permissions for the group
                cmd.AddStatement();
                cmd.AddCommand("Add-MailboxFolderPermission");
                cmd.AddParameter("Identity", string.Format(@"{0}:\{1}", userPrincipalName, calendarName));
                cmd.AddParameter("User", "[email protected]" + companyCode);
                cmd.AddParameter("AccessRights", "AvailabilityOnly");
                cmd.AddParameter("DomainController", this.domainController);

                powershell.Commands = cmd;
                powershell.Invoke();

                // Log the powershell commands
                LogPowershellCommands(ref powershell);

                // Find all the error
                if (powershell.HadErrors)
                {
                    // Log all warning detected
                    foreach (WarningRecord warn in powershell.Streams.Warning)
                    {
                        string warnMessage = warn.Message;
                        if (!warnMessage.Contains("completed successfully but no permissions"))
                            logger.Warn("Warning was generated running the command to modify the mailbox permissions for " + userPrincipalName + ": " + warnMessage);
                    }

                    // Log all errors detected
                    foreach (ErrorRecord err in powershell.Streams.Error)
                    {
                        string exception = err.Exception.ToString();
                        if (exception.Contains("An existing permission entry was found for user"))
                            logger.Info("Attempted to modify permission on " + userPrincipalName + " but the permission already existed.");
                        else
                            throw err.Exception;
                    }
                }

                // Stop the clock
                stopwatch.Stop();

                // Log the success
                logger.Info("Successfully modified calendar permissions for " + userPrincipalName);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (powershell != null)
                    powershell.Dispose();
            }
        }
开发者ID:KnowMoreIT,项目名称:CloudPanel_3.0,代码行数:89,代码来源:ExchPowershell.cs


注:本文中的System.Management.Automation.PSCommand.AddStatement方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。