Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active July 3, 2023 14:57
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save markwragg/0900550dc3951c2f44433f6f9225f7aa to your computer and use it in GitHub Desktop.
Save markwragg/0900550dc3951c2f44433f6f9225f7aa to your computer and use it in GitHub Desktop.
Powershell script to get a list of DFS folder targets for all DFS folders under a defined path and test if those paths are valid from the location running the script.
$Servers = @("SERVER01","SERVER02","SERVER03")
$FolderPaths = $Servers | foreach {
Get-ChildItem "\\$_\DFSShare$"
} | Sort Path
$FolderPaths | Export-Csv "FolderPaths-$(Get-Date -format yyyy-MM-dd).csv" -NoTypeInformation
$TestPaths = (($FolderPaths).FullName | Sort-Object).Trimend('\')
$DFSPaths = ((Import-CSV "DFS-$(Get-Date -format yyyy-MM-dd).csv").TargetPath | Where-Object {($_ -ilike "*SERVER*") | Sort-Object).Trimend('\')
$TestPaths | Where-Object {$DFSPaths -notcontains $_}
[CmdletBinding()]
param(
$DFSPath #Example:= '\\example.com\folder\*\whatever'
)
Write-Progress "Getting all DFS folders for $DFSPath (this can take a very long time)" -PercentComplete -1
$DFSTree = Get-DfsnFolder $DFSPath
$i = 1
$DFSTree | ForEach-Object{
Write-Progress "Getting DFS Folder Targets for $($_.Path)" -PercentComplete (($i / $DFSTree.Count) *100)
$DFSTarget = Get-DfsnFolderTarget $_.Path | Select Path,TargetPath,State
$Result = [ordered]@{
Path = $DFSTarget.Path
TargetPath = $DFSTarget.TargetPath
State = $DFSTarget.State
"ValidFrom_$Env:ComputerName" = Test-Path $DFSTarget.Path
}
New-Object PSObject -Property $Result
$i++
} | Sort Path | Export-Csv "DFS-$(Get-Date -format yyyy-MM-dd).csv" -NoTypeInformation
@shackdan
Copy link

Perhaps you don't have access to the DFS namespace.
You can verify by simply running "Get-DfsnRoot -domain [domain.com]"

@cah-yoga-damalachervu
Copy link

@JoeAlvis81
For some reason i get empty cells as output shown below:

#TYPE Selected.System.Management.Automation.PSCustomObject    
Path TargetPath State
    Online
    Online
    Online
    Online
    Online
    Online
    Online
    Online
    Online

@cah-yoga-damalachervu
Copy link

This one worked
$namespaces = '\domainns\x*'
foreach ($ns in $namespaces)
{
$DFSNFolders = Get-DfsnFolder -path $ns
foreach($DFSNFolder in $DFSNFolders )
{
$dfs=$DFSNFolder.Path
$DFSTarget = Get-DfsnFolderTarget $DFS | Select Path,TargetPath,State
$DFSTarget|Export-Csv "DFS.csv" -NoTypeInformation -Append
}
}

@alanmckeon
Copy link

I get one or more parameter values passed to the method were invalid in the line $DFSNFolders = Get-DfsnFolder -path $ns

@GeraldStokesQuest
Copy link

Alanmckeon - To fix the error add a \ in the path

$DFSPath = "$($ns.path)*"

@laymanstake
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment