Travis
Travis

Reputation: 360

PowerShell: A drive with the name '@{Path=C' does not exist

I'm trying to search through a directory of files and pull out all the file paths that have the pattern. Then loop through each file and search for another pattern of text. It works if I manually do:

Select-String -Path "C:\inetpub\mailroot\Badmail-Archive\003c908531613052021000000A2.BAD" -Pattern ('Final-Recipient') | Select -ExpandProperty line

It does not if I do it in the loop:

$FileList = Get-ChildItem  "C:\inetpub\mailroot\Badmail-Archive" -Filter *.BAD | Select-String -Pattern 'Diagnostic-Code: smtp;550 5.1.1' | Select-Object Path
$FileList += Get-ChildItem  "C:\inetpub\mailroot\Badmail-Archive" -Filter *.BAD | Select-String -Pattern 'Diagnostic-Code: smtp;550 5.1.2' | Select-Object Path
$FileList += Get-ChildItem  "C:\inetpub\mailroot\Badmail-Archive" -Filter *.BAD | Select-String -Pattern 'Diagnostic-Code: smtp;550 5.2.1' | Select-Object Path



foreach($filepath in $FileList) { 
   $BADSearch = Select-String -Path $filepath -Pattern ('Final-Recipient') | Select -ExpandProperty    line
   $eMailAddress = $BADSearch.Split(";")[1]
   echo "File Path:  $filepath"
   echo $eMailAddress
}
File Path:  C:\inetpub\mailroot\Badmail-Archive\003c908531613052021000000A2.BAD

Select-String : Cannot find drive. A drive with the name '@{Path=C' does not exist.
At C:\Scripts\BadEmails-SQLGenerator.ps1:46 char:14
+ ... BADSearch = Select-String -Path $filepath -Pattern ('Final-Recipient' ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (@{Path=C:String) [Select-String], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SelectStringCommand
 

Upvotes: 1

Views: 2848

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

Your problem is with treating any arbitrary object as if it's a string. Select-String expects a string instance for the Path parameter, but you're passing it the resulting MatchInfo object returned from the previous Select-String call.

You'll need to tease out the path by selecting the .Path member from the previous command output:

Select-String -Path $filepath.Path # ...

Upvotes: 3

Related Questions