0

以下を検索するスクリプトを作成しようとしています。

  1. 特定の名前を持つフォルダー。
  2. 特定の拡張子を持つファイル
  3. 検索から特定のディレクトリを除外します。

検索が実行されたら、1 と 2 をすべて削除し、3 と一致しない場合は親フォルダーも削除したいと思います。

#Define list of computers to search for the files and folders in question
$ComputerList = Get-Content -Path "C:\computers.txt"

#Define a function for searching files and folders based on criteria:
function search {
    PROCESS {
        $srcFolder ="C:\test"
        Get-ChildItem $srcFolder -ErrorAction SilentlyContinue  -recurse -Force | Where-Object {`
            $_.Name -eq “keyword1” -or`
            $_.Name -eq “keyword2” -or`

              -and $_.fullname -notmatch 'C:\\Windows'`
              -and $_.fullname -notmatch 'C:\\Program Files'
        } | foreach-object -process { _.FullName }
    }
}

foreach ($strComputer in $ComputerList)
{
    #Perform the search function on all the computers listed
    foreach ($objItem in $colItems)
    {
        write-host         "-------------------------$strComputer ----------------" -foregroundcolor "red"
        write-host         "                      Files Found                                                    " -foregroundcolor "yellow" -backgroundcolor "black"
             $ComputerList | search

        "Number of files and folders found: " +($ComputerList | search | Measure-Object | Select-Object -ExpandProperty Count)
        write-host "------------------------------------------------------------------" -foregroundcolor "red"
    }

    if (($ComputerList | search).count -lt 1) {
        write-host "No files found to delete"
    }
    else {
        #Prompt if you want to delete the files
        write-host         "Do you want to delete these files?" -foregroundcolor "yellow" -backgroundcolor "black"
        $ComputerList | search | Remove-Item -Force -confirm
    }
}
Out-File -FilePath C:\results.txt

だからここに私が抱えている問題があります:

  1. スクリプトを動作させることができます。ただし、除外されたフォルダーを保護しながら親フォルダーを削除する方法がわかりません。

  2. ファイルへの出力が機能していません。ファイルは作成されますが、空白です。なんで?

を見てGet-ChildItem | Get-Member、Parent プロパティが System.IO.DirectoryInfo Parent によって指定されていることに気付きました。それを削除する項目のリストに追加できれば、機能するはずです。

フォルダ構成は以下。おそらくより明確な方法で繰り返すために、フォルダーを削除したいと思います。

  1. 名前またはコンテンツがキーワードに一致するユーザー
  2. コンテンツがキーワードと一致するが、フォルダーの名前が一致しない場合 > 明示的に除外されているかどうかを確認します (例: C:\Windows、C:\Program Files など)。 > 除外されている場合はそのままにしておきます。そうでない場合は、フォルダーを削除します。**

プレーンな URL は次のとおりです。

oi42.tinypic.com/2nulmz4.jpg
oi43.tinypic.com/fwlxd0.jpg
oi42.tinypic.com/315hdw9.jpg
4

1 に答える 1

1

シンプルに保つことができます:

$folder = "C:\pst"
$excludedFolders = "c:\Windows", "c:\PST\new"

$itemsFromFolder = get-childitem $folder -recurse
$itemsToDelete = New-Object System.Collections.Generic.List[string]

foreach ($item in $itemsFromFolder)
{
    $exclude = $false
    foreach ($exclusion in $excludedFolders)
    {
        if ($item.FullName.ToLower().StartsWith($exclusion.ToLower()))
        {
            $exclude = $true
        }
    }
    if (!$exclude)
    {
        $itemsToDelete.Add($item.FullName)
    }
}

ご覧のとおり、事前に除外する必要があるフォルダーを定義し、すべてのアイテムを並べ替えて、保持する必要があるディレクトリを除外できます。次に、Remove-Item を使用して一部のパスまたは拡張子も除外できます。

Remove-Item c:\scripts\* -include *.txt -exclude *test*
于 2011-12-05T10:16:26.590 に答える