October 10, 2016 at 8:35 am
I am using TSQL to assemble a PowerShell Get-ChildItem command to get a list of files on the server. When the call is successful, all is good, but it can happen that the call tries to access a non-existent folder. In that case, I get back a table-full of error message text, like this:
Get-ChildItem : Cannot find path 'D:\Databaze\Paleontologie\Prilohy\Verejne\q'
because it does not exist.
At line:1 char:2
+ & <<<< Get-ChildItem -Path D:\Databaze\Paleontologie\Prilohy\Verejne\q -recur
se -filter "q*927*" | Where-Object {$_.Name -match "[Qq]0{0,4}927[^\-\+0-9A-Za-
z]+.*$"} | Select Length, LastWriteTime, FullName | Write-Host
+ CategoryInfo : ObjectNotFound: (D:\Databaze\Pal...ilohy\Verejne
\q:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCh
ildItemCommand
If it was all on one line, it would not be a problem, but since it comes back as a ten-line table, I am not sure if I can reliably examine the text for an error message. If I can always be certain that the text comes back in the same order, I can simply look at the first record. However, this is dumped into a table variable as a heap, so theoretically (I think, anyway) these lines could be delivered to me in any order. Is there a way to be sure of the order in which the lines of this message come back, or do I have to read the entire temp table to be sure of getting the error message? Or is there another way altogether, like a PowerShell directive that would trim the error message to something sensible?
October 10, 2016 at 9:57 am
Okay, here's what I ended up doing. I don't suppose it's 100% reliable, since the pattern -could- occur in a legit path, but it's unlikely enough in my case that I'm not going to worry about it.
select @ErrCnt = (select COUNT(0) from @PrilohyDruheEvidenceTmp Where PowerShellResult like '%Cannot find path%')
if @ErrCnt = 0
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply