How to Exchange Prepare Schema with PowerShell DSC
During some automatized deployments, as often, we have to deploy Microsoft Exchange 2016 automatically.
The well-known module https://www.powershellgallery.com/packages/xExchange/ does many things, but no Exchange preparation on a fresh AD.
Here is a simple DSC Code which
- checks if AD schema is at the right version
- if not, launch Exchange setup with the correct parameters
Code
Import-Module $PSScriptRoot\..\myAdHelper.psm1 -Verbose:$false
function Get-TargetResource
{
param
(
[System.String]$ExchSchemaVersion,
[parameter(Mandatory = $true)]
[System.String]$Path,
[parameter(Mandatory = $true)]
[System.String]$Path,
Import-Module $PSScriptRoot\..\myAdHelper.psm1 -Verbose:$false
function Get-TargetResource
{
param
(
[System.String]$ExchSchemaVersion,
[parameter(Mandatory = $true)]
[System.String]$Path,
[parameter(Mandatory = $true)]
[System.String]$Arguments,
[System.Management.Automation.PSCredential]$Credential
)
$Schema = ReplacePartitionTokens -Identity "%%schema%%" -Credential $Credential
myGetAdObject -Identity "CN=ms-Exch-Schema-Version-Pt,$Schema" -Credential $Credential -Properties @('rangeUpper')
}
function Set-TargetResource
{
[CmdletBinding()]
param
(
[System.String]$ExchSchemaVersion,
[parameter(Mandatory = $true)]
[System.String]$Path,
[parameter(Mandatory = $true)]
[System.String]$Arguments,
[System.Management.Automation.PSCredential]$Credential
)
StartAndWaitWaitForProcessEnd -Path $Path -Arguments $Arguments -Credential $Credential -Delay 7200 -Verbose -TaskName 'EXCHANGE - PrepareSchema' -ProcessToWaitFor 'ExSetup*'
}
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[System.String]$ExchSchemaVersion,
[parameter(Mandatory = $true)]
[System.String]$Path,
[parameter(Mandatory = $true)]
[System.String]$Arguments,
[System.Management.Automation.PSCredential]$Credential
)
$Result = $true
$Version = Get-TargetResource @PSBoundParameters
if ($Version)
{
if ([uint32]$Version.rangeUpper -lt [uint32]$ExchSchemaVersion)
{
$Result = $false
}
}
else
{
$Result = $false
}
$result
}
Export-ModuleMember -Function *-TargetResource
Module Call
myAdExchPrepareSchema ExchangePrepareSchema
{
ExchSchemaVersion = '15317' #CU3 15326
Path = "$($ConfigurationData.Binaries.Exchange2016)\Setup.exe"
Arguments = '/PrepareSchema /IAcceptExchangeServerLicenseTerms'
Credential = $DscCredentials
DependsOn = @('[xPendingReboot]BeforeExchangePreparation')
}