0

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.

7
  • 1
    Is your script the only script that writes to the csv or is there some other application/scheduled task also accessing it? As aside, instead of Write-Output $Obj | Export-Csv ... just feed the object itself to the Export-Csv cmdlet: $Obj | Export-Csv Commented Dec 12, 2023 at 15:45
  • No other app is writing/accessing it. Thank You, I will try that. Commented Dec 12, 2023 at 15:49
  • You do not have permission to read the file. If you are an admin try running PS by right click PS shortcut and select Run As Admin. Commented Dec 12, 2023 at 17:05
  • 1
    Generally speaking I would recommend capturing the output and writing it all to a file at once rather than appending to the file in a loop causing PowerShell to open and close the file over and over. Commented Dec 12, 2023 at 17:22
  • $Obj | Export-Csv did it however I still need to suspend it for 2 seconds but at least it's not skipping any of the report now to the csv file. Thank you very much, really appreciate it. Commented Dec 12, 2023 at 18:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.