2

如何组合这些 PowerShell cmdlet 而不是单独运行它们?这些命令在我们的 Exchange 服务器上运行,用于内部部署和 Office 365。

# General
New-DistributionGroup -Name dis_its3 -DisplayName dis_its3 -Alias dis_its3 -PrimarySmtpAddress dis_its3@jackson.k12.ms.us 

# Ownership
Set-DistributionGroup -Identity "dis_its3" –ManagedBy Robinson Mykal -BypassSecurityGroupManagerCheck 

# Membership approval
Get-DistributionGroup | Set-DistributionGroup -MemberJoinRestriction:closed -MemberDepartRestriction:closed 

# Delivery Management
Set-DistributionGroup "dis_its3" -RequireSenderAuthenticationEnabled $False

图中显示的是 PowerShell 脚本正在自动化的内容:

O365 截图

4

1 回答 1

0

您可以创建如下所示的函数并将其放入 .psm1 中,然后将其作为模块导入或导入 .ps1 并将其点源到您的会话中。然后你可以像我在下面显示的那样调用这个函数。请注意,您可能需要添加一些错误处理。

 function Set-CustomDistributionGroup
 {
     [CmdletBinding()]
     param
     (
         [Parameter(
             Position = 0,
             Mandatory = $true
         )]
         [ValidateNotNullorEmpty()]
         [Alias('DistributionGroupName')]
         [String] $Name,

         [Parameter(
             Position = 1,
             Mandatory = $true
         )]
         [ValidateNotNullorEmpty()]
         [Alias('DistributionGroupDisplayName')]
         [String] $DisplayName,

         [Parameter(
             Position = 2,
             Mandatory = $true
         )]
         [ValidateNotNullorEmpty()]
         [Alias('DistributionGroupAlias')]
         [String] $Alias,

         [Parameter(
             Position = 3,
             Mandatory = $true
         )]
         [ValidateNotNullorEmpty()]
         [Alias('DistributionGroupPrimarySmtpAddress')]
         [String] $PrimarySmtpAddress,

         [Parameter(
             Position = 4,
             Mandatory = $true
         )]
         [ValidateNotNullorEmpty()]
         [Alias('DistributionGroupManagedBy')]
         [String] $ManagedBy        
     )
     process
     {
         # General
         New-DistributionGroup -Name $Name -DisplayName $DisplayName -Alias $Alias -PrimarySmtpAddress $PrimarySmtpAddress

         # Ownership
         Set-DistributionGroup -Identity $Name –ManagedBy $ManagedBy -BypassSecurityGroupManagerCheck 

         # Membership approval
         Get-DistributionGroup | Set-DistributionGroup -MemberJoinRestriction:closed -MemberDepartRestriction:closed 

         # Delivery Management
         Set-DistributionGroup -Identity $Name -RequireSenderAuthenticationEnabled $False
     }
 }

然后像这样调用它:

 Set-CustomDistributionGroup -Name 'dis_its3' -DisplayName 'dis_its3' -Alias 'dis_its3' -PrimarySmtpAddress 'jackson.k12.ms.us' -ManagedBy 'Robinson Mykal'

但是我会小心这个电话,因为你没有指定要得到什么。许多 powershell cmdlet 只会返回所有内容,最终效果是您更新所有这些。

 # Membership approval
 Get-DistributionGroup | Set-DistributionGroup -MemberJoinRestriction:closed -MemberDepartRestriction:closed
于 2016-07-21T19:35:48.513 回答