powershell - 检查文件的前 n 个字节是否在 Powershell 中是特定的

标签 powershell powershell-2.0

如何将文件中的字节与值的文字数组进行比较?

$path = "C:\BinaryFile.bin"
$bytes = Get-Content -LiteralPath $path -Encoding byte -TotalCount 4 -ReadCount 4
if ($bytes -eq [0 1 2 3]) { # How to compare to expected bytes?
    Write-Output "OK: $bytes"
}

最佳答案

也许比较对象。没有输出意味着相等。 Syncwindow 0 以获得准确的顺序。

$path = 'file'
[byte[]](0..3) | set-content $path -Encoding Byte
$bytes = Get-Content $path -Encoding byte -TotalCount 4
if (! (compare-object $bytes 0,1,2,3 -syncwindow 0)) { 
    "OK: $bytes"
}

或者做一个字符串比较:

if ("0 1 2 3" -eq $bytes) {
    "OK: $bytes"
}

关于powershell - 检查文件的前 n 个字节是否在 Powershell 中是特定的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70927857/

相关文章:

powershell - 无法在PowerShell中使用ADSI设置管理器属性

powershell - 电源外壳。创建一个类文件来保存自定义对象?

powershell - 如何将PowerShell版本从2.0升级到3.0

Powershell:在函数中定义详细开关

.net - Bash `set -e` 的 Powershell 模拟

powershell - 尝试创造。初始化并格式化VHD磁盘

powershell - 使用-asjob使脚本​​更有效

c# - SetCurrentConsoleFontEx 不适用于长字体名称

powershell - Get-WinEvent 仅获取交互式登录消息

java - 使用java执行Powershell脚本