gpt4 book ai didi

powershell - PowerShell中的陷阱错误无法按预期工作

转载 作者:行者123 更新时间:2023-12-03 08:10:26 24 4
gpt4 key购买 nike

我正在对Get-Children进行递归调用,以获取文件夹/目录结构的总大小。它可以正常工作,但不能将错误保留在我使用的ErrorVariable中,并且从查看文档来看,它应该应该吗?有任何想法吗?

        [string]$fsoobjexist = Test-Path -Path $Folder
$err=@()
If ($fsoobjexist -eq "True"){
$err.clear
if ((Get-Item $Folder) -is [System.IO.DirectoryInfo]){
$Foldersize = "{0:N2} MB" -f ((Get-ChildItem -LiteralPath $Folder -Force -Recurse | Measure-Object -Property Length -Sum -EV +err -EA Continue ).Sum / 1MB)
$logline = "$Folder,$Foldersize"
LogWrite $logline

Foreach ($Errors in $err){
ErrLogWrite "$($Errors.CategoryInfo.Category) $($Errors.CategoryInfo.Activity) $($Errors.CategoryInfo.Reason) $($Errors.CategoryInfo.TargetName) $($Errors.CategoryInfo.TargetType) $($Errors.Exception.Message)"
}
}
}

最佳答案

您已经将+err作为参数传递给ErrorVariableMeasure-Object参数,但是很可能是Get-ChildItem cmdlet引发了错误。

更改为:

$Foldersize = "{0:N2} MB" -f ((Get-ChildItem -LiteralPath $Folder -Force -Recurse -EV +err -EA Continue | Measure-Object -Property Length -Sum).Sum / 1MB) 

关于powershell - PowerShell中的陷阱错误无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53763106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com