这个简单的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
字符串 "0x00" 与表示的数值不同 0x00 。
"0x00"
0x00
确保指定 -Encoding Byte 尝试在文件中读取和写入原始数据时:
-Encoding Byte
,$hex * 1048576 | Set-Content $randomnum -Encoding Byte
同样,在读取文件时:
$filecontent = Get-Content $randomnum -Encoding Byte if($filecontent |Where-Object {$_ -notin @(0x00,0xFF)}){ # do nothing }