首页 > 解决方案 > 使用powershell v2分配给递归gci搜索省略文件夹时没有返回变量

问题描述

我正在尝试递归搜索 Windows 7 机器上的文件 + 连接到它的网络驱动器,同时排除某些文件夹,即 C:\windows 和其中的所有递归文件夹,例如 system32。

我知道之前有人问过这个问题,但遵循答案并没有帮助,我仍然留下一个空白变量。

以下是我尝试过的组合:

       $AllDrives = Get-PSDrive
      $files=@("*.xml",*.txt)


 foreach ($Drive in $AllDrives) {
if ($Drive.Provider.Name -eq "FileSystem") {


$filenames = Get-ChildItem -recurse $drive.root -include ($files) -File | Where-Object {$_.PSParentPath -notlike "*Windows*" -and $_.PSParentPath -notlike "*Microsoft*"

 }

}

我也尝试过这些组合:

   $filenames = Get-ChildItem -recurse $drive.root -include ($files) -File | Where-Object {$_.PSParentPath -notmatch "Program Files|Users|Windows"}



   $exclude_pattern = $drive.root + "Windows"
    $filenames = Get-ChildItem -Force -Recurse -path $drive.root -Include $files  -Attributes !Directory+!System -ErrorAction "SilentlyContinue"  | Where-Object { $_.PSIsContainer -eq $false } | Where-Object { $_.FullName -notmatch $exclude_pattern }

不幸的是,经过一段时间后,当我$filename在终端中输入时,没有任何分配给它。

标签: powershellpowershell-2.0

解决方案


推荐阅读