Powershell:递归存在文件扩展名的目录如果存在则写出comman如果不是运行命令

Powershell: recurse directories for where a file extension exists if Exist write out comman if not run a command

具体来说,我有一个包含大量其他随机命名目录的目录(不是真正随机的,但这并不重要)。在这些目录中,有些包含扩展名为 .tsidx 的文件,有些则没有。

包含 .tsidx 扩展名的目录我想输出到屏幕,.tsidx 文件已经存在。那些我不希望它输出它不存在然后 运行 使用程序供应商提供的可执行文件针对目录构建 tsidx 文件的命令。

到目前为止,这是我的代码中的内容:

$index = Read-Host "Enter the Index Name" #This is to receive a directory  location from user
$loc = "F:\thawdb\splunk$index\thaweddb"
$dir = dir $loc

cd "d:\splunk\bin"
foreach ($d in $dir) 
{
   if (gci -dir | ? { !(gci $_ -file -recur -filter *tsidx) })
      {
         # writes out the directory contains files and doesn't need rebuilt
         Write-host -foregroundcolor Yellow "TSIDX Exists"
      }
      else
      {
         # writes out rebuild is necessary and runs the rebuild
         write-host -Foregroundcolor Green "Running Rebuild command against $loc$d" | .\splunk.exe rebuild $loc$d 
      }
}

不知道 splunk.exe 的作用,我认为应该这样做:

$index = Read-Host "Enter the Index Name" #This is to receive a directory  location from user
$loc = "F:\thawdb\splunk$index\thaweddb"

# get a collection of DirInfo objects for all directories in the path
$dir = Get-ChildItem $loc -Directory -Recurse

Set-Location "d:\splunk\bin"

# loop through the DirInfo collection
foreach ($d in $dir) {
   if ((Get-ChildItem -Path $d.FullName -File -Filter '*.tsidx').Count) {
         # writes out the directory contains files and doesn't need rebuilt
         Write-host -foregroundcolor Yellow "TSIDX Exists in $($d.FullName)"
    }
    else {
        # writes out rebuild is necessary and runs the rebuild
        Write-Host -Foregroundcolor Green "Running Rebuild command against $($d.FullName)" 
        & .\splunk.exe rebuild "$($d.FullName)"
    }
}