0
我正在嘗試使用以下腳本記錄有關重命名文件的數據。唯一的問題是日誌文件包含的文件在嘗試重命名時由於「訪問被拒絕」錯誤而未重命名。我需要弄清楚如何爲成功重命名的文件創建日誌條目,或者將失敗的重命名管道傳送到不同的日誌文件。如果可能的話,我還希望在日誌文件頂部重新命名的文件總數(即'xxx文件被重命名')。我很感激任何關於使用powershell v2來使用它的建議。將重命名項目事件輸出到日誌文件
$drivesArray = Get-PSDrive -PSProvider 'FileSystem' | select -Expand Root
foreach ($drive in $drivesArray) {
Get-ChildItem $drive | Where-Object {
$_.FullName -notlike "${Env:WinDir}*" -and
$_.FullName -notlike "${Env:ProgramFiles}*"
} | ForEach-Object {
Get-ChildItem $_.FullName -Recurse -ErrorAction SilentlyContinue
} | Where-Object {
-not $_.PSIsContainer -and
$_.Extension -notmatch '^\.(xxx|exe|html)$'
} | ForEach-Object {
$newName = $_.FullName + '.xxx';
Rename-Item -Path $_.FullName -NewName ($_.FullName + '.xxx') -ErrorAction SilentlyContinue
Add-Content c:\temp\renameLog.txt -Value $('{0} {1} {2} {3}' -f $(Get-Date),$_.fullname,$_.name,$newName)
}
}
很棒的工作!只需要排除重命名日誌文件,它就像一個魅力 –