i am the funny
 

 I AM THE FUNNY!!!

DFS!!! YAY!!! Not...
Anyway, this script was born because we migrated some old servers to new servers and it broke the DFS folder target link. Yes, this would be fixed if you added in the server CNAME, but that is just so messy, and really it is better to clean it up straight away.

So, this script does two things:

  1. Search for broken path links - prints to screen and exports to CSV
  2. Search for a name in the target - prints to screen
Now, when I say that it searches for a name in the target, it is literally just looking for that text. E.g. if you search for a server called Toowoomba, and there is a folder called Toowoomba, it will return the result for the folder as well. Just try to make it unique.

Also, it caches the info because it does take a while to gather it. As such, I included a way to refresh the data in case you just leave it running, or in case you make some changes and want to check it. Ok, with all that said, have fun...

#enter the area you want to search here (star is a wildcard)
$StartRoot = "\\lands\data\*"
 
$TargetArray = @()

cls

Function Show-Menu([string]$Title){
    cls
    Write-Host "================ $Title ================"

    Write-Host "1: Press '1' Find Broken Links."
    Write-Host "2: Press '2' Search Targets for a server."
    Write-Host "3: Press '3' Refresh Folder Link Data."
    Write-Host "3: Press '4' Refresh Folder Target Data."
    Write-Host "Q: Press 'Q' to quit."
}

Function GetDFSLinks{
    $Result = Get-DfsnFolder -Path $StartRoot
    return $Result
}

Function Deeper($Root){
    $Result = @()
    Foreach ($Folder in $Root){
        $myobjCheckem = "" | Select Path, Exists, Target, Description
        write-host $Folder.Path  " - " -NoNewline
        [string]$Exists = Test-Path $Folder.Path
        If($Exists -eq "TRUE"){
            Write-Host $Exists -ForegroundColor Green
        }Else{
            Write-Host $Exists -ForegroundColor Red
            $myobjCheckem.Path = $Folder.Path
            $myobjCheckem.Exists = $Exists
            $myobjCheckem.Target = (Get-DfsnFolderTarget -Path $Folder.Path).TargetPath
            $myobjCheckem.Description = $Folder.Description
            $Result += $myobjCheckem
        }
    }
    return $Result
}

Function GiveMeTargets($Root, $count){
    $Result = @()
    $i = 0
    Foreach ($Folder in $Root){
        $i++
        Write-Progress -Activity Collecting Target Details of $($Folder.path) -status Target $i of $count -percentComplete ($i / $count*100)
        $Target = Get-DfsnFolderTarget -Path $Folder.Path
        $myobjCheckem = "" | Select Path, Target, State, Description
        write-host $Folder.Path  " - " -NoNewline
        Write-Host $Target.TargetPath -ForegroundColor Cyan
        $myobjCheckem.Path = $Folder.Path
        $myobjCheckem.Target = $Target.TargetPath
        $myobjCheckem.State = $Target.State
        $myobjCheckem.Description = $Folder.Description
        $Result += $myobjCheckem
    }
    Write-Progress -Activity Collecting Target Details of $($Folder.path) -status Target $i of $count -percentComplete ($i / $count*100) -Completed
    return $Result
}

Write-host "Getting DFS Folder Link Data..."
$DFSRoot = GetDFSLinks | sort-object Path 

$TotalPaths = $DFSRoot.count

Write-host "Getting DFS Folder Target Data..."
$TargetArray = GiveMeTargets $DFSRoot $TotalPaths

do{
    Show-Menu "Choose your DFS Task"2
    $input = Read-Host "Please make a selection"
    Switch ($input) {
        "1" {
            $ExistResult = @()
            write-host $DFSRoot.Count

            $ExistResult = Deeper($DFSRoot)

            $ExistResult | export-csv C:\TEMP\DFS_BrokenLinks.csv

            $myformat = @{Expression={$_.Path};Label="DFS Path";width=90},@{Expression={$_.Exists};Label="Exists";width=10},@{Expression={$_.Target};Label="Target Folder";width=80},@{Expression={$_.Description};Label="Description";width=60}
            $ExistResult = $ExistResult | Format-Table $myformat -Wrap | Out-String

            Write-host
            Write-Host "#########################################" -ForegroundColor Yellow
            Write-host "################ Results ################" -ForegroundColor Yellow
            write-host $ExistResult
            Pause
        }
        "2" {
            Cls
            $FilteredTargets = @()
            $ServerSearch = Read-Host "Please enter the name of the server you wish to search"
            ForEach($Targeted in $TargetArray){
                write-host $Targeted.Target
                If($Targeted.Target -match $ServerSearch){
                    write-host $Targeted.Target -ForegroundColor Green
                    $FilteredTargets += $Targeted
                }
            }
            $myformattarget = @{Expression={$_.Path};Label="DFS Path";width=90},@{Expression={$_.Target};Label="Target Folder";width=80},@{Expression={$_.State};Label="State";width=10},@{Expression={$_.Description};Label="Description";width=60}
            $FilteredTargets = $FilteredTargets | Format-Table $myformattarget -Wrap | Out-String
            Cls
            Write-Host "All results for $ServerSearch"
            Write-host $FilteredTargets 
            Pause
        }
        "3" {
            Cls
            Write-host "Updating DFS Folder Link Data..."
            $DFSRoot = GetDFSLinks | sort-object Path 
            $TotalPaths = $DFSRoot.count
        }
        "4" {
            Cls
            $TargetArray = @()
            Write-host "Updating DFS Folder Target Data..."
            $TargetArray = GiveMeTargets $DFSRoot $TotalPaths
        }
        # You don't really need to add an option for q here... but i wanted to
        "q" {
            Write-host "Quitting now"
            sleep -s 2
        }
    }
}until ($input -eq 'q')