gpt4 book ai didi

robocopy 退出代码的 Powershell 按位比较

转载 作者:行者123 更新时间:2023-12-04 20:43:49 24 4
gpt4 key购买 nike

我无法解决这个按位转换问题。

Robocopy exit codes不符合正常的 0(成功)、1(失败)模式,所以我想将我的 robocopy 调用包装在下面的 powershell 脚本中,以使我的 TeamCity 构建配置失败或在 robocopy 终止时正确继续。

第一部分是使用 tip from the net 解决的与:($LastExitCode -band 24)它正确地将退出代码 8 到 16 视为失败 (1),将所有其他代码视为成功 (0)。

现在我想回显与退出代码对应的消息。如何将整数退出代码 (0 - 16) 转换和舍入/舍入为其等效的十六进制 (0x00 - 0x10)?

param(
[string] $source,
[string] $target,
[string[]] $action = @("/MIR"),
[string[]] $options = @("/R:2", "/W:1", "/FFT", "/Z", "/XA:H")
)
$cmd_args = @($source, $target, $action, $options)
& robocopy.exe @cmd_args
$returnCodeMessage = @{
0x00 = "[INFO]: No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized."
0x01 = "[INFO]: One or more files were copied successfully (that is, new files have arrived)."
0x02 = "[INFO]: Some Extra files or directories were detected. Examine the output log for details."
0x04 = "[WARN]: Some Mismatched files or directories were detected. Examine the output log. Some housekeeping may be needed."
0x08 = "[ERROR]: Some files or directories could not be copied (copy errors occurred and the retry limit was exceeded). Check these errors further."
0x10 = "[ERROR]: Usage error or an error due to insufficient access privileges on the source or destination directories."
}
Write-Host $returnCodeMessage[($LastExitCode <what goes here?>)]
exit ($LastExitCode -band 24)

最佳答案

在您的情况下,您不需要转换它。
您不需要转换,因为 Hashtable 键在预编译阶段转换为 [int]。
如果您查找 $returnCodeMessage.Keys ,您将看到十进制数字,而不是十六进制数字

要显示您应该使用的所有消息

$exitcode = $LastExitCode    
Write-Host $( @( $returnCodeMessage.Keys | Where-Object { $_ -band $exitcode } | ForEach-Object {return $returnCodeMessage[$_]} ) -join "`r`n")

如果要显示十六进制编码的 $LastExitCode,请执行
$exitcode = $LastExitCode
Write-Host $('0x' + [System.Convert]::ToString([int]$exitcode,[int]16) )
return $exitcode

String System.Convert.ToString(Int32, Int32)

关于robocopy 退出代码的 Powershell 按位比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21428632/

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