英文:
Capture the error from Remove-Item into the existing calculated property in PowerShell script to delete files?
问题
如何捕获无法删除的文件以及它们的错误消息到一个单独的变量中?
以下脚本可以执行删除操作,但我不确定如何获取仍在使用中的文件。
$totalSize = 0
$results = Get-ChildItem -Path $searchRoot -Filter *.log -Recurse | ForEach-Object {
$_ | Select-Object -Property Name,
FullName,
@{ n = 'Size (MB)'; e = { [math]::Round($_.Length / 1MB) } },
LastWriteTime
$totalSize += $_.Length
$paramRemoveItem = @{
Path = $_.FullName
Force = $true
Verbose = $true
}
Remove-Item @paramRemoveItem
}
$results
$totalSize
这是错误信息:
Remove-Item:无法删除项目C:\ErrorFile.BAK:进程无法访问文件'C:\ErrorFile.BAK',因为它正在被另一个进程使用。
在第17行的字符2处
+ Remove-Item @paramRemoveItem
英文:
How can I capture the files that cannot be deleted and their error message into a separate variable?
The below script can perform the deletion, but I'm not sure how to get the files that are still in use.
$totalSize = 0
$results = Get-ChildItem -Path $searchRoot -Filter *.log -Recurse | ForEach-Object {
$_ | Select-Object -Property Name,
FullName,
@{ n = 'Size (MB)'; e = { [math]::Round($_.Length / 1MB) } },
LastWriteTime
$totalSize += $_.Length
$paramRemoveItem = @{
Path = $_.FullName
Force = $true
Verbose = $true
}
Remove-Item @paramRemoveItem
}
$results
$totalSize
This is the error:
Remove-Item : Cannot remove item C:\ErrorFile.BAK: The process cannot access the file 'C:\ErrorFile.BAK' because it is being used by another process.
At line:17 char:2
+ Remove-Item @paramRemoveItem
答案1
得分: 1
Using error trapping and custom objects:
$totalSize = 0
$results = Get-ChildItem -Path $searchRoot -Filter *.log -Recurse | ForEach-Object {
Try {
# Remove '-WhatIf' to delete files
Remove-Item -Path $_.FullName -Force -Verbose -ErrorAction Stop -WhatIf
# You should try to avoid '+=' operations - please search Stack Overflow for rationale and alternate methods
$totalSize += $_.Length
# $Status populated with OK message
$Status = 'OK'
}
Catch {
# This block is only processed if there's an error in the 'Try' block
# $Status populated with the error message
$Status = $_.Exception
}
Finally {
# Output object for this item
[pscustomobject]@{Name=$_.Name;FullName=$_.FullName;SizeMB=([math]::Round($_.Length / 1MB));LastWriteTime=$_.LastWriteTime;Status=$Status}
}
}
$results
$totalSize
英文:
Using error trapping and custom objects:
$totalSize = 0
$results = Get-ChildItem -Path $searchRoot -Filter *.log -Recurse | ForEach-Object {
Try {
# Remove '-WhatIf' to delete files
Remove-Item -Path $_.FullName -Force -Verbose -ErrorAction Stop -WhatIf
# You should try to avoid '+=' operations - please search Stack Overflow for rationale and alternate methods
$totalSize += $_.Length
# $Status populated with OK message
$Status = 'OK'
}
Catch {
# This block is only processed if there's an error in the `Try` block
# $Status populated with the error message
$Status = $_.Exception
}
Finally {
# Output object for this item
[pscustomobject]@{Name=$_.Name;FullName=$_.FullName;SizeMB=([math]::Round($_.Length / 1MB));LastWriteTime=$_.LastWriteTime;Status=$Status}
}
}
$results
$totalSize
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论