Using PowerShell to Set DFS Override Referral Ordering

Introduction

As part of an Active(Up)/Active(Down) DFS Failover Mechanism, we need to set our Folder Targets so that the Active(Up) target has an ‘Override referral ordering: Target priority: First among all targets’, and the Active(Down) target has an ‘Override referral ordering: Target priority: Last among all targets’. Then for DR, we simply switch them. And here is a simple script that does exactly what we want!

Note: The DFS cmdlets used are only available in Windows Server 2012+, but can be used to manage Windows Server 2008R2 DFS.

The Script

#############################
## .\Set-DfsnOverrides.ps1 ##
#############################

Function FolderTargets{
$output = Get-DfsnFolderTarget -path $folders[0].Path
If($folders.count -eq 1){return $output}
$folders[1..($folders.count -1)] | foreach{$output += Get-DfsnFolderTarget -path $_.Path}
return $output}

"";"DFSN Override Referral Ordering";"===============================";"";"DFS Namespaces:"
Get-DfsnRoot | Format-Table -Autosize
$namespace  = Read-Host "Enter the DFS Namespace"
$namespace += "\*"
$folders = Get-DfsnFolder -path $namespace
If($folders.count -eq 0){"`nNo folders in this namespace!`n";exit}
$folderTargets = FolderTargets
$folderTargets | Format-Table -Autosize
$serverFirst = Read-Host "Enter the server NETBIOS\FQDN to be set as 'First among all targets'"
$serverLast  = Read-Host "Enter the server NETBIOS\FQDN to be set as 'Last among all targets' "

$folderTargets | foreach {
If (($_.TargetPath).contains($serverFirst)){
[Void](Set-DfsnFolderTarget -Path $_.Path -TargetPath $_.TargetPath -ReferralPriorityClass GlobalHigh)}
If (($_.TargetPath).contains($serverLast)){    
[Void](Set-DfsnFolderTarget -Path $_.Path -TargetPath $_.TargetPath -ReferralPriorityClass GlobalLow)}}

"";"Result:";"======="
FolderTargets | Format-Table -Autosize

An Example

PS C:\Users\administrator> .\Set-DfsnOverrides.ps1

DFSN Override Referral Ordering
===============================

DFS Namespaces:

Path               Type      Properties   TimeToLiveSec State  Description
----               ----      ----------   ------------- -----  -----------
\\lab.priv\TEST001 Domain V2 Site Costing 300           Online
\\lab.priv\TEST002 Domain V2 Site Costing 300           Online
\\lab.priv\TEST003 Domain V2 Site Costing 300           Online

Enter the DFS Namespace: \\lab.priv\TEST001

Path                  TargetPath        State  ReferralPriorityClass ReferralPriorityRank
----                  ----------        -----  --------------------- --------------------
\\LAB\TEST001\SHARE01 \\NACLU1\share01$ Online global-high           0
\\LAB\TEST001\SHARE01 \\NACLU2\share01$ Online global-low            0
\\LAB\TEST001\SHARE02 \\NACLU1\share02$ Online global-high           0
\\LAB\TEST001\SHARE02 \\NACLU2\share02$ Online global-low            0
\\LAB\TEST001\SHARE03 \\NACLU1\share03$ Online global-high           0
\\LAB\TEST001\SHARE03 \\NACLU2\share03$ Online global-low            0

Enter the server NETBIOS\FQDN to be set as 'First among all targets': NACLU2
Enter the server NETBIOS\FQDN to be set as 'Last among all targets' : NACLU1

Result:
=======

Path                  TargetPath        State  ReferralPriorityClass ReferralPriorityRank
----                  ----------        -----  --------------------- --------------------
\\LAB\TEST001\SHARE01 \\NACLU1\share01$ Online global-low            0
\\LAB\TEST001\SHARE01 \\NACLU2\share01$ Online global-high           0
\\LAB\TEST001\SHARE02 \\NACLU1\share02$ Online global-low            0
\\LAB\TEST001\SHARE02 \\NACLU2\share02$ Online global-high           0
\\LAB\TEST001\SHARE03 \\NACLU1\share03$ Online global-low            0
\\LAB\TEST001\SHARE03 \\NACLU2\share03$ Online global-high           0

PS C:\Users\administrator>

Comments