Search code examples
powershellscriptingps

Script returning error: "Get-Content : An object at the specified path ... does not exist, or has been filtered by the -Include or -Exclude parameter


EDIT I think I now know what the issue is - The copy numbers are not REALLY part of the filename. Therefore, when the array pulls it and then is used to get the match info, the file as it is in the array does not exist, only the file name with no copy number.

I tried writing a rename script but the same issue exists... only the few files I manually renamed (so they don't contain copy numbers) were renamed (successfully) by the script. All others are shown not to exist.

How can I get around this? I really do not want to manually work with 23000+ files. I am drawing a blank..

HELP PLEASE


I am trying to narrow down a folder full of emails (copies) with the same name "SCADA Alert.eml", "SCADA Alert[1].eml"...[23110], based on contents. And delete the emails from the folder that meet specific content criteria.

When I run it I keep getting the error in the subject line above. It only sees the first file and the rest it says do not exist...

The script reads through the folder, creates an array of names (does this correctly). Then creates an variable, $email, and assigns the content of that file. for each $filename in the array. (this is where is breaks)

Then is should match the specific string I am looking for to the content of the $email var and return true or false. If true I want it to remove the email, $filename, from the folder.

Thus narrowing down the email I have to review. Any help here would be greatly appreciated.

This is what I have so far... (Folder is in the root of C:)

$array = Get-ChildItem -name -Path $FolderToRead   #| Get-Content | Tee C:\Users\baudet\desktop\TargetFile.txt

Foreach ($FileName in $array){

    $FileName          # Check File

    $email = Get-Content $FolderToRead\$FileName  
    $email             # Check Content

    $ContainsString = "False"                      # Set Var 
    $ContainsString                                # Verify Var
    $ContainsString = %{$email -match "SYS$,ROC"}  # Look for String
    $ContainsString                                # Verify result of match

        #if ($ContainsString -eq "True") {
            #Remove-Item $FolderToRead\$element
            #}
}

Solution

  • Here's a PowerShell-idiomatic solution that also resolves your original problems:

    Get-ChildItem -File -LiteralPath $FolderToRead | Where-Object {
      (Get-Content -Raw -LiteralPath $_.FullName) -match 'SYS\$,ROC'
    } | Remove-Item -WhatIf
    

    Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

    Note how the $ character in the RHS regex of the -match operator is \-escaped in order to use it verbatim (rather than as metacharacter $, the end-of-input anchor).

    Also, given that $ is also used in PowerShell's string interpolation, it's better to use '...' strings (single-quoted, verbatim strings) to represent regexes, assuming no actual up-front string expansion is needed before the regex engine sees the resulting string - see this answer for more information.


    As for what you tried:

    • The error message stemmed from the fact that Get-Content $FolderToRead\$FileName binds the file-name argument, $FolderToRead\$FileName, implicitly (positionally) to Get-Content's -Path parameter, which expects PowerShell wildcard patterns.

      • Since your file names literally contain [ and ] characters, they are misinterpreted by the (implied) -Path parameter, which can be avoided by using the -LiteralPath parameter instead (which must be specified explicitly, as a named argument).
    • %{$email -match "SYS$,ROC"} is unnecessarily wrapped in a ForEach-Object call (% is a built-in alias); while that doesn't do any harm in this case, it adds unnecessary overhead;
      $email -match "SYS$,ROC" is enough, though it needs to be corrected to
      $email -match 'SYS\$,ROC', as explained above.