A while back I asked about Batch Script Redirection Syntax and received a nice answer. I've utilized the parenthesis ()
method more often to echo
in batch scripts and redirect to files, etc.
I've run into an odd issue where it appears the word Where
is not be able to be echo
'd as that word literally and it seems it's interpreted as the Where
command being invoked instead.
To keep this example simple, I've dumbed down the script logic to a much simpler version, but the PowerShell logic being echo
'd could be much more complex than this.
Script Example
@ECHO ON
(
ECHO [System.IO.DriveInfo]::GetDrives() ^| Where {$_.Name -like "C:\"}
)>> Outfile.txt
PAUSE
Error
INFO: Could not find files for the given pattern(s).
What I've done
I troubleshot this a little and did some quick research and couldn't find a simple answer. Since I have not asked many questions on SuperUser, I figured this would be a good one potentially.
One thing in particular I tried was setting a variable as a string value of Where
(i.e. SET w=Where
) and then in the parenthesized echo
commands I referenced that variable (i.e. %w%
) in place of the word Where
but the result is still the same error.
I also played around a little with SETLOCAL ENABLEDELAYEDEXPANSION
but it made no difference.
@ECHO ON
SET w=Where
(
ECHO [System.IO.DriveInfo]::GetDrives() ^| %w% {$_.Name -like "C:\"}
)>> Outfile.txt
PAUSE
The Workaround
I have simply not been using the parenthesized method for echo
ing the word "Where
" and I have just been utilizing the appended >>
redirect to the file method on each echo
'd line individually where "where
" is involved. No big deal for this small example but a bigger deal for a larger script.
@ECHO ON
ECHO [System.IO.DriveInfo]::GetDrives() ^| Where {$_.Name -like "C:\"}>> Outfile.txt
PAUSE
Correlated Questions
Hmmmmm......
What is causing this and what exactly is happening when this occurs?
Is there a way to allow the word (or string) "
Where
" to be used without the issue?
Resources
Answer
You have misdiagnosed the problem - WHERE has nothing to do with it.
You simply need to escape the closing parenthesis:
@ECHO ON
(
ECHO [System.IO.DriveInfo]::GetDrives(^) ^| Where {$_.Name -like "C:\"}
)>> Outfile.txt
PAUSE
No comments:
Post a Comment