2

How can I combine these PowerShell cmdlets instead of running them separately? These commands are ran on our Exchange server for on premise and 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

In the picture shown is what the PowerShell script is automating:

O365 screenshot

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Mykal
  • 21
  • 2
  • What do you mean "combine"? – Ansgar Wiechers Jul 21 '16 at 17:39
  • Combine as in put all scripts into one body of text so when i copy and paste it into powershell it will execute. The scripts are working because I have test them, but I have to copy and paste them one by one. I want to be able to copy all of them at once and paste it all then execute. – Mykal Jul 21 '16 at 18:40
  • I don't know what characters or parameters to put between the scripts so that they'll execute without errors. – Mykal Jul 21 '16 at 18:42
  • 1
    What happens if you copy all the commands in a .ps1 file and run it ? Do you get any errors ? – sodawillow Jul 21 '16 at 18:57
  • Normally you just put one after the other, each in a separate line. Do you get errors when you do that? If so, what do they say? – Ansgar Wiechers Jul 21 '16 at 21:16

1 Answers1

0

You could create a function like below and throw it into a .psm1 and it import it as a module or into a .ps1 and dot source it to your session. Then you can call this function like I have also shown below. Mind you that you will probably want to add some error handling.

 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
     }
 }

And Then Call it like this:

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

However I Would be careful with this call because you are not specifying what to get. A lot of powershell cmdlets will just return everything and the net effect will be that you update all of them.

 # Membership approval
 Get-DistributionGroup | Set-DistributionGroup -MemberJoinRestriction:closed -MemberDepartRestriction:closed
jkdba
  • 2,378
  • 3
  • 23
  • 33