将错误从Remove-Item捕获到PowerShell脚本中的现有计算属性中以删除文件?

huangapple go评论58阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月13日 15:40:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定