Skip to content

Commit f46ab1f

Browse files
Test-DbaPath - Handle xp_fileexist execution failures gracefully
When xp_fileexist raises a SQL exception (e.g. for UNC paths the SQL Server service account cannot resolve), wrap ExecuteWithResults in try/catch and return $false instead of crashing with "Cannot index into a null array". Fixes #10286 (do Test-DbaPath) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
1 parent 562e3ac commit f46ab1f

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

public/Test-DbaPath.ps1

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,26 @@ function Test-DbaPath {
9595
$query += "EXEC master.dbo.xp_fileexist '$p'"
9696
}
9797
$sql = $query -join ';'
98-
$batchresult = $server.ConnectionContext.ExecuteWithResults($sql)
98+
try {
99+
$batchresult = $server.ConnectionContext.ExecuteWithResults($sql)
100+
} catch {
101+
Write-Message -Level Verbose -Message "xp_fileexist execution failed for path(s) on $instance. The SQL Server service account may not have access to the specified path(s). Error: $_"
102+
if ($Path.Count -eq 1 -and $SqlInstance.Count -eq 1 -and (-not($RawPath -is [array]))) {
103+
return $false
104+
} else {
105+
foreach ($p in $PathsBatch) {
106+
[PSCustomObject]@{
107+
SqlInstance = $server.Name
108+
InstanceName = $server.ServiceName
109+
ComputerName = $server.ComputerName
110+
FilePath = $p
111+
FileExists = $false
112+
IsContainer = $false
113+
}
114+
}
115+
}
116+
continue
117+
}
99118
if ($Path.Count -eq 1 -and $SqlInstance.Count -eq 1 -and (-not($RawPath -is [array]))) {
100119
if ($batchresult.Tables.rows[0] -eq $true -or $batchresult.Tables.rows[1] -eq $true) {
101120
return $true

0 commit comments

Comments
 (0)