mercredi 21 septembre 2016

Deploying an High-Availability Domain-Based DFS Namespace with Powershell on Windows Server 2012 R2

As I'm often doing some tests in my lab environment, I try to automate as many things as possible.

So here's a little Powershell script that can be used to deploy an High Availability Domain-Based DFS Namespace on a Windows Server 2012 R2. The UI result can be seen below.




In this lab's case I've been using the following servers to create the root namespace:

  • lab-dc001: This is the Windows Server 2012 R2 Domain Controller
  • lab-dfs001: This is the first DFS Server
  • lab-dfs002: This is the second DFS Server

Both DFS servers are members of my katalykt.lan domain.

Note that the script can be executed on any of our DFS servers and that we rely on PS/CIM sessions to execute some remote commands (i.e Folders creation, SMB Shares)

# Parameters

$DFS_Root = "lab-dfs001"

$DFS_Secondaries = "lab-dfs002"

$DFS_Domain = "katalykt.lan"

$DFS_Namespace = "root"

$DFS_SmbPath = "E:\DFS\root"

$DFS_Servers = $DFS_Root, $DFS_Secondaries

# Install the DFS Namespace & Replication features

$DFS_Servers | ForEach-Object {Install-WindowsFeature -ComputerName $_ -Name FS-DFS-Namespace,FS-DFS-Replication -IncludeManagementTools}

# Create the Local Folders

$Sessions_DFS = New-PSSession -ComputerName $DFS_Servers

Invoke-Command -Session $Sessions_DFS -Script { New-Item $args[0] –Type Directory } -ArgumentList $DFS_SmbPath

$Sessions_DFS | Remove-PSSession

# Create the Local Shares

$CIMs_DFS = New-CimSession -ComputerName $DFS_Servers

New-SmbShare –Name $DFS_Namespace –Path $DFS_SmbPath -CimSession $CIMs_DFS

$CIMs_DFS | Remove-CimSession

# Create the main DFS Namespace root

New-DfsnRoot -TargetPath ("\\{0}\{1}" -f $DFS_Root, $DFS_Namespace) -Type DomainV2 -Path ("\\{0}\{1}" -f $DFS_Domain, $DFS_Namespace)

# Add additional targets to our DFS Namespace

$DFS_Secondaries | ForEach-Object {

    New-DfsnRootTarget -Path ("\\{0}\{1}" -f $DFS_Domain, $DFS_Namespace) -TargetPath ("\\{0}\{1}" -f $_, $DFS_Namespace)

}