Help please - I have a script to report all DFS folder targets and will check the path status but I'm getting a random export-csv error "The process cannot access the file \xxx\hhh\fff\report.csv because it is being used by another process.". It doesn't happen to each of the loop, script will run w/o an error for few loops then I will get the message. I tried adding Start-Sleep from 1-10 secods but still getting the same error.
Script below:
cls
$Report = "\\server1\DFSReport"
$Date = (Get-Date -format "MMddyyyy")
$Tab = [char]9
$Roots = Get-DfsnRoot -Path 'ABC.com\data'
ForEach ($Root in $Roots) {
Write-Host
Write-Host $Tab Enumarating $Root.Path -ForegroundColor Cyan
$Dirs = Get-DfsnFolder -Path "$($Root.Path)\*" | Where-Object { ($_.Path -notlike '*.DFSFolderLink*') -and ($_.Path -notlike '*test*') }
Write-Host $Tab Found $DIRs.Count Folder Targets -ForegroundColor DarkCyan
ForEach ($DIR in $DIRs) {
$Targets = Get-DfsnFolderTarget -Path $DIR.Path | Select-Object State, Path, NamespacePath, TargetPath, ReferralPriorityClass, ReferralPriorityRank
ForEach ($Target in $Targets) {
$TF = @{
$True = 'Accessible'
$False = 'NOT Accessible'
}
$DFSN = Test-Path $Target.NamespacePath
Write-Host
Write-Host $Tab $Tab Checking DFSN Path - $Target.NamespacePath - $TF[($DFSN)] -ForegroundColor Cyan
$NativePath = Test-Path $Target.TargetPath
Write-Host $Tab $Tab Checking DFSN Target Path - $Target.TargetPath - $TF[($NativePath)] -ForegroundColor DarkYellow
$Prop = [ordered]@{
'DFS State' = $Target.State
'NamespacePath' = $Target.NamespacePath
'NamespacePath Status' = $TF[($DFSN)]
'TargetPath' = $Target.TargetPath
'TargetPath Status' = $TF($NativePath)]
'ReferralPriorityClass' = $Target.ReferralPriorityClass
'ReferralPriorityRank' = $Target.ReferralPriorityRank
}
$Obj = New-Object -TypeName PSObject -Property $Prop
Write-Output $Obj | Export-Csv -Path "$Report\DSFN-Report-$Date.csv" -NoTypeInformation -Append
Start-Sleep -Seconds 4
}
}
}
Added Start-Sleep, maybe someone can help what am I doing wrong here.