代码之家  ›  专栏  ›  技术社区  ›  Bill Paxtonn

是否在PowerShell中使用十六进制字节?

  •  1
  • Bill Paxtonn  · 技术社区  · 8 年前

    这个简单的PowerShell脚本“填充”了USB闪存驱动器的坏扇区。但我需要用包含0x00(所有位0)和0x255(所有位1)的文件填充驱动器。如何在PowerShell中使用十六进制?

    function filler {
        Param( [byte]$hex )
    
        $filearray = @()
        $count = 1
        $freespace = Get-PSDrive H
        $maxfiles = [int]($freespace.Free / 1048576)
    
        do {
            $randomnum = Get-Random -Minimum 100000000 -Maximum 999999999
    
            "$hex" * 1048576 | Out-File $randomnum
    
            $filecontent = Get-Content $randomnum -Raw
    
            if ($filecontent -notcontains ('$hex')) {
                # do nothing because the content is incorrect
            } else {
                $filearray += $randomnum
            }
    
            $count++
        } while ($count -le $maxfiles)
    
        foreach ($filename in $filearray) {
            Remove-Item $filename
        }
    }
    
    filler -hex 0x00
    filler -hex 0xFF
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Mathias R. Jessen    8 年前

    字符串 "0x00" 与表示的数值不同 0x00

    确保指定 -Encoding Byte 尝试在文件中读取和写入原始数据时:

    ,$hex * 1048576 | Set-Content $randomnum -Encoding Byte
    

    同样,在读取文件时:

    $filecontent = Get-Content $randomnum -Encoding Byte
    if($filecontent |Where-Object {$_ -notin @(0x00,0xFF)}){
        # do nothing
    }
    
    推荐文章