As my first blog post I would like to talk about scripting in Exchange 2013, lets talk about Creating the DAG.

The below is the simple function to create the DAG, but lets talk about what permissions you will need to create the DAG for the Org.


You need to be part of the below role groups if you would like to run the creating of DAG:

  • Organization Management management role group
  • Database Availability Groups management role

------------------------------------------------------------------------------------------------------------------------------------------

function CreateDAG {

$DagName = Read-Host "Enter the name for the DAG:"
$DAGip = Read-Host "Enter the ip/ips for the DAG:"
$DAGDir = "D:\MNS_FSW\$Dagname"

#If Multiple ips below variable will be used
$IPSplit = $DAGip.Split(',;')

$Search = New-Object DirectoryServices.DirectorySearcher([ADSI]“”)

$Search.filter = “(&(objectClass=Computer)(name=$DagName))”
$A=$Search.Findall()

if ($A[0].path -ilike "*$DagName*")
{
Write-Host "DAG name $DagName Already Exist in AD, select a DAG Name that does not exist in AD and re-run the script"
}
Else
{
Write-Host "Creating DAG $Dagname ........"

$New = New-DatabaseAvailabilityGroup -Name $DAGName -WitnessDirectory $DAGDir
$HT = $new.witnessserver.hostname


Write-Host "Fileshare witness is created on HubTransport Server $HT and Fileshare Witness located at $DAGDir"
Write-Host "Sleeping for 20 seconds to replicate"
start-sleep -seconds 20
if ($New.name -ilike "$DagName")
{
Write-Host "$DagName is created successfully, Setting up the Static Ip addresses and You can then add members to it by running AddtoDAG"

#Condition to check if its single or multiple ips
 if ($Dagip -ilike "*,*")
 {
   Set-DatabaseAvailabilityGroup -Identity $Dagname -DatabaseAvailabilityGroupIpAddresses ($IPSplit[0]),($IPSplit[1])
  if ($? -eq $False){$Status = "FAILED"}
  else{$Status = "SUCCESS"}
  write-Host "$Status - Set ip address $DAGip to DAG $Dagname"
  if ($Status -eq "FAILED"){writelog $error[0]}
  start-sleep -seconds 15
  Get-DatabaseAvailabilityGroup $Dagname |fl
 }
 Else
 {
  Set-DatabaseAvailabilityGroup -Identity $Dagname -DatabaseAvailabilityGroupIpAddresses $DAGip

  if ($? -eq $False){$Status = "FAILED"}
  else{$Status = "SUCCESS"}
  write-Host "$Status - Set ip address $DAGip to DAG $Dagname"
  if ($Status -eq "FAILED"){writelog $error[0]}
  start-sleep -seconds 30
  Get-DatabaseAvailabilityGroup $Dagname |fl
 }
}
Else
{
Write-Host "$Dagname failed to create either it is already existing or failed for some reason, try again!"
}
}
}

CreateDAG

------------------------------------------------------------------------------------------------------------------------------------------

Copy the above function to a notepad and save it as CreateDag.ps1 and run the script .\CreateDAG.PS1 and it would ask for the DAG name and IPs and then create the DAG.

My Next posts would be adding servers to DAG, Creating Databases and the Database copies.