Combining Multiple Shares into One DFS Folder (With PowerShell)
I had the need to combine multiple Windows shares into one DFS Folder / Location. Anyone that uses DFS knows that you can either create links to the top level share or its sub folder and that there is no way to automatically combines all the folders together
Creating links to all the sub folders is fine if you have 10 or 20, but what if you have 350 links to create. Such as in my case, I had the need to create links to all the “Users” folders for every one of my departments.
Doing this would allow me to point all my scripts / group policies to one single location, without putting all the user folders on one server.
Here is the script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<# User Home Script This cript will scan all the file server shares specified in the "UserShare" array It will then create a link for every folder in that share under the designated DFS path > $UserShare = "\\winsrv1\Users","\\winsrv2\Users","\\winsrv3\Users" $DFSRoot = "\\pinchii.com\data" $DFSPath = "$DFSRoot\userhome" function addUsers { foreach ($share in $UserShare) { #Echo $share Get-ChildItem $share | where {$_.psiscontainer} | Select-Object $_.Name | foreach {If (Test-Path $DFSpath\$_) {Echo "Skipping $_. Already Exists"} Else {Echo "Adding $_ From $Share"; dfsutil link add $DFSpath\$_ $share\$_}} } } function purgeUsers { #[xml]$xml = Get-Content c:\root.xml dfsutil root export \\ps.lan\data $evn:temp\root.xml [xml]$xml = Get-Content $evn:temp\root.xml foreach ($link in $xml.Root.Link) { #$Link.Name #$Link.Target.State #$Link.Target.'#Text' If ($Link.Target."#Text".contains("\\null") -eq $false) { If (Test-Path $Link.Target."#Text") { Echo "Skipping $($Link.Name). Target Is Good. $DFSPath" } Else { Echo "Removing $($Link.Target.'#Text') From $DFSpath"; dfsutil link remove $DFSRoot\$($Link.Name) } } } } #this is past the functions, now we process command line arguments of the script and use those to know which process to run $a = $args[0] #check if argument is null and assign a blank string if it is if (!$a) {$a = ""} switch ($a.ToLower()) { "/addusers" {addUsers} "/purgeusers" {purgeUsers} default {Echo "`r`nInvalid Argument.`n================`n `nIt must be one of the following: `n/addusers `n/purgeusers`n"} } |
That’s it. This script will scan the shares, take every sub folder and add a link to the sub folder under the DFS folder I designated. For my purpose, I used it to combine user folders, and its been working great in production for months now.
You can use this script to combine any folder, except for 1 catch. Last I checked, DFS has a limit of 5000 links per “name space”, so if you are worried about approaching that limitation, you can always create a separate namespace specifically for the user folders or whatever other folders you are planning to combine.