- Working with Pipelines
- Running Programs
- Creating and Running Scripts
- Registry Modifications with PowerShell
- Understanding Quotes
Creating and Running Scripts
In most cases, you will run individual cmdlets to achieve your desired result, and with the use of pipelining you can combine cmdlets together to allow for creative results. However, just like from a Windows command prompt, a single line of code may not be able to provide you with the ability to do everything you desire. From the Windows command prompt, it is common to use a .bat file to group multiple commands into a single file, but that will not work in Exchange Management Shell. But, do not fear. PowerShell offers support for a scripting language, based on the Microsoft .NET Framework, and those scripts can be used to automate tasks or run multiple cmdlets together when pipelining is not appropriate. The Shell lets you create scripts, assign variables in the scripts, perform looping operations, and use conditional logic in the scripts. This is all done within a text file using PowerShell cmdlets. When you save the text file with a .ps1 extension, it is executable from Exchange Management Shell.
By creating your own library of these .ps1 scripts, you can automate tasks and efficiently run your scripts on any computer that has Exchange Management Shell installed on it. The following example illustrates how to create a script to perform the five steps necessary to configure Messaging Records Management.
Create a text file with a .ps1 extension. In the example that follows, the file will be called C:\Users\Administrator\MRM_Retention.ps1. |
Five steps are involved in configuring Messaging Records Management. This is commonly configured from Exchange Management Console (prior to Service Pack 1) because each step is performed in a unique place and no single cmdlet will perform all five steps. However, in the Console, there is no single wizard that will perform all five steps either. Therefore, you want to create a script to perform all five steps together in a PowerShell script. |
New-ManagedFolder -Name "ObjectName" -FolderName "FolderName" PS C:\Users\Administrator> New-ManagedFolder -Name "Retention" -FolderName "Retention" |
Step 1: Create a new managed custom folder. |
$AgeLimit = New-TimeSpan -Day ValueInDays PS C:\Users\Administrator> $AgeLimit = New-TimeSpan -Day 1100 |
Create a variable to use in step 2. |
New-ManagedContentSettings -Name "ContentSettingsName" -FolderName "FolderName" -MessageClass "MessageClassType" -RetentionEnabled $boolean value -AgeLimitForRetention $AgeLimit -RetentionAction RetentionActionType PS C:\Users\Administrator> New-ManagedContentSettings -Name "Retention Settings for Retention Folder" -FolderName "Retention" -MessageClass * -RetentionEnabled: $true -AgeLimitForRetention $AgeLimit -RetentionAction PermanentlyDelete |
Step 2: Create managed content settings for the 3 Year Retention Folder, which permanently deletes all items after 3 years or 1,100 days. |
New-ManagedFolderMailboxPolicy -Name "PolicyName" -ManagedFolderLinks "Folder(s)ToBeLinked ToPolicy" PS C:\Users\Administrator>New- ManagedFolderMailboxPolicy -Name "Executives Mailbox Policy" -ManagedFolderLinks "Retention" |
Step 3: Create a managed folder mailbox policy. |
Set-Mailbox -Identity MailboxUsername -ManagedFolderMailboxPolicy "PolicyName" PS C:\Users\Administrator>Set-Mailbox -Identity Administrator -ManagedFolderMailboxPolicy "Executives Mailbox Policy" |
Step 4: Apply the managed folder mailbox policy to a mailbox. |
PS C:\Users\Administrator>$ServerName = cmd /c echo %computername% PS C:\Users\Administrator>Set- MailboxServer -ID $ServerName -ManagedFolderAssistantSchedule "Schedule" PS C:\Users\Administrator>Set- MailboxServer -ID $ServerName -ManagedFolderAssistantSchedule "Sun.12:00-Sun.11:00" |
Step 5: Schedule the Managed Folder Assistant to run each day. |
PS C:\Users\Administrator> Start-ManagedFolderAssistant |
Alternate step 5: Start the Managed Folder Assistant manually. |
# Step 1: Create a new managed custom folder. New-ManagedFolder -Name "Retention" -FolderName "Retention" # Create a variable, "$AgeLimit," to use in Step Number 2. $AgeLimit = New-TimeSpan -Day 1100 # Step 2: Create managed content settings for the 3 Year Retention Folder that permanently deletes all items after 3 years or 1100 days. New-ManagedContentSettings -Name "Retention Settings for Retention Folder" -FolderName "Retention" -MessageClass * -RetentionEnabled:$true -AgeLimitForRetention $AgeLimit -RetentionAction PermanentlyDelete # Step 3: Create a managed folder mailbox policy. New-ManagedFolderMailboxPolicy -Name "Executives Mailbox Policy" -ManagedFolderLinks "Retention" # Step 4: Apply the managed folder mailbox policy to a mailbox. Set-Mailbox -Identity Administrator -ManagedFolderMailboxPolicy "Executives Mailbox Policy" # Step 5: Start the Managed Folder Assistant manually. Start-ManagedFolderAssistant |
The completed script with all steps combined in a file. The # sign indicates a remark, and that line will not be executed. Save this file with a name such as C:\Users\Administrator>MRM_Retention.ps1. |
PS C:\Users\Administrator>.\ MRM_Retention.ps1 |
Run the script as shown in Figure 3-5. |
Figure 3-5 Running the MRM_Retention.ps1 script created in the example
Figures 3-6, 3-7, 3-8, and 3-9 show the results after the script has been run.
Figure 3-6 Custom folder and content settings on a folder viewed from the Exchange Management Console
Figure 3-7 Managed folder mailbox policy viewed from the Exchange Management Console
Figure 3-8 Executives mailbox policy linked to a user, as viewed from the Exchange Management Console
Figure 3-9 Retention folder viewed on user's Outlook Web App client