以下を検索するスクリプトを作成しようとしています。
- 特定の名前を持つフォルダー。
- 特定の拡張子を持つファイル
- 検索から特定のディレクトリを除外します。
検索が実行されたら、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
だからここに私が抱えている問題があります:
スクリプトを動作させることができます。ただし、除外されたフォルダーを保護しながら親フォルダーを削除する方法がわかりません。
ファイルへの出力が機能していません。ファイルは作成されますが、空白です。なんで?
を見てGet-ChildItem | Get-Member
、Parent プロパティが System.IO.DirectoryInfo Parent によって指定されていることに気付きました。それを削除する項目のリストに追加できれば、機能するはずです。
フォルダ構成は以下。おそらくより明確な方法で繰り返すために、フォルダーを削除したいと思います。
- 名前またはコンテンツがキーワードに一致するユーザー
- コンテンツがキーワードと一致するが、フォルダーの名前が一致しない場合 > 明示的に除外されているかどうかを確認します (例: C:\Windows、C:\Program Files など)。 > 除外されている場合はそのままにしておきます。そうでない場合は、フォルダーを削除します。**
プレーンな URL は次のとおりです。
oi42.tinypic.com/2nulmz4.jpg
oi43.tinypic.com/fwlxd0.jpg
oi42.tinypic.com/315hdw9.jpg