Posts

Showing posts from November, 2016

Powershell Custom Object Formatting

Image
Today I would like to share some guidelines on how to apply formatting to custom powershell objects. There are many times when writing advanced functions, cmdlets and modules where a custom object is very useful, so let's start by creating an object type for our custom objects.

Creating a type is very simple, you just define the type as you would in C# and then add it:

add-type@'namespace TestModule.CustomObject{ public class ExchangeServerMaintenanceMode { public string Name; public bool Enabled; public int ID; }}'@
I should also add here that Powershell 5 makes it even easier by allowing us to create classes directly!

Now that we have added the type of our custom object, lets create some objects!

$myuser = New-Object TestModule.CustomObject $myuser.Id = "1"$myuser.Name = "User1"$myuser.Enabled = $false
Here, we have created a new object of type TestModule.CustomObject and we have stored in the "myuser" variable. T…

Exchange Shell Colors

Image
You may have noticed that the output of the Write-Verbose - and possibly the color of other "Write-" commands - may change when using standard powershell, exchange shell or other flavors of powershell compared to the Powershell ISE.

I'm a very big fan of ISE so I wanted to change the colors in order to match the ones in ISE.

You can see the colors that are configured on your powershell window using the command

(Get-Host).PrivateData
You'll get something like this


Now that we have a way to see the colors that are configured, what about a way to change them?

First, we'll save the output of the above command to a variable:

$colors = (Get-Host).PrivateData
We are now able to update the color we want by just setting the proper property of the variable:

$colors.VerboseForegroundColor = "Cyan"
Put that in your $profile file and you won't have to worry about colors again!

Exchange Server Relay Receive Connector

Microsoft Exchange servers do not allow SMTP relay by default so you have to configure it yourself.

But, before you allow relaying, make sure this is want you need.

Let's go over a simple design for relaying.

First of all, you have to create new receive connectors with the appropriate permissions. Make sure the "Anonymous" access is enabled and that the servers that need to relay are configured on the Remote IP Ranges. This way, only these servers will be able to relay.

I also create A records in the DNS servers that point to the servers with the connectors and set the record's name as the HELO/EHLO response of the connectors.

The following commands will extend the receive connectors in order to accept messages for any sender and accept any sender:

Get-ReceiveConnector "Relay Exchange 1" |     Add-ADPermission -User 'NT AUTHORITY\Anonymous Logon' -ExtendedRights MS-Exch-SMTP-Accept-Any-Recipient
Get-ReceiveConnector "Relay Exchange 1" |  …

Exchange Server Recipient Type Values for MSExchRecipientTypeDetails Active Directory Attribute

There are many times where I need to get the type of an exchange recipient but all I have is Active Directory access and not Exchange. A typical scenario is during a migration where you may only have AD access for MIM to sync the directories.

How are we going to get the exchange recipient with only AD access? Using the MSExchRecipientTypeDetails Active Directory attribute. Below is the table with the possible values for that attribute.

Object Type

RecipientTypeDetails

Value Name

User Mailbox

1

UserMailbox

Linked Mailbox

2

LinkedMailbox

Shared Mailbox

4

SharedMailbox

Legacy Mailbox

8

LegacyMailbox

Room Mailbox

16

RoomMailbox

Equipment Mailbox

32

EquipmentMailbox

Mail Contact

64

MailContact

Mail User

128

MailUser

Mail-Enabled Universal Distribution Group

256

MailUniversalDistributionGroup

Mail-Enabled Non-Universal Distribution Group

512

MailNonUniversalGroup

Mail-Enabled Universal Security Group

1024

MailUniversalSecurityGroup

Dynamic Distribution Group

2048

DynamicDistributionGroup

Public Folder

4096

Public Folder

System Atten…

Verbose Parameter Passing to cmdlet inside Function

Well, almost all powershell cmdlets, functions and workflows have a Verbose parameter that displays more information about what the command is doing during it's execution. But how are we going to take advantage of this parameter when building our own functions and scripts?

First of all, we have to include the "cmdletBinding()] statement. This will add the -Verbose switch to the function/script and when we call the function with that script the output from the command "Write-Verbose" will be displayed.

Take the following function for example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14Function Test { [cmdletBinding()] Param ( ) Write-Verbose"Started"Start-Sleep -Seconds 5 Write-Verbose"Completed." }
All it does is sleep for five seconds. If we call it without the -Verbose switch it will display nothing. With the -Verbose switch it will display the "Started" message before sleeping and "Completed" afterw…