本文整理汇总了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();
}
}