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

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

编辑 我想我现在知道问题出在哪里了——拷贝数实际上并不是文件名的一部分。所以,当数组拉取然后用于获取匹配信息时,数组中原样的文件并不存在,只有没有拷贝数的文件名。

我尝试编写一个重命名脚本,但存在同样的问题...只有我手动重命名的几个文件(因此它们不包含拷贝数)被脚本重命名(成功)。所有其他的都显示不存在。

我该如何解决这个问题?我真的不想手动处理 23000 多个文件。我画的是空白..

请帮忙


我正在尝试缩小包含同名“SCADA Alert.eml”、“SCADA Alert[1].eml”...[23110] 的电子邮件(副本)的文件夹的范围,基于内容。并从文件夹中删除符合特定内容标准的电子邮件。

当我 运行 它时,我不断收到上面主题行中的错误。它只看到第一个文件,其余文件说不存在...

脚本读取整个文件夹,创建一个名称数组(正确执行此操作)。 然后创建一个变量 $email,并分配该文件的内容。对于数组中的每个 $filename。 (这是休息的地方)

那么应该将我要查找的特定字符串与 $email var 的内容相匹配,return true 或 false。如果为真,我希望它从文件夹中删除电子邮件 $filename。

从而缩小我必须查看的电子邮件范围。 如有任何帮助,我们将不胜感激。

这是我目前所拥有的...(文件夹在 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
            #}
}
[System.IO.Directory]::EnumerateFiles($Folder) |
    Where-Object {$true -eq [System.IO.File]::ReadAllText($_, [System.Text.Encoding]::UTF8).Contains('SYS$,ROC') } |
    ForEach-Object {
        Write-Host "Removing $($_)"
        #[System.IO.File]::Delete($_)
    }

你的错误:

  1. %{$email -match "SYS$,ROC"} - 百分比是多少?这是 ForEach-Object 别名。
  2. %{$email -match "SYS$,ROC"} - 为什么要使用 -match?这比 -likeString.Contains()
  3. 慢得多
  4. %{$email -match "SYS$,ROC"} - 当在双引号内使用 $ 时,您应该使用单个反引号 (I have `0) 进行转义。否则,$ 之后的所有内容都是变量名:Hello, $usernameI's $($weather.ToString()) today!
  5. 以正确的方式编写调试输出:使用 Write-DebugWrite-VerboseWrite-HostWrite-WarningWrite-ErrorWrite-Information

可以更好:

  1. 避免使用 Get-ChildItem,因为 Get-ChildItem returns 文件具有属性(如 mtime、atime、ctime 等)。此附加信息是每个文件的附加请求。当您只需要文件列表时,请使用来自 System.IO.Directory 的原生 .Net EnumerateFiles。这是对大量文件的显着性能提升。
  2. 使用 RealAllText or ReadAllLines or ReadAllBytes from System.IO.File static class 更具体而不是使用 universal 获取内容。
  3. 使用管道 ;-)

这是一个 PowerShell 惯用的解决方案,它也可以解决您原来的问题:

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

注意:上面命令中的-WhatIf common parameter预览操作。一旦您确定该操作将执行您想要的操作,请删除 -WhatIf

请注意 -match 运算符的 RHS regex 中的 $ 字符如何被 \ 转义以便使用它 verbatim(而不是作为元字符 $,输入结束锚点)。

此外,鉴于$也用于PowerShell的字符串插值,最好使用'...'字符串(单引号verbatim strings) 来表示正则表达式,假设在正则表达式引擎看到结果字符串之前不需要实际的 up-front 字符串扩展 - 请参阅 更多信息。


至于你试过的

  • 错误消息源于 Get-Content $FolderToRead$FileName 将文件名参数 $FolderToRead$FileName 隐式(按位置)绑定到 Get-Content's -Path parameter, which expects PowerShell wildcard patterns.

    • 由于您的文件名字面上包含 [] 字符,它们被(隐含的)[=22] 误解 =]参数可以通过使用-LiteralPath参数来避免(必须明确指定,作为 命名的 参数)。
  • %{$email -match "SYS$,ROC"} 不必要地包含在 ForEach-Object 调用中(% 是内置别名);虽然在这种情况下这不会造成任何伤害,但会增加不必要的开销;
    $email -match "SYS$,ROC" 就够了,不过需要更正为
    $email -match 'SYS$,ROC',如上所述。